Updating From Text Boxes

G

Guest

Hi, I apologize for a newbie question, but my books/research have yielded me
no examples and my time is now short.

I simply want to update data from text boxes (fields). I'm not using a
datagrid or datalist I found plenty of examples of how to do that and can get
that to work. I simply want to update my databound text boxes.
I'm using visual studios DataAdapters, DataSets and Dataviews. I can insert
to the database utilizing the sqladapter, but I can't figure out how to
update or delete using the records unique id / primary key.

Can someone please direct me to an example or show me sample code.
Preferrably in vb. I'm desperate. :)

Thanks,
enrique
 
B

Brock Allen

So in your button click event you're going to have to write the SQL to do
an update back to your database:

Dim cn as new SqlConnection() ' this assumes you're using SqlServer
cn.ConnectionString = "some connection string (depends upon your DB and security
settings)"
cn.Open()
Dim cmd as SqlCOmmand = cn.CreateCommand()
cmd.CommandText = "update YourTable set x=@x, y=@y where z=@z"
cmd.Parameters.Add("@x", txtBoxX.Text)
cmd.Parameters.Add("@y", txtBoxY.Text)
cmd.Parameters.Add("@z", txtBoxZ.Text)
cmd.ExecuteNonQuery()
cmd.Dispose() ' technically we need to call Dispose(), as SqlCommand implements
IDisposable
cn.Close() ' Don't forget to close the connection
 
G

Guest

Brock, Thank you very much.
Although I was looking for a solution that manipulates the disconnected data
and then updates via the sqlDataAdapter, this did the trick and I'm very
grateful.
I'll figure out updating dataTables and dataRows another day. (but if you
have the answer you can shoot it my way :) )

Again, Thanks!
-enrique
 
B

Brock Allen

How is what I suggested not disconnected data? SqlDataAdpater is just a layer
on SqlConnection and SqlCommand.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,149
Latest member
Vinay Kumar Nevatia0
Top