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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Handle Null Values in DataRow

Status
Not open for further replies.

JPimp

Technical User
Mar 27, 2007
79
US
I am retrieving a value from an access table, and I get an error each time the value is null, how can I work around this so it continues to execute the code and populates the datagrid and leaves those null values blank in the datagrid?

Code:
Dim cmdSelectSWBLower = New OleDbCommand("Select * From PartSpecs Where [PART No_:] = '" & strPID & "' AND Limit = 'SWBLower'", conMetrics)

        Dim myDA5 As OleDbDataAdapter = New OleDbDataAdapter(cmdSelectSWBLower)

        Dim myDataSet5 = New DataSet

        myDA5.Fill(myDataSet5, "PartSpecs")

        Dim Row5 As DataRow

        If myDataSet5.Tables("PartSpecs").Rows.Count > 0 Then

            Row5 = myDataSet5.Tables("PartSpecs").Rows(0)

        End If



If Row5.Item("Value") Is DBNull.Value Then
            SWBLower.Text = String.Empty
        Else
            SWBLower.Text = Row5.Item("Value")
        End If

Thanks for any insight!

Kai-What?
 
1. do not use injected sql statements, use parameterized queries.
2 the error is occuring here most likely occuring here [tt]If Row5.Item("Value") Is DBNull.Value Then[/tt]
change [tt]DBNull.Value[/tt] to [tt]Nothing[/tt]. (I think, c# it's null)

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thanks jmeckley, yeah I am starting to conver to paramerized queries, never used before and I am just a rookie programmer, do you have a tip for a good resource on how to learn more about setting those up?

It fails on the Bind grid sub routine, and then later on the row where it is calling for a value for Row5.

Object reference not set to an instance of an object



Kai-What?
 
Comparing a value in VB to DBNull.Value is valid. However, since you are using an Access DB, it may not be valid. Does Access have a NULL datatype?

Also, provide specific lines of code where it fails.
 
Ahh, that might be the issue, the Access DB....

This is the line where it fails:

If Row5.Item("Value") Is DBNull.Value Then
SWBLower.Text = String.Empty
Else
SWBLower.Text = Row5.Item("Value")
End If



Kai-What?
 
jPimp, if your a novice then i would recommend taking a step back at OOP in general, not just ADO.Net parameterized queries.

Read up on Domain Driven Design (the book is invaluable). Also understand the acronym SOLID. this should be the core of any developer (although few .net devs have ever heard of them). You'll also want to refine your skills at identifying patterns (factory, strategy, adapter, visitor, command, etc.) none of this is .net specific, but if you understand this stuff then languages are just about syntax differences.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top