How to insert new row in db

tmachine

New member
Joined
Jun 10, 2007
Messages
4
Programming Experience
Beginner
I have read many tutorials but I can't get a grasp of adding a new row to the database or even delete.

The closest example I came across is this:

VB.NET:
' Create a new row.
Dim newRegionRow As NorthwindDataSet.RegionRow
newRegionRow = Me.NorthwindDataSet._Region.NewRegionRow()
newRegionRow.RegionID = 5
newRegionRow.RegionDescription = "NorthWestern"

' Add the row to the Region table
Me.NorthwindDataSet._Region.Rows.Add(newRegionRow)

' Save the new row to the database
Me.RegionTableAdapter.Update(Me.NorthwindDataSet._Region)

I have also read this post http://www.vbdotnetforums.com/showthread.php?t=17004 which may work but they only gave an example of adding one column.

I would like the new row to be added after clicking a button. I know how to add the codes to the button but getting the row created is where i get lost. I tried changing values in the above examples but nothing happened.

Things I have created...
I am using a sql file for database (.mdf)
MyDataset
MyTableAdapter
My_Table [id, title, description, ratings] -(I should mention that "id" is autoincrement)

Could someone write an example of how I would add a new row to the database?
Thanks in advance :)
 
Read the DW2 link in my signature, start with "Creating a Simple Data App" and then read other sections as appropriate
 
Click the DNU link in my signature. Sorry I missed that point in your message - usually I point people to DNU when they make it more obvious that the save is working but they cannot see the changes.. You post didnt quite lead me to realise this was happening, sorry..
 
Thanks for the reply :) Actually I made a major rookie mistake, i thought the data would be shown inside the database so i did not create a listbox to immediately retrieve the new addition so basically I had no clue there was even records being saved :eek:

Thanks again for the prompt reply.

Another question if you don't mind answering. Is stored procedures the same as in the link http://vbdotnetforums.com/showthread.php?p=56447 ? If not, what is the difference and why I should choose one over the other. I find stored procedures very easy to work with.
 
Yep, storedprocedures are the same as parameterized queries, except for a small setup difference:

command.CommandText = "SELECT * FROM blah WHERE @param"
command.CommandType = Text
command.Parameters.Add(...)

command.CommandText = "SelectAllFromBlahProc(@param)"
command.CommandType = StoredProcedure
command.Parameters.Add(...)


But do note that the dataset designer can attach to and use sprocs so dont bother writing all this by hand. Read the DW2 link about SProcs
 
Back
Top