Adding to a table

o-mheien@hdsoftware.no

Active member
Joined
Feb 6, 2007
Messages
27
Programming Experience
10+
Hi there
In my application I have declared a tableadapter and a datatable:
VB.NET:
MyTableTA as new DatasetTableAdapters.MyTableTableAdapter
MyTableDT as new DatasetTableDatasets.MyTableDataset
then in the code I want to add and edit a record and I do like this:
VB.NET:
dim NewID as GUID.NewGuid.ToString
MyTableTA.InsertUsingID(MyTableDT,NewID)
MyTableTA.fill(MyTable.DT)
dim CurrentRecord as MyTableDT.GetByID(NewID)
dim EditForm as new frmMyTable(CurrentRecord)
if EditForm.ShowDialog() = DialogResult.OK
  MyTableTA.Update(CurrentRecord)
End If
InsertUsingID is a method created using the built in Dataset designer.

Would you say this is the correct way to do this? Is there a bether way doing this?

Any comments would be helpfull

Thanx
Ole
 
do you have to do it in code?

Why not use the GUI produced TableAdapter? It gives the correct code, you can add queries to it easily and you can explore the code to see exactly what it has added.

Personally I see no reason why no one should not use the built in wizards, at the end of the day, that is what they are there for!!
 
Well, the situation is that I have a Class handmade called FileManager. This class handles all the file handling. Therfore there are no GUI involved at all.
 
How does database access relate to file handling?
How do datasets relate to GUIs?

Hmm.. just because you design a dataset visually doesnt mean you have to have a GUI to use it!

Can you do me (and yourself, ultimately) a favour, and get really solid in your mind, on the difference between a DataSet and a DataTable?

To see your code:
MyTableDT as new DatasetTableDatasets.MyTableDataset

It might as well be:

Chalk as new Cheese


Guidelines recommend that you do not start variable names with a capital letter. Initial caps are the convention for Method names and Class names. Following this guide enables you to determine which of these is a static method call, static property access, instance method call and instance property access:

Obj.Noun
Obj.Verb()
obj.Noun
obj.Verb()


hence you really should be doing:

carsDT as New ForecourtDataSet.CarsDataTable

not:

carsDT As New DataSetTableSetData.MyDataTableTableDataSet



DataSet = collection of DataTable
DataTable = collection of DataRow
DataRow = collection of (data items)

Dont call something a set, when it is a table, and vice versa
 
Personally I see no reason why no one should not use the built in wizards, at the end of the day, that is what they are there for!!
Indeed, especially since, in 2.0, it's not a "wizard" per se, but a full fledged visual-component-that-writes-good-code design tool, just like the Form designer, that people use every day.. To snub it, and then use the Form designer is something of a hypocrisy, I reckon..
 
How does database access relate to file handling?
How do datasets relate to GUIs?
I didnt mention the GUI. Arg81 did

Hmm.. just because you design a dataset visually doesnt mean you have to have a GUI to use it!
of course not, and that was exactly my point in my reply to Arg81

Can you do me (and yourself, ultimately) a favour, and get really solid in your mind, on the difference between a DataSet and a DataTable?
I donbt think I have any problems regarding the difference betwee, DT, DS and DR
To see your code:
MyTableDT as new DatasetTableDatasets.MyTableDataset

It might as well be:

Chalk as new Cheese
Well, this is mostly not a problem since the intellisence takes care of this. But I see your point.
Guidelines recommend that you do not start variable names with a capital letter. Initial caps are the convention for Method names and Class names. Following this guide enables you to determine which of these is a static method call, static property access, instance method call and instance property access:

Obj.Noun
Obj.Verb()
obj.Noun
obj.Verb()

hence you really should be doing:

carsDT as New ForecourtDataSet.CarsDataTable

not:

carsDT As New DataSetTableSetData.MyDataTableTableDataSet
As mentioned, the only thing I can do is make sure the variable itselfe is typed in a special way. The rest of the line is taken care of by the intelicense. And the names of the classes was just default values presented by the wizzy - This was just a test app that I worked with to find out how to do things

DataSet = collection of DataTable
DataTable = collection of DataRow
DataRow = collection of (data items)

Dont call something a set, when it is a table, and vice versa
I dont think I do ;-) Or did I really?

MyTableTA as new DatasetTableAdapters.MyTableTableAdapter
MyTableDT as new DatasetTableDatasets.MyTableDataset

These two lines is of couorse a result of a typo. The code was just added by hand and not by cut and paste. It should have been:

MyTableTA as new DatasetTableAdapters.MyTableTableAdapter
and
MyTableDT as new DatasetDataSet.MyTableDataTable

Anyway - thanx for the input.

Regards
Ole
 
Arg81 mentioned GUI?

You wrote:
Well, the situation is that I have a Class handmade called FileManager. This class handles all the file handling. Therfore there are no GUI involved at all.

This post implied, to me, that you thought DataSet designer could only be used in Windows Forms apps..

These two lines is of couorse a result of a typo. The code was just added by hand and not by cut and paste. It should have been:

MyTableTA as new DatasetTableAdapters.MyTableTableAdapter
and
MyTableDT as new DatasetDataSet.MyTableDataTable
That makes much more sense!

Now, to your original problem (Now I know what youre using is a dataset, not a table).. Incidentally, it's still confusing when you write MyTableDT, but this is clearly an instance of a dataset, because you Fill() MyTableDT.DT, the bold DT being the DataTable, The MyDataTable being the DataSet

As noted before. If something is a spade, call it a spade. If that means pasting your code verbatim, rather than hand-writing a bug-ridden example, then please paste working code. Dont ask others to guess what youre doing, as a result of posting buggy/untested code or code that I wont be able to see does what youre claiming it does..

Looking at the code itself.. Erm, Generally, not the way I would expect it to be done.. The process you have is:
VB.NET:
dim NewID as GUID.NewGuid.ToString
MyTableTA.InsertUsingID(MyTableDT,NewID)
MyTableTA.fill(MyTable.DT)
dim CurrentRecord as MyTableDT.GetByID(NewID)
dim EditForm as new frmMyTable(CurrentRecord)
if EditForm.ShowDialog() = DialogResult.OK
  MyTableTA.Update(CurrentRecord)
End If

To my mind this does:

Make new GUID
Insert and write new record into database table with GUID
Download entire table from database into client
Find the record you just downloaded, on the client side
Open a new form to edit the record
Have the tableadapter send the change back to the database


Seen described, does that seem sensible? What if, one day, your table contains more than a million rows?


Ordinarily we would have:

A datatable
A bindingsource attached to that datatable
Form controls, bound to elements of the bindingsource

we would say:
myDataTable.SomeIDColumn.DefaultValue = myGUID
BindingSource.AddNew
Edit it
Call myTableAdapter.Update



See the DW2 link in my signature...
 
Back
Top