how to use datareader

Joined
May 11, 2005
Messages
5
Programming Experience
Beginner
i want to read data firstname and lastname from sql and display in a textbox

and after that i want to update that textbox how to do that my backend is sql. thanx
 
Last edited:
kulrom said:
update that textbox
could you explain this please? Maybe you want to update db if you change something within certain textbox?

Kind regards :)

btw, you said only SQL ... but didn't point out which DB type you are using there ... MS Access, MSSQL ....

my back end is sql
 
I assume you mean that your back end is SQL Server and you want to update the database if the value in the TextBox has changed. If these assumptions are true, I personally would recommend using an SqlDataAdapter to Fill a DataTable, to which you can bind your TextBoxes. When the Text in the databound Controls is updated, so too are the values in the DataTable. You can then use the SqlDataAdapter to Update the database from the DataTable.
 
Datareader

Dim cnPodaci As New SqlConnection
cnPodaci.ConnectionString = "Database Path"
cnPodaci.Open()
Dim cm As New SqlCommand
cm.CommandText = "SELECT * FROM TABLE"
cm.Connection = cnPodaci
Dim dr As SqlDataReader
dr = cm.ExecuteReader

If dr.HasRows Then

dr.Read()
me.txtBox.text = dr.Item("ColumnName")

dr.Close()

End If
cnPodaci.Close()

- to update that record you would have to use direct UPDATE command.
UPDATE Table SET ....
... parameters ...
executenonquery
 
Dim cnPodaci As New SqlConnection
cnPodaci.ConnectionString = "Database Path"
cnPodaci.Open()
Dim cm As New SqlCommand
cm.CommandText = "SELECT * FROM TABLE"
cm.Connection = cnPodaci
Dim dr As SqlDataReader
dr = cm.ExecuteReader

If dr.HasRows Then

dr.Read()
me.txtBox.text = dr.Item("ColumnName")

dr.Close()

End If
cnPodaci.Close()

- to update that record you would have to use direct UPDATE command.
UPDATE Table SET ....
... parameters ...
executenonquery

but when i give the code it is telling syntax error.
what is the problem?
 
tharshi, please click on the link DW2 in my signature, and read the section "Creating a Simple Data Application"
 
Back
Top