Bind Textbox to Dataset

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • raamay
    New Member
    • Feb 2007
    • 107

    Bind Textbox to Dataset

    I have a module where i have specified the connection string as below:
    Code:
    Public Function Connection() As String
            Return "Data Source=192.168.0.1,1433;Network Library=DBMSSOCN;Initial Catalog=test;User ID=sa;Password=123456"
        End Function
    Then i have a form which has two textboxes. What i am trying to do is bind the two textboxes to a dataset in the following way:
    Code:
    Imports System.Data.SqlClient
    
    Public Class Form1
        Inherits System.Windows.Forms.Form
    
        Dim CONNECTION_STRING As String = Connection()
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim Connection As New SqlClient.SqlConnection(CONNECTION_STRING)
    
            Dim ds As New DataSet
    
            Dim sql As String = "SELECT * FROM tblname"
    
            Dim da As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter(sql, Connection)
    
            Try
                da.Fill(ds, "tblname")
            Finally
                da.Dispose()
            End Try
            Me.TextBox1.DataBindings.Add("Text", ds, "firstname")
            Me.TextBox1.DataBindings.Add("Text", ds, "lastname")
    
        End Sub
    I get the error "Cannot bind to the property or column firstname on the DataSource. Parameter name: dataMember".

    What is wrong and how can i achieve this..please direct me.
  • aryanbs
    New Member
    • Mar 2009
    • 42

    #2
    Me.TextBox1.Dat aBindings.Add(" Text", ds.Tables("tbln ame"), "firstname" )

    Comment

    • raamay
      New Member
      • Feb 2007
      • 107

      #3
      thankyou for the help but it didnt work. So i tried in the following way and it worked. But i am not sure what is the difference between the binding method and the one below:
      Code:
      TextBox1.Text = ds.Tables(0).Rows(index).Item("firstname").ToString
      TextBox2.Text = ds.Tables(0).Rows(index).Item("lastname").ToString
      In fact this way also works but i am not sure why is the difference.
      Code:
      TextBox1.DataBindings.Add("Text", ds.Tables(0), "firstname")
      TextBox2.DataBindings.Add("Text", ds.Tables(0), "lastname")

      Comment

      • aryanbs
        New Member
        • Mar 2009
        • 42

        #4
        Excellant, you found the problem

        ds.Tables("tbln ame") but ds.Tables(0), so 0 is the table you are binding, i thought you were also filling datatable with name tblname, its samething whether you go by table name or table index

        In first case you are assigning value of certain row manually to textbox, in another one you are binding the property text to data source

        Look at this link as well

        Comment

        Working...