HELP!!
I try to bind a textbox to a datatable but i cant.. I checked if the datatable is filled (it includes the results of a sql query)and it s ok.. i can depict the result in a combo box but not in textbox. I wait for only one result that s why i need textbox instead of combo.. that s my code.. i m desperate ..

Dim StateTable As New DataTable
        StateTable.Clear()
       ' Dim ds As New DataSet
        Dim sqlCmd As New SqlCommand("select DISTINCT State_LCL from T_MUNICIPALITY,T_MUNICIPALITY_STREETS where  T_MUNICIPALITY.Municipality_LCL=T_MUNICIPALITY_STREETS.Municipality_LCL and T_MUNICIPALITY_STREETS.Municipality_LCL='" & txtMunicipality.Text & "'", conn)
       
 Dim StateDA As New SqlDataAdapter(sqlCmd)

        StateDA.Fill("StateTable")
         txtState.Text = DSmunicipality.Tables(0).Rows(0)("State_LCL").ToString()

The error says "There is no row at position 0."

Recommended Answers

All 4 Replies

If you are expecting only 1 value and you are not really binding -let's face it you are just trying to assign a value - why are you using a datatable?

Wouldn't it be easier to use an ExecuteScalar and get the value you are looking to assign to the textbox?

I'll try it and I'll be back!

Yeah!!! It works!! This is what I did! Thank you so much!!!! :)

Dim sqlCmd As New SqlCommand("select DISTINCT State_LCL from T_MUNICIPALITY,T_MUNICIPALITY_STREETS where  T_MUNICIPALITY.Municipality_LCL=T_MUNICIPALITY_STREETS.Municipality_LCL and T_MUNICIPALITY_STREETS.Municipality_LCL='" & txtMunicipality.Text & "'", conn)
conn.Close()

        Dim result As String
        Try
            conn.Open()
            result = sqlCmd.ExecuteScalar()

            If result IsNot Nothing Then
                txtState.Text = result.ToString()
             End If
        Catch ex As SqlException
            MessageBox.Show(ex.Message)
        End Try

If your issue is resolved, please mark this thread as solved. Thanks.

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.