Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15705

VS 2019 Listbox selection affecting if another listbox will save data

$
0
0
Hey folks! Let me start by saying my experience coding is next to nothing but somehow at work I have become responsible for maintaining this web app we have that is made with VB.net.
Recently, they wanted a new listbox added to the site. It was to work the exact same as another existing listbox, so I used the code from the original as an example. I've got it functioning and saving to the database, but I've run into one very odd issue I can't seem to figure out.

I have 2 listboxes: ddConditions (the original) and ddComplaints2 (I realized thee should prob start with lb, but one thing at a time!)
Users can select as many or as few items from them as they want from them, they can select neither, they can select a combination of the two, etc.
Both listboxes use the same code behind the scenes EXACTLY aside from their variable names.
Each also saves data into the main tblEvents, and their own respective tables (tblEvent_Conditions, tblEvent_Complaints).

The issue is that the newer listbox (ddComplaints2) will ONLY save if an item from the original listbox (ddConditions) has a selected value. Works absolutely as expected as long as ddConditions has a value.
The data always saves correctly to tblEvents, it's the tblEvent_Complaints where the data won't save if ddConditions has no value.

I have gone through all the functions and save parts and such and I can't find any sort of duplicated name or error that makes me see why this would happen.

Is there some common reason something like this might occur that I just don't know about because I'm new to VB?

Thanks for your time


Here are the two functions that save the data to the specific tables. SaveConditions seems to ALWAYS work. SaveComplaints works except when there is nothing to save in SaveConditions.

Code:

Public Function SaveConditions(ByVal iid As String, ByVal condid As String) As String

        Dim sqlcmd As String
        Dim da As New SqlDataAdapter
        Dim ds As New DataSet
        Dim cmdSQLStatement As New SqlCommand With {
            .Connection = conDatabaseIUse
        }

        'Select data from tblEvent_Conditions and put it into the ds variable.
        sqlcmd = "select event_id, condition_id from dbEvents.dbo.tblEvent_Conditions where event_id=" & iid & "" ' and condition_id IN (" & condid & ")"
        cmdSQLStatement.CommandText = sqlcmd
        da.SelectCommand = cmdSQLStatement
        da.Fill(ds)

        ' Delete existing events.
        If ds.Tables(0).Rows.Count > 0 Then
            sqlcmd = "delete from dbEvents.dbo.tblEvent_Conditions where event_id=" & iid & "" ' and condition_id IN (" & condid & ")"
            cmdSQLStatement.CommandText = sqlcmd
            da.SelectCommand = cmdSQLStatement
            da.Fill(ds)
        End If
 
        'Split the condid string into individual items and then insert them into tblEvent_Conditions.
        Dim s As String = condid
        Dim words As String() = s.Split(New Char() {","c})
        Dim word As String

        For Each word In words
            sqlcmd = "insert into dbEvents.dbo.tblEvent_Conditions values(" & iid & ",'" & word & "')"
            cmdSQLStatement.CommandText = sqlcmd
            da.SelectCommand = cmdSQLStatement
            da.Fill(ds)
        Next
 
        Dim rtn As String = "True"

        da.Dispose()
        ds.Dispose()
        cmdSQLStatement.Dispose()

        Return rtn
    End Function



    Public Function SaveComplaints(ByVal iid As String, ByVal compid As String) As String
        Dim sqlcmd As String
        Dim da As New SqlDataAdapter
        Dim ds As New DataSet
        Dim cmdSQLStatement As New SqlCommand With {
            .Connection = conDatabaseIUse
        }

        'Select data from tblEvent_Complaints and put it into the ds variable.
        sqlcmd = "select event_id, complaint_id from dbEvents.dbo.tblEvent_Complaints where event_id=" & iid & ""
        cmdSQLStatement.CommandText = sqlcmd
        da.SelectCommand = cmdSQLStatement
        da.Fill(ds)

        ' Delete existing events.
        If ds.Tables(0).Rows.Count > 0 Then
            sqlcmd = "delete from dbEvents.dbo.tblEvent_Complaints where event_id=" & iid & ""
            cmdSQLStatement.CommandText = sqlcmd
            da.SelectCommand = cmdSQLStatement
            da.Fill(ds)
        End If

        'Split the compid string into individual items and then insert them into tblEvent_Complaints.
        Dim s As String = compid
        Dim words As String() = s.Split(New Char() {","c})
        Dim word As String

        For Each word In words
            sqlcmd = "insert into dbEvents.dbo.tblEvent_Complaints values(" & iid & ",'" & word & "')"
            cmdSQLStatement.CommandText = sqlcmd
            da.SelectCommand = cmdSQLStatement
            da.Fill(ds)
        Next

        Dim rtn As String = "True"

        da.Dispose()
        ds.Dispose()
        cmdSQLStatement.Dispose()

        Return rtn
    End Function


Viewing all articles
Browse latest Browse all 15705

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>