We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,430 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

textbox databinding

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

2
Contributors
4
Replies
1 Day
Discussion Span
1 Year Ago
Last Updated
5
Views
Question
Answered
skran
Light Poster
48 posts since Nov 2011
Reputation Points: 19
Solved Threads: 2
Skill Endorsements: 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?

adam_k
Veteran Poster
1,057 posts since Jun 2011
Reputation Points: 274
Solved Threads: 205
Skill Endorsements: 11

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

skran
Light Poster
48 posts since Nov 2011
Reputation Points: 19
Solved Threads: 2
Skill Endorsements: 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
skran
Light Poster
48 posts since Nov 2011
Reputation Points: 19
Solved Threads: 2
Skill Endorsements: 0

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

adam_k
Veteran Poster
1,057 posts since Jun 2011
Reputation Points: 274
Solved Threads: 205
Skill Endorsements: 11
Question Answered as of 1 Year Ago by adam_k

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0639 seconds using 2.65MB