Click here to Skip to main content
14,579,188 members
Rate this:
Please Sign up or sign in to vote.
See more:
Private Sub Form7_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim dr As DataRow
        con = New SqlConnection(conn)
        con.Open()
        Dim dt As DataTable
        dt = New DataTable()

        da = New SqlDataAdapter("select LAID  from LoanApplication where statuss=1", con)
        da.Fill(dt)
        
        If dt.Rows.Count > 0 Then
            dr = dt.NewRow()
            dr.ItemArray = New Object() {0, "--Select Name--"}
            dt.Rows.InsertAt(dr, 0)
           
            ComboBox3.ValueMember = "LAID"
            ComboBox3.DisplayMember = "LAID"
            ComboBox3.DataSource = dt

        End If

        con.Close()
    End Sub
End Class



What I have tried:

Private Sub Form7_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim dr As DataRow
        con = New SqlConnection(conn)
        con.Open()
        Dim dt As DataTable
        dt = New DataTable()

        da = New SqlDataAdapter("select LAID  from LoanApplication where statuss=1", con)
        da.Fill(dt)
        
        If dt.Rows.Count > 0 Then
            dr = dt.NewRow()
            dr.ItemArray = New Object() {0, "--Select Name--"}
            dt.Rows.InsertAt(dr, 0)
           
            ComboBox3.ValueMember = "LAID"
            ComboBox3.DisplayMember = "LAID"
            ComboBox3.DataSource = dt

        End If

        con.Close()
    End Sub
End Class
Posted
Comments
F-ES Sitecore 24-Jun-20 1:54am
   
Your DataTable only has one column (LAID) yet you are trying to add two values to it (0 and "--Select Name--"). Rather than adding data to the datatable, add the "Select Name" to the combobox directly. Set it's property that appends databound items rather than replace them.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

You only SELECT one column from your DB:
da = New SqlDataAdapter("select LAID  from LoanApplication where statuss=1", con)
da.Fill(dt)
Then you try to set two data items to a row of the table:
dr = dt.NewRow()
dr.ItemArray = New Object() {0, "--Select Name--"}
The system looks at it, and complains because it doesn't know what to do with your data: if it add a column to the DataTable automatically, it has no idea what to put in the other rows, if it doesn't, then it has to discard data you have just supplied.

Probably, you need to SELECT the row/number or ID from your DB as well as the LAID value.
   
Comments
Richard MacCutchan 24-Jun-20 2:08am
   
A database that helps you get "LAID". Blimey, whatever next? :))
OriginalGriff 24-Jun-20 2:39am
   
I'm jealous.
None of *my* databases have ever helped me get laid...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100