Dim mm As New SqlClient.SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=Integrated Security=True;Connect Timeout=30;User Instance=True")
mm.Open()
Dim mm1 As New SqlDataAdapter("select * from Area_type order by Area_name", mm)
Dim ds As New DataSet
mmm1.Fill(ds)
With ComboBox1
.DataSource = ds.Tables(0)
.DisplayMember = "Area_name"
.ValueMember = "Area_name"
.SelectedIndex = 0
End With
mm.Close()
'hi i am student and what do i now for bind other combobox1 with combobbox2 help of above code????'
'below code fill textbox to combobox3 Selected item what i do plz give full code with detail thanx'
Dim mm1 As New SqlClient.SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=;Integrated Security=True;Connect Timeout=30;User Instance=True")
mm.Open()
Dim mm2 As New SqlDataAdapter("select * from Add_Book order by Book_name", mm)
Dim ds1 As New DataSet
mm2.Fill(ds1)
With ComboBox3
.DataSource = ds1.Tables(0)
.DisplayMember = "Book_name"
.ValueMember = "Book_id"
.SelectedIndex = 0
End With
Private Sub ComboBox3_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox3.SelectedIndexChanged
Dim mm3 As New SqlClient.SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=;Integrated Security=True;Connect Timeout=30;User Instance=True")
Dim da As New SqlDataAdapter
Dim dq As New DataSet
Dim dt As DataTable
Try
da = New SqlDataAdapter("select * from Add_Book where Book_price = " & ComboBox1.SelectedItem & "", mm3)
da.Fill(dq, "Add_Book")
dt = dq.Tables("Add_Book")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
TextBox1.Text = dt.Rows.Item(0).Item(0)
End Sub
Dani AI
Generated
A concise, safe pattern for binding two ComboBoxes (parent → dependent) and filling a TextBox from the selected child item. This builds on ’s code while avoiding common mistakes: leaving connections open, concatenating values into SQL, relying on SelectedItem.ToString for queries, and not guarding against SelectedIndexChanged firing during binding.
Steps (high level)
- Populate the parent ComboBox from a DataTable and set DisplayMember/ValueMember.
- In the parent’s SelectedIndexChanged, call a method that fills the child ComboBox using a parameterized query (or a DataView.RowFilter) so the child shows only related rows.
- Read other fields from the selected child row via the DataRowView (not by running a new concatenated query) and handle DBNull.
- Use Using blocks to dispose connections and guard event handlers during initial binding.
Example VB.NET pattern (replace table/column names and connection string as needed):
' Imports System.Data.SqlClient
Private Sub LoadParents()
Using cn As New SqlConnection(My.Settings.DbConn)
Dim da As New SqlDataAdapter("SELECT ParentID, ParentName FROM tblParents ORDER BY ParentName", cn)
Dim dt As New DataTable()
da.Fill(dt)
comboParent.DataSource = dt
comboParent.DisplayMember = "ParentName"
comboParent.ValueMember = "ParentID"
End Using
End Sub
Private Sub comboParent_SelectedIndexChanged(sender As Object, e As EventArgs) Handles comboParent.SelectedIndexChanged
If comboParent.SelectedValue Is Nothing Then Return
LoadChildren(CInt(comboParent.SelectedValue))
End Sub
Private Sub LoadChildren(parentId As Integer)
Using cn As New SqlConnection(My.Settings.DbConn)
Using cmd As New SqlCommand("SELECT ChildID, ChildName, Price FROM tblChildren WHERE ParentID = @p ORDER BY ChildName", cn)
cmd.Parameters.Add("@p", SqlDbType.Int).Value = parentId
Dim da As New SqlDataAdapter(cmd)
Dim dt As New DataTable()
da.Fill(dt)
comboChild.DataSource = dt
comboChild.DisplayMember = "ChildName"
comboChild.ValueMember = "ChildID"
End Using
End Using
End Sub
Private Sub comboChild_SelectedIndexChanged(sender As Object, e As EventArgs) Handles comboChild.SelectedIndexChanged
If comboChild.SelectedItem Is Nothing Then Return
Dim row As DataRowView = CType(comboChild.SelectedItem, DataRowView)
TextBoxPrice.Text = If(IsDBNull(row("Price")), String.Empty, row("Price").ToString())
End Sub Troubleshooting notes
- SelectedIndexChanged fires when DataSource is set; wrap bindings or check for Nothing to avoid null errors.
- Prefer parameterized commands to prevent SQL errors and injection.
- Use DataBindings.Add("Text", dataSource, "Column") if automatic textbox updates are preferred.
- Always check for DBNull before reading column values.
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.