Question Try Find out correct solution (combobox more than 2 combo)

dbdev999

New member
Joined
Apr 17, 2013
Messages
3
Programming Experience
Beginner
Hi Guy

I have 4 Combo box
data for 1.CmbJobType ==> 01.jpg
data for 2.CmbIssueType ==> 02.jpg
data for 3.CmbIssueSystem ==> 03.jpg
data for 4.CmbIssueTopic ==> 04.jpg
I try to debug but not work . Any one can guide me for fix it on First POst
Thank in Advance , My code (f_action form for edit data) as below.
01.jpg02.jpg03.jpg04.jpgErr.jpg

Sub GetData2Combo()
     
        'try
        ConnectDB()
        strSQL = ""
        strSQL = "Select * From tblJobtype order by JobTypeID"
        da.SelectCommand.CommandText = strSQL

        If ds.Tables.Contains("ds_jobtype") = True Then
            ds.Tables("ds_jobtype").Clear()
        End If

        da.Fill(ds, "ds_jobtype")

        If ds.Tables("ds_jobtype").Rows.Count <> 0 Then
            Dim Dr As DataRow
            Dim dt As DataTable
            dt = ds.Tables("ds_Jobtype")
            Debug.WriteLine("Getdata2Combo\DS_JOBTYPE")
            For Each Dr In dt.Rows
                Debug.WriteLine(Dr.Item("JobTypeID") & " | " & Dr.Item("JobTypeDesc"))
            Next

            With cmbJobtype
                .DataSource = ds.Tables("ds_jobtype")
                .DisplayMember = "jobtypedesc"
                .ValueMember = "jobtypeid"
                '.SelectedIndex = 0
            End With
        End If

        da.Dispose()
        ds.Dispose()

        'Catch ex As Exception

        '    MessageBox.Show(ex.message)

        'End Try

    End Sub

Private Sub f_action_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim strSQL As String = Nothing
        Dim Myconnection As SqlConnection
        Dim dr As SqlDataReader
        Dim Cmd As SqlCommand

       
        GetData2Combo()

        strSQL = "Select * from tbllogcall where logno='" & f_monitor.Str_Logno & "'"
        Myconnection = New SqlConnection(Strcn)
        da = New SqlDataAdapter(strSQL, Myconnection)

        da.Fill(ds, "focus_logcall")

        If ds.Tables("focus_logcall").Rows.Count = 0 Then
            is_Found = False
            MessageBox.Show("ไม่พบข้อมูลที่ต้องการ", "ผลการค้น", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Exit Sub
        End If

        If ds.Tables("focus_logcall").Rows.Count <> 0 Then
           '=============== Begin  Binding Master Table Section 
            is_Found = True
            With cmbOutlet

                .DataBindings.Clear()
                .DataBindings.Add("selectedvalue", ds, "focus_logcall.outletid")
            End With
            With cmbJobtype

          
                .DataBindings.Clear() 
                .DataBindings.Add("selectedvalue", ds, "focus_logcall.jobtypeid")
            End With
            With cmbpriority

           
                .DataBindings.Clear()
                .DataBindings.Add("selectedvalue", ds, "focus_logcall.priorityid")
            End With

            With txtjobdetail

                .DataBindings.Clear() 
                .DataBindings.Add("text", ds, "focus_logcall.jobdetail")
            End With

            BindingCTRL()

End sub

 Sub BindingCTRL()
        Dim strSQL As String

        strSQL = ""
        strSQL = "Select IssueTypeCode,IssueTypeDesc From tblIssueType Where JobTypeID='" & ds.Tables("focus_logcall").Rows(0).Item("JobTypeID").ToString & "'       or IssueTypeCode='00' order by IssueTypeCode ASC"
        da.SelectCommand.CommandText = strSQL

        If ds.Tables.Contains("ds_IssueType") = True Then
            ds.Tables("ds_IssueType").Clear()
        End If

        da.Fill(ds, "ds_IssueType")

        If ds.Tables("ds_IssueType").Rows.Count <> 0 Then
            Dim Dr As DataRow
            Dim dt As DataTable
            dt = ds.Tables("ds_IssueType")
            Debug.WriteLine("BindingCTRL\DS_IssueType")
            For Each Dr In dt.Rows
                Debug.WriteLine(Dr.Item("IssueTypeCode") & " | " & Dr.Item("IssueTypeDesc"))
            Next

            With cmbIssueType
                .DataSource = ds.Tables("ds_IssueType")
                .ValueMember = "IssueTypeCode"
                .DisplayMember = "IssueTypeDesc"
                '.SelectedIndex = 0
                .DataBindings.Clear()
                .DataBindings.Add("selectedvalue", ds, "focus_logcall.IssueTypeCode")
            End With
        End If

        strSQL = ""
        strSQL = "Select SysCode,SysDesc From tblIssueSystem Where IssueTypeCode='" & ds.Tables("focus_logcall").Rows(0).Item("IssueTypeCode").ToString & "' or SysCode='00' order by SysCode ASC"
        da.SelectCommand.CommandText = strSQL

        If ds.Tables.Contains("ds_IssueSystem") = True Then
            ds.Tables("ds_IssueSystem").Clear()
        End If

        da.Fill(ds, "ds_IssueSystem")

        If ds.Tables("ds_IssueSystem").Rows.Count <> 0 Then
            Dim Dr As DataRow
            Dim dt As DataTable
            dt = ds.Tables("ds_IssueSystem")
            Debug.WriteLine("BindingCTRL\ds_IssueSystem")
            For Each Dr In dt.Rows
                Debug.WriteLine(Dr.Item("SysCode") & " | " & Dr.Item("SysDesc"))
            Next

            With cmbissueSystem
                .DataSource = ds.Tables("ds_IssueSystem")
                .ValueMember = "SysCode"
                .DisplayMember = "SysDesc"
                '.SelectedIndex = 0
                .DataBindings.Clear()
                .DataBindings.Add("selectedvalue", ds, "focus_logcall.SysCode")
            End With
        End If

        strSQL = ""
        strSQL = "Select TopicCode,TopicDesc From tblIssueTopic where Syscode='" & ds.Tables("focus_logcall").Rows(0).Item("SysCode").ToString & "' or TopicCode='00' order by TopicCode ASC"
        da.SelectCommand.CommandText = strSQL

        If ds.Tables.Contains("ds_IssueTopic") = True Then
            ds.Tables("ds_IssueTopic").Clear()
        End If


        da.Fill(ds, "ds_IssueTopic")

        If ds.Tables("ds_IssueTopic").Rows.Count <> 0 Then

            Dim Dr As DataRow
            Dim dt As DataTable
            dt = ds.Tables("ds_IssueTopic")
            Debug.WriteLine("BindingCTRL\ds_IssueTopic")
            For Each Dr In dt.Rows
                Debug.WriteLine(Dr.Item("TopicCode") & " | " & Dr.Item("TopicDesc"))
            Next

            With cmbIssueTopic
                .DataSource = ds.Tables("ds_IssueTopic")
                .ValueMember = "TopicCode"
                .DisplayMember = "TopicDesc"
                '.SelectedIndex = 0
                .DataBindings.Clear()
                .DataBindings.Add("selectedvalue", ds, "focus_logcall.TopicCode")
            End With
        End If

    End Sub


    Private Sub cmbIssueType_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbIssueType.SelectedIndexChanged


        If ds.Tables.Contains("ds_IssueSystem") = True Then
            ds.Tables("ds_IssueSystem").Clear()
        End If

        'Try


        strSQL = ""
        strSQL = "select sysCode,sysDesc from tblissueSystem where issuetypeCode='" & cmbIssueType.SelectedValue.ToString & "' or issuetypeCode='00' order by sysCode ASC"
        da = New SqlDataAdapter(strSQL, Cn)


        da.Fill(ds, "ds_IssueSystem")
        

        If ds.Tables("ds_IssueSystem").Rows.Count <> 0 Then

            Dim Dr As DataRow
            Dim dt As DataTable
            dt = ds.Tables("ds_IssueSystem")
            Debug.WriteLine("CmbIssueType IndexChanged\ds_IssueSystem")
            For Each Dr In dt.Rows
                Debug.WriteLine(Dr.Item("SysCode") & " | " & Dr.Item("SysDesc"))
            Next

            With cmbissueSystem

              
                .DataSource = dt
                .DisplayMember = "sysdesc"
                .ValueMember = "syscode"
                .SelectedIndex = 0

               

            End With
        End If
        'Catch ex As Exception
        'MessageBox.Show(ex.Message)
        'Exit Sub
        'End Try

    End Sub


    Private Sub cmbJobtype_SelectedIndexChanged1(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbJobtype.SelectedIndexChanged
        If ds.Tables.Contains("ds_IssueType") = True Then
            ds.Tables("ds_IssueType").Clear()
        End If

        'Try

            strSQL = ""
            strSQL = "select issueTypecode,issueTypeDesc from tblissueType where jobtypeID='" & cmbJobtype.SelectedValue.ToString & "' or jobtypeID='00'  order by issueTypeCode ASC"
            da = New SqlDataAdapter(strSQL, Cn)

            da.Fill(ds, "ds_Issuetype")



            If ds.Tables("ds_Issuetype").Rows.Count <> 0 Then

                Dim Dr As DataRow
                Dim dt As DataTable
                dt = ds.Tables("ds_Issuetype")
                Debug.WriteLine("CmbJobType IndexChanged\ds_Issuetype")
                For Each Dr In dt.Rows
                    Debug.WriteLine(Dr.Item("issueTypeCode") & " | " & Dr.Item("issueTypeDesc"))
                Next

                With cmbIssueType
                   
                    .DataSource = dt
                    .DisplayMember = "IssueTypedesc"
                    .ValueMember = "IssueTypeCode"
                    .SelectedIndex = 0

       
                End With

            End If

       ' Catch ex As Exception
            'MessageBox.Show(ex.Message & ex.InnerException.ToString)
            'Exit Sub

        'End Try
    End Sub

    Private Sub cmbissueSystem_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbissueSystem.SelectedIndexChanged

        If ds.Tables.Contains("ds_IssueTopic") = True Then
            ds.Tables("ds_IssueTopic").Clear()
        End If

        'Try


        strSQL = ""
        strSQL = "select TopicCode,TopicDesc from tblissueTopic where TopicCode='" & cmbissueSystem.SelectedValue.ToString & "' or TopicCode='00' order by TopicCode ASC"
        da = New SqlDataAdapter(strSQL, Cn)


        da.Fill(ds, "ds_IssueTopic")
        

        If ds.Tables("ds_IssueTopic").Rows.Count <> 0 Then

            Dim Dr As DataRow
            Dim dt As DataTable
            dt = ds.Tables("ds_IssueTopic")
            Debug.WriteLine("CmbIssueSYstem IndexChanged\ds_IssueTopic")
            For Each Dr In dt.Rows
                Debug.WriteLine(Dr.Item("TopicCode") & " | " & Dr.Item("TopicDesc"))
            Next

            With cmbIssueTopic

               
                .DataSource = dt
                .DisplayMember = "Topicdesc"
                .ValueMember = "Topiccode"
                .SelectedIndex = 0

            End With
        End If
        'Catch ex As Exception
        'MessageBox.Show(ex.Message)
        ' Exit Sub
        'End Try
    End Sub


 
Last edited by a moderator:
That almost certainly means that the SelectedValue of the ComboBox is Nothing, i.e. no item is selected. That means that you need to check whether an item is selected before trying to use it.

Also, use parameters rather than string concatenation to insert values into SQL code. To learn why and how, follow the Blog link in my signature and check out my post on Parameters In ADO.NET.
 
That almost certainly means that the SelectedValue of the ComboBox is Nothing, i.e. no item is selected. That means that you need to check whether an item is selected before trying to use it.

Also, use parameters rather than string concatenation to insert values into SQL code. To learn why and how, follow the Blog link in my signature and check out my post on Parameters In ADO.NET.

Thanks you :O) I will Try.
 
Back
Top