Drstein99 Posted October 14, 2003 Posted October 14, 2003 How do I deal with null values in tables? If i'm getting information from a table (all over the program) and in one record in one field might be a random DBNULL, whats the best way to cast this into a string? "Cast from type 'DBNull' to type 'String' is not valid." line of code: sTemp2 = esEmpTable.Rows(0).Item("middle name") Quote www.DRSTEIN99.com www.RAIDGEAR.net www.THERE.com -> Tell them DrStein99 sent ya!
*Experts* jfackler Posted October 14, 2003 *Experts* Posted October 14, 2003 (edited) If (esEmpTable.Rows(0).Item("middle name")GetType.ToString)<> "System.DBNull" Then sTemp2 = cstr(esEmpTable.Rows(0).Item("middle name")) Else: sTemp2 = "No value set" End If Edited October 14, 2003 by jfackler Quote
Drstein99 Posted October 14, 2003 Author Posted October 14, 2003 Yep. That fixed it. I added the code to my generic code to query data for the whole program. If it's a dbnull, then it makes it " ", so if it ever goes to write the data back then it won't be null and cause error. Quote www.DRSTEIN99.com www.RAIDGEAR.net www.THERE.com -> Tell them DrStein99 sent ya!
*Experts* Volte Posted October 14, 2003 *Experts* Posted October 14, 2003 A possibly more efficient way to do it (plus shorter) is like this:If esEmpTable.Rows(0).Item("middle name") <> Convert.DBNull Then sTemp2 = esEmpTable.Rows(0).ItemI would avoid string comparison wherever possible due to the terrible inefficiency and speed issues. Quote
Drstein99 Posted October 14, 2003 Author Posted October 14, 2003 That is shorter, and you definately showed me a nicer way of accomplishing what I need thank you. BTW - I'm just trying to replace the data with a space in event of null If esEmpTable.Rows(0).Item("middle name") = Convert.DBNull Then esEmpTable.Rows(0).Item("middle name") = " " Thank you very much. Quote www.DRSTEIN99.com www.RAIDGEAR.net www.THERE.com -> Tell them DrStein99 sent ya!
*Experts* Nerseus Posted October 15, 2003 *Experts* Posted October 15, 2003 Slightly off-topic: I've never seen Convert.DBNull - is that VB-specific, or just another way to get at DBNull? In C# it's System.DBNull.Value. I'm just wondering if it's like int and Int32 in C# or Integer and Int32 in VB.NET (the same object, just used two different ways - or 4 if you count different languages). -nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
*Experts* Volte Posted October 15, 2003 *Experts* Posted October 15, 2003 I'm using it in my C# app right now, so it's not VB specific. I think the idea is that it represents an object rather than a value, so it can be used as such. Quote
Jerod Posted October 15, 2003 Posted October 15, 2003 Here's the way I prefer to check this, for what its worth: <vb> if ISDBNull(esEmpTable.Rows(0).Item("middle name")) then sTemp2 = esEmpTable.Rows(0).Item </vb> Quote
Jerod Posted October 15, 2003 Posted October 15, 2003 Sorry, misinterpretation. Should be: if ISDBNull(esEmpTable.Rows(0).Item("middle name")) then sTemp2 = " " Quote
*Experts* Volte Posted October 15, 2003 *Experts* Posted October 15, 2003 Now that one is VB.NET specific. As a general rule, if you are using a standalone function (i.e. ThisFunction(params) rather than Class.ThisFunction(params)) that is built in to the framework, there is a good chance it is for VB6 compatability. With .NET, almost all functions are built into the classes they modify: stringobject.IndexOf() has replaced InStr(), for example. Nothing stands out on its own except the VB6 compatability library. I really hope Microsoft does away with it in the next .NET, though I really don't see it happening. Quote
Drstein99 Posted October 15, 2003 Author Posted October 15, 2003 I agree. I just started .net 2 months ago, from using VB6 and this new way of doing things has slowed me a down alot. But if microsoft does decide to change things I'm not sure I'm gonna be so quick to upgrade, as I get used to using one thing I'm forced to learn a new way with these upgrades. Quote www.DRSTEIN99.com www.RAIDGEAR.net www.THERE.com -> Tell them DrStein99 sent ya!
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.