Adding a row with a Date and Number Type

Rogue 1

Member
Joined
Oct 5, 2006
Messages
12
Programming Experience
Beginner
Need some help with adding a row to a data table with a data type of date and number type. All the examples I find are for fields that are strings. Here is the code:

VB.NET:
drNew.Item("Unique Key") = "Unique Key" 'number type, does not work
drNew.Item("Player") = "Player" 'string type, works
drNew.Item("Picked") = "Picked" 'date type, does not work

This is part of the error I get for the date:

VB.NET:
An unhandled exception of type 'System.ArgumentException' occurred in system.data.dll
Additional information: System.FormatException: The string was not recognized as a valid DateTime.  There is a unknown word starting at index 0.

I am sure this is rather simple but I can't locate any examples of adding new rows with one of these types. Thanks for any help in advance.
 
The trouble I am having is understanding the two parts of the of the code (highlighted in bold)

VB.NET:
drNew.Item("[B]Picked[/B]") = "[B]Picked[/B]"

The first "Picked" in this case appears to be the name of the field in the table. The second "Picked" is the value is being attemped to be inserted into a bound text box.

My goal here was after the user clicked a button, a new record is created with default values of null. The user keys data into the field, clicks another button and the data saves. Thanks again
 
drNew can be thought of as a collection

drNew.Item(<indexer>) can be thought of as a value in that collection

values are typed

if drNew.Item("Picked") is specified to be a Date Type then you must put a date into it, not a string.
If the datarow is typed then you should really use the typed form.


i.e. you have a typed datatable called MyDataTable. When you add a row, dont do this:

Dim drNew as DataRow = MyDataTable.NewMyDataTableRow()


Do this:

Dim mdtdrNew as MyDataTableDataRow = MyDataTable.NewMyDataTableRow()



Now you have a typed object, and you can say:

mdtdrNew.Picked = 'something

rather than:

drNew.Item("Picked") = 'something


Its the same end, but one is more type safe. Also turn Option Strict ON in your project


But the answer to your problem lies in your attempt to put a square peg ina round hole. presumably you can see that this statement is flawed:

Dim x as Date
x = "Picked"


So you can similarly accept that if either of these:
mdtdrNew.Picked
drNew.Item("Picked")

are dates, then trying to shove a string into them wont work


what you need is:
mdtdrNew.Picked = DateTime.Now 'a DATE object!!!
drNew.Item("Picked") = DateTime.Now
 
Back
Top