Typed Dataset Insert problem

Funger

Member
Joined
Dec 15, 2006
Messages
8
Programming Experience
5-10
Hi All,

I am attempting to use my first Typed Dataset. I admit that it simplifies the data access code of my application, and appears to be nice so far. I can not, however, figure out how to get it to insert new records back into my database.

My Dataset is called AppData. In it I've got one DataTable called Genre which relates back to a table in my database with the same name.

The Genre DataTable has only the default query built for it (Fill, and GetData.) Also, the GenerateDBDirectMethods property is set to true, and the SQL statements that were generated are valid.

I am trying to write the code that inserts a single new record into the Genre table. Here's what I wrote:

VB.NET:
Dim ta As New AppDataTableAdapters.GenreTableAdapter

ta.Insert(1234, "New Genre", "Description of new Genre")

ta = Nothing

When I execute the code, it doesn't generate any errors, but when I look in the database table, the record was not inserted. Can someone please explain why this is, and how to accomplish what I am attempting? Thanks!

:D
-Funger
 
You can use this if you want:

VB.NET:
Public Function NewRecord(ByVal dataset As DataSet) As DataSet
        Dim row As DataRow
        row = dataset.Tables(0).NewRow
        dataset.Tables(0).Rows.Add(row)
        Return dataset
End Function

Hope this one helps.

Jah Bless!!!:D
 
You can use this if you want:

No, no, no! The OP is using TYPED datasets. Please, do NOT advise them ever to use ANY line of code containing .Tables( when working with TYPED datasets!

Also, providing a function called NewRecord and providing a dataset as parameter/return is a very poor idea because it blurs the critical distinction that datasets do not contain records. Datasets are a collection of DataTables, which hold records.

Please do update your knowledge of datasets, and dont advise newbies on .NET 2.0 to use their datasets in a generic ds.Tables(xyz) way; it's the old way and completley defeats the point of type safety introduced by typed datasets :(
 
Last edited:
Hi All,

I am attempting to use my first Typed Dataset. I admit that it simplifies the data access code of my application, and appears to be nice so far. I can not, however, figure out how to get it to insert new records back into my database.

My Dataset is called AppData. In it I've got one DataTable called Genre which relates back to a table in my database with the same name.

The Genre DataTable has only the default query built for it (Fill, and GetData.) Also, the GenerateDBDirectMethods property is set to true, and the SQL statements that were generated are valid.
An INSERT statement will always be generated in the tableadapter, unless the table is readonly, or based on a view that is not updateable. UPDATE and DELETE statements can only be generated if the table has a PK.

GenerateDBDirect merely provides you with the ability to directly update/ insert or delete one row from the database, using the tableadapter, but no datatable. We dont generally use them. Of far more importance is the option to generate updating commands as well..

Suffice to say you wont be using the DB Direct methods for this operation

I am trying to write the code that inserts a single new record into the Genre table. Here's what I wrote:

VB.NET:
Dim ta As New AppDataTableAdapters.GenreTableAdapter

ta.Insert(1234, "New Genre", "Description of new Genre")

ta = Nothing

When I execute the code, it doesn't generate any errors, but when I look in the database table, the record was not inserted. Can someone please explain why this is, and how to accomplish what I am attempting? Thanks!
OK, youre not using a datatable here at all. Youre simply using a TableAdapter that is correctly inserting a new record directly. If you were looking to e.g. let the user click a button to add a new row to the database, but let them edit the data first you would:

Have a dataset, containing a datatable, a bindingsource bound to that table, some controls bound to that binding source. In adding the row you would most easily call:

myBindingSource.AddNew()

but you can also, if youre setting values in code:
appDatasetInstance.Genre.AddGenreRow(1, "rock n roll", "guitars and drums")

or:
Dim tempRow as MyDataset.MyTypedRow = appDataSetInstance.Genre.NewGenreRow()
tempRow.ID = 1
tempRow.GenreName = "rock n roll"
tempRow.GenreDesc = "guitars and drums"
appDataSetInstance.Genre.AddRow(tempRow)



you DO NOT DO THIS to gather user input:

but you can also, if youre setting values in code:
appDatasetInstance.Genre.AddGenreRow(idTextBox.Text, nameTextBox.Text, descTextBox.Text)

neither do you do this:

Dim tempRow as MyDataset.MyTypedRow = appDataSetInstance.Genre.NewGenreRow()
tempRow.ID = idTextBox.Text
descTextBox.TexttempRow.GenreName = nameTextBox.Text
tempRow.GenreDesc = descTextBox.Text
appDataSetInstance.Genre.AddRow(tempRow)


use databinding, mmkay?
-

Now, i mentioned that your DB is updating correctly, but you dont see that. Read the DNU link in my signature to find out why. To continue on your journey of learning .NET 2 data access properly, read the DW2 link in my sig, starting with "creating a simple data app" and moving on from there.
 
So, I'm kinda dumb

Ok.... The problem wasn't with the Typed Dataset. This is also the first time that I have tried to incorporate a SQL Server Express database into an application.

When I inserted data into my database, I expected the data to go into the database listed in my Server Explorer window. I didn't realize that a copy of the database is made in the bin folder of the project and used during runtime.

Thanks for taking the time to look at my post!

-Funger
 
Back
Top