Click here to Skip to main content
15,897,291 members
Please Sign up or sign in to vote.
1.00/5 (1 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

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.
 
Share this answer
 
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900