1,105,644 Community Members

textbox databinding

Member Avatar
skran
Light Poster
48 posts since Nov 2011
Reputation Points: 9 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

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."

Member Avatar
adam_k
Veteran Poster
1,056 posts since Jun 2011
Reputation Points: 239 [?]
Q&As Helped to Solve: 212 [?]
Skill Endorsements: 17 [?]
 
0
 

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?

Member Avatar
skran
Light Poster
48 posts since Nov 2011
Reputation Points: 9 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
skran
Light Poster
48 posts since Nov 2011
Reputation Points: 9 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

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
Member Avatar
adam_k
Veteran Poster
1,056 posts since Jun 2011
Reputation Points: 239 [?]
Q&As Helped to Solve: 212 [?]
Skill Endorsements: 17 [?]
 
0
 

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

Question Answered as of 2 Years Ago by adam_k
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: