Datarow datatype defining?

A

Ahmed

All of a sudden a bug crept up in my program where i am inserting something
in a newly created datarow.
Here is a little sample:

drCurrent = tblCalls.NewRow() 'Now this is how i define a new datarow
(drcurrent is a datarow datarow)
drCurrent.Item(0) = timeStart
drCurrent.Item(1) = timeEnd <- Exception thrown here! as the following

"An unhandled exception of type 'System.ArgumentException' occurred in
system.data.dll
Additional information: System.InvalidCastException: Invalid cast from
DateTime to Int32.
at System.DateTime.System.IConvertible.ToInt32(IFormatProvider provider)
at System.Convert.ToInt32(Object value)
at System.Data.Common.Int32Storage.Set(Int32 record, Object value)
at System.Data.DataColumn.set_Item(Int32 record, Object value)Couldn't
store <5/20/2004 1:10:25 AM> in Call_Id Column. Expected type is Int32."

Now both timeStart and timeEnd are of DateTime data type....this actually
worked perfectly and all of a sudden i am getting exceptions and i am not
quite sure why?

Can anyone help me out here?

TIA,
Ahmed
 
J

Jon Skeet [C# MVP]

Ahmed said:
All of a sudden a bug crept up in my program where i am inserting something
in a newly created datarow.
Here is a little sample:

drCurrent = tblCalls.NewRow() 'Now this is how i define a new datarow
(drcurrent is a datarow datarow)
drCurrent.Item(0) = timeStart
drCurrent.Item(1) = timeEnd <- Exception thrown here! as the following

"An unhandled exception of type 'System.ArgumentException' occurred in
system.data.dll
Additional information: System.InvalidCastException: Invalid cast from
DateTime to Int32.
at System.DateTime.System.IConvertible.ToInt32(IFormatProvider provider)
at System.Convert.ToInt32(Object value)
at System.Data.Common.Int32Storage.Set(Int32 record, Object value)
at System.Data.DataColumn.set_Item(Int32 record, Object value)Couldn't
store <5/20/2004 1:10:25 AM> in Call_Id Column. Expected type is Int32."

Now both timeStart and timeEnd are of DateTime data type....this actually
worked perfectly and all of a sudden i am getting exceptions and i am not
quite sure why?

Can anyone help me out here?

Well clearly the datarow thinks that the column 1 is meant to be an
int, not a DateTime. How are you creating the DataTable to start with?
 
A

Ahmed

Well drCurrent is a public variable for that form.
I create a new instance of drCurrent with the following line

drCurrent = tblCalls.NewRow()

Now tblCalls is populated by a dataset @ the start of the program

tblCalls = myDataSet.Tables("Call_Times")

The thing is there are already values in the table of type DateTime, that's
why I do not understand why it is not accepting it.
Is there anyway of creating the datarow while defining what datatypes will
be accepted?

Ahmed
 
J

Jon Skeet [C# MVP]

Ahmed said:
Well drCurrent is a public variable for that form.
I create a new instance of drCurrent with the following line

drCurrent = tblCalls.NewRow()

Now tblCalls is populated by a dataset @ the start of the program

tblCalls = myDataSet.Tables("Call_Times")

The thing is there are already values in the table of type DateTime, that's
why I do not understand why it is not accepting it.
Is there anyway of creating the datarow while defining what datatypes will
be accepted?

I suspect you don't have any DateTimes in that *column* of the table.

How has that table been populated in the dataset, exactly? What does
tblCalls.Columns[1].DataType give you?
 
A

Ahmed

When i do the call on tblCalls for the data type on column 1 i get
"System.DateTime"
i populate the dataset with this:

Try
myDataAdapter.Fill(myDataSet, "Call_Times")
Catch
End Try

myDataView = New DataView(myDataSet.Tables("Call_Times"))
tblCalls = myDataSet.Tables("Call_Times")

Thats quite odd that i get a datetime datatype BUT when i populate drcurrent
it rejects it :)

Ahmed

Jon Skeet said:
Ahmed said:
Well drCurrent is a public variable for that form.
I create a new instance of drCurrent with the following line

drCurrent = tblCalls.NewRow()

Now tblCalls is populated by a dataset @ the start of the program

tblCalls = myDataSet.Tables("Call_Times")

The thing is there are already values in the table of type DateTime, that's
why I do not understand why it is not accepting it.
Is there anyway of creating the datarow while defining what datatypes will
be accepted?

I suspect you don't have any DateTimes in that *column* of the table.

How has that table been populated in the dataset, exactly? What does
tblCalls.Columns[1].DataType give you?
 
J

Jon Skeet [C# MVP]

Ahmed said:
When i do the call on tblCalls for the data type on column 1 i get
"System.DateTime"
i populate the dataset with this:

Try
myDataAdapter.Fill(myDataSet, "Call_Times")
Catch
End Try

myDataView = New DataView(myDataSet.Tables("Call_Times"))
tblCalls = myDataSet.Tables("Call_Times")

Thats quite odd that i get a datetime datatype BUT when i populate drcurrent
it rejects it :)

That would seem very odd indeed.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
A

Ahmed

Thank you for the info Jon....i looked over my code and i was creating
datasets and dataviews....it was quite confusing so i just removed them and
created a datatable which fixed the problem!

Thanks Jon!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top