Databind to a text box

jamie_pattison

Well-known member
Joined
Sep 9, 2008
Messages
116
Programming Experience
Beginner
I have a Textbox which i would like to bind to my database(Sql) which in turn runs a Stored Procedure.

Ive tried to bind the one row that is returned using this code

TextBox1.DataBindings.Add("Text", DataSet1, "tableName.columnName")

This doesnt work, so im lost as to where i should start with this. Im using a dataset, which does bring back values. I think i could use a tableadaptor but again not sure if i really need this (hopefully not for this task)

Could anyone advise please?

Thanks
 
You can use datareader
VB.NET:
dim cmd as oledcommand=new oledbcommand("querystring","connectionstring")

dim dr as oledbdatareader=cmd.executedatareader
textbox1.text=dr.item(0)
dr.close
connection.close
 
Last edited:
Below is my code if i have anything wrong here

VB.NET:
Dim sqlConn As SqlConnection
Dim connectionString As String = "server=server; database=testdb; integrated security=true; "
Dim da As SqlDataAdapter
Dim ds As New DataSet

sqlConn = New SqlConnection(connectionString)

da = New SqlDataAdapter("Some_Stored_Procedure", sqlConn)
da.Fill(ds)

sqlConn.Open()
Me.TextBox2.DataBindings.Add("text", ds.Tables("TableName"), "ColumnName")
sqlConn.Close()

Count brings back one table (if i type ds.tables.count)

The error i recieve is "Value cannot be null. Parameter name: dataSource"

Thanks guys
 
The problem is that you aren't giving your DataTable a name when you create it. If you expect to be able to retrieve the DataTable by name then you have to give it a name. If you don't then you can only retrieve it by numeric index. You need to either change your code to this:
VB.NET:
da.Fill(ds)

sqlConn.Open()
Me.TextBox2.DataBindings.Add("text", ds.Tables([B][U]0[/U][/B]), "ColumnName")
or this:
VB.NET:
da.Fill(ds, [B][U]"TableName"[/U][/B])

sqlConn.Open()
Me.TextBox2.DataBindings.Add("text", ds.Tables("TableName"), "ColumnName")
Now, see how easy that was when we have all the relevant information? It's not always easy to determine what's relevant and what's not but error messages are ALWAYS relevant.
 
Reading your post. I tried to insert value in access table.My data is getting saved in databse but i m also geeting a error message. "Value cannot be null. Parameter name: dataSource"

VB.NET:
'query string
            q = "insert into eng values('" & TextBox1.Text & "','" & TextBox2.Text & "')"
            'connection string
            cn = New OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=C:\Documents and Settings\Admin\Desktop\data.mdb;")
            cn.Open()
            ds = New DataSet
            'creating connection with database & table
            ad = New OleDbDataAdapter(q, cn)
            'filling dataset
            ad.Fill(ds, "eng")
            'adding data to table
            Me.TextBox1.DataBindings.Add("text", ds.Tables("eng"), "eno")
            Me.TextBox2.DataBindings.Add("text", ds.Tables("eng"), "ename")
            cn.Close()
            MessageBox.Show("Data Saved")
 
What exactly are you trying to achieve there. You're calling Fill on a DataAdapter, which is supposed to be for executing a SELECT statement and populating a DataTable with the result set. Your SQL statement is an INSERT statement though. It gets executed and the data is inserted but the statement doesn't create a result set so no DataTable is created in your DataSet. You're then trying to bind your TextBoxes to that non-existent DataTable.

If you want to bind data then you need to retrieve it first, which requires a SELECT statement. You create your DataAdapter with that SELECT statement and then call Fill to populate a DataTable, which you can then bind to your controls. If you want to insert new data then you can add rows to that DataTable and then call Update on the DataAdapter, making sure you've supplied an INSERT statement. If you want to insert a single record into the database without retrieving any data first then you should be creating a Command and calling ExecuteNonQuery.

I suggest that you do a member search and find cjard, then follow some of the data links in his signature to see how to properly use ADO.NET.
 
Back
Top