Connecting Tech Pros Worldwide Forums | Help | Site Map

Bind Textbox to Dataset

Member
 
Join Date: Feb 2007
Posts: 75
#1: Mar 26 '09
I have a module where i have specified the connection string as below:
Expand|Select|Wrap|Line Numbers
  1. Public Function Connection() As String
  2.         Return "Data Source=192.168.0.1,1433;Network Library=DBMSSOCN;Initial Catalog=test;User ID=sa;Password=123456"
  3.     End Function
  4.  
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:
Expand|Select|Wrap|Line Numbers
  1. Imports System.Data.SqlClient
  2.  
  3. Public Class Form1
  4.     Inherits System.Windows.Forms.Form
  5.  
  6.     Dim CONNECTION_STRING As String = Connection()
  7.  
  8.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  9.         Dim Connection As New SqlClient.SqlConnection(CONNECTION_STRING)
  10.  
  11.         Dim ds As New DataSet
  12.  
  13.         Dim sql As String = "SELECT * FROM tblname"
  14.  
  15.         Dim da As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter(sql, Connection)
  16.  
  17.         Try
  18.             da.Fill(ds, "tblname")
  19.         Finally
  20.             da.Dispose()
  21.         End Try
  22.         Me.TextBox1.DataBindings.Add("Text", ds, "firstname")
  23.         Me.TextBox1.DataBindings.Add("Text", ds, "lastname")
  24.  
  25.     End Sub
  26.  
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.
Member
 
Join Date: Mar 2009
Location: Kathmandu
Posts: 42
#2: Mar 26 '09

re: Bind Textbox to Dataset


Me.TextBox1.DataBindings.Add("Text", ds.Tables("tblname"), "firstname")
Member
 
Join Date: Feb 2007
Posts: 75
#3: Mar 27 '09

re: Bind Textbox to Dataset


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:
Expand|Select|Wrap|Line Numbers
  1. TextBox1.Text = ds.Tables(0).Rows(index).Item("firstname").ToString
  2. TextBox2.Text = ds.Tables(0).Rows(index).Item("lastname").ToString
  3.  
In fact this way also works but i am not sure why is the difference.
Expand|Select|Wrap|Line Numbers
  1. TextBox1.DataBindings.Add("Text", ds.Tables(0), "firstname")
  2. TextBox2.DataBindings.Add("Text", ds.Tables(0), "lastname")
  3.  
Member
 
Join Date: Mar 2009
Location: Kathmandu
Posts: 42
#4: Mar 27 '09

re: Bind Textbox to Dataset


Excellant, you found the problem

ds.Tables("tblname") 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

http://software.techrepublic.com.com...5&promo=100511
Reply