Adding row to database

delcolt

New member
Joined
Aug 15, 2010
Messages
1
Programming Experience
1-3
I have recently begun learning about databases in visual studio and have since learned how to create an SQL database, create a dataset out of that database, add and delete entries. Pretty much all the basic stuff.

However, I've stumbled onto a problem. It is possible for me to manually add an entry to the dataset and database by dragging the dataset onto a form and then manually adding the data. The problem is that I don't want to enter the data manually, I want to do this via vb.net code. I cannot find any information on how to do this with an SQL database. A basic example of what I want to do is when you press on a command button on a form, it will automatically add a new row to an existing database with certain data in it.

I know this has to be possible, does anyone know how?
Oh and I have no experience with SQL
 
Add a DataSet to your form. In code, add a new row to the desired DataTable, e.g.
VB.NET:
Dim row = myDataSet.SomeTable.NewSomeRow()

row.SomeColumn = someValue
myDataSet.SomeTable.Add(row)
You then save the changes using a TableAdapter, exactly as you would if the user added the new row using a DataGridView.

If you're only saving one row at a time then you have another option. You can enable DBDirect methods on your TableAdapter, which you do in the DataSet designer, and then you will get a new Insert method on your TableAdapter, so you can call that and simply pass the appropriate field values. That will save the row to the database without a local DataSet.
 
Back
Top