Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems entering value into a DataTable

Status
Not open for further replies.

WallyAnchor

Programmer
May 17, 2008
12
NZ
Hi.

I'm having troubles understanding what the problem is with this. In my application I'm trying to parse & insert a (valid) string into a column/row in a datatable.

Here's a boiled down example:

Dim dt As DataTable = New DataTable("dt")
dt.Columns.Add("dbl", Type.GetType("System.Double"))
Dim r As DataRow = dt.NewRow

Decimal.TryParse("4", r("dbl")) '<---- gives error "Conversion from type 'DBNull' to type 'Decimal' is not valid."


If I 'initialise' r("dbl") = 0 or something first, it appears to work here, although that causes other issues in my actual application. And DBNull could be valid anyway, if I'm not populating that column for a particular row.

So I'm very confused as to what the problem is here? Can somebody please point out what's wrong with my example?

Cheers
Wally
 

This line:

Dim r As DataRow = dt.NewRow

creates a new datarow, with nulls for all the values. So when you do the decimal.TryParse you get the BDNull error. As you stated, you have to set the value of the field first. Also, to actually add the row to the datatable you need to do this:

dt.rows.Add(r)


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Hi.
Thanks for your reply.

Yes, the new datarow will have nulls for all the values.

Decimal.TryParse("4", r("dbl"))

I'm trying to populate r("dbl") with the value 4. Surely it shouldn't matter whether r("dbl") is null or not...?


Cheers
Wally
 

You need to use an actual decimal variable in the second parameter of the TryParse method.


Dim n As Decimal

If Decimal.TryParse("4", n) Then
r("dbl") = n
Else
MsgBox("Number cannot be parsed.")
End If



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Hi jebenson

Thanks. I tried that and while it works, the next line of code executed is a 'random' (not random, but I can't figure out the logic) line of code buried in an If block later in the code, missing out a chunk. I can't figure out why.

If I put it in a try/catch block it throws a null exception, which doesn't help me at all.

Bizarre.

I think I'll have to start again from scratch with this block, and attack it differently.

Thanks for your help

Wally
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top