I have 1 form whose columns are slno,type,shape
and another form where i want to retrive data from database using combo box.
if i select slno from combo box in form2,then rest of the fields should display in other textbox in form2.

 Dim cn As New SqlConnection("Data Source=.\INSTANCE;initial catalog=record;user=sa;password=gariahat")
        Dim da As New SqlDataAdapter
        Dim cmd As New SqlCommand
        'Dim dr As New SqlDataReader
        Dim ds As New DataSet
        For Each row As DataRow In ds.Tables("data").Rows

            If row(0).ToString = ComboBox1.Text Then
                TextBox3.Text = row(1).ToString()
                TextBox4.Text = row(2).ToString()

                Exit For

            End If
        Next

Quoted Text Here

please can you give me the whole code in vb.net2008 as i am using vb.net 2008 and sqlserver 2005
but its showing error as Object reference not set to an instance of an object in the below line

For Each row As DataRow In ds.Tables("data").Rows

Recommended Answers

All 2 Replies

Quoted Text Here

I have updated the code like this

Dim strcon As String = ("Data Source=.\INSTANCE;initial catalog=record;user=sa;password=gariahat")
        Dim cn As New SqlConnection("Data Source=.\INSTANCE;initial catalog=record;user=sa;password=gariahat")
        ' Dim da As New SqlDataAdapter
        'Dim cmd As New SqlCommand
        'Dim dr As New SqlDataReader
        Dim ds As New DataSet

        cmd.Connection = cn
        cn.Open()
        Dim dt As New DataTable
        Dim da As New SqlDataAdapter("select * from data", cn)
        da.Fill(dt)
        For Each row As DataTable In ds.Tables("data").Rows
            If row(0).ToString = ComboBox1.Text Then
                TextBox3.Text = dt.Rows(0)("type").ToString()
                TextBox4.Text = dt.Rows(1)("shape").ToString()
                TextBox5.Text = dt.Rows(2)("size").ToString()
                TextBox6.Text = dt.Rows(3)("place").ToString()
                TextBox7.Text = dt.Rows(1)("weight").ToString()
                TextBox3.ReadOnly = True
                TextBox4.ReadOnly = True
                TextBox5.ReadOnly = True
                TextBox6.ReadOnly = True
                TextBox7.ReadOnly = True
                Exit For
            End If
        Next


        cn.Close()

But still showing same error. the loop for each is not working.
Please kindly help. i will be greatful
thanks
sudeshna

I fixed it, but replaced the For Each with For:

        For i As Integer = 0 To ds.Tables("data").Rows.Count - 1
            If ComboBox1.SelectedItem.ToString = ds.Tables("data").Rows(i).Item("YourColName").ToString() Then
                TextBox1.Text = ds.Tables("data").Rows(i+1).Item("YourColName").ToString()
                TextBox2.Text = ds.Tables("data").RowsRows(i+2).Item("YourColName").ToString()
                '.... add more textbox
            End If
        Next
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.