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!

Extract value from Database and determine if exists or not.

Status
Not open for further replies.

JPimp

Technical User
Mar 27, 2007
79
0
0
US
I am trying to extract a field value from a database table, and if the value is Null, I want to redirect the user to another webpage, but it comes up with the object not found error on the line where it checks if the value is null or not.

Code:
Dim CmdSelectVerify As OleDbCommand

        CmdSelectVerify = New OleDbCommand("Select * From PartSpecs WHERE [Part No_:] = '" & strPID & "'", conMetrics)

        Dim myDAVerify As OleDbDataAdapter = New OleDbDataAdapter(CmdSelectVerify)

        Dim myDataSetVerify = New DataSet

        myDAVerify.Fill(myDataSetVerify, "PartSpecs")

        Dim RowVerify As DataRow

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

            RowVerify = myDataSetVerify.Tables("PartSpecs").Rows(0)

        End If

        If RowVerify.Item("Part NO_:") Is DBNull.Value Then

            Response.Redirect("[URL unfurl="true"]http://....")[/URL]
        Else

        End If



Kai-What?
 
did you verify that the table "PartsSpecs" actually contains a row?
 
1. use parameters not injected sql
2. use a scalar query, not a reader
3. this has nothing to do with asp.net, this is core .net funtionality.
4. you will never return a row, because you are fetching the row by the id. if the id doesn't exist then no rows are returned. if there is a row, then the item exists.
5. if you have any control over the database remove the non-alpha characters. it makes querying the db much easier.

use a query like this
Code:
select [specific field] from PartSpecs WHERE [Part No_:] = @partNumber
then use the scalar query to get the value
Code:
object result = command.ExecuteScalar();
if(result == DbNull.Value)
{
   //do something without value
}
else
{
   string value = (string)result;
   //do something with value;
}

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

Part and Inventory Search

Sponsor

Back
Top