PDA

View Full Version : Compare QueryString to AccessDatasource Column Value


s.rye
07-21-2009, 04:39 PM
Hi,

When my page loads I want to compare the value of a QueryString against the data in a column from an Access Database. I then want to redirect the user to another page if the value of the QueryString does not exist within the column.

I thought something like this....


Protected Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
If Request.QueryString("ProdID") <> ACCESSDATABASECOLUMN
Then
Response.Redirect("page1.aspx")
Else
DOSOMETHINGELSE
End Sub

But I do not know what code I should use above where I have "ACCESSDATABASECOLUMN" to pull in the information from the database.

Can anyone help with this please? Maybe there is a completely different way that I can try that will work?

Thanks

Steve

Mike_O
07-23-2009, 06:52 PM
Hey s.rye,

Here's a link to start:
http://www.w3schools.com/aspnet/aspnet_dbconnection.asp

Sounds like object DataReader is what you need to concentrate on.

Regards,
Mike

Old Pedant
07-24-2009, 01:35 AM
So all you really want to do is find out if a given product ID exists in a given column in a particular table in an Access database, right?

You don't even need a DataReader, then.

This is *POSSIBLE* code. Other ways to do this. But this is pretty minimalist.

This *assumes* that the ProdID in the DB is an integer.


Protected Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Dim pid As Integer
Try
pid = CINT( Request.QueryString("ProdID") )
Catch e As Exception
pid = -1
End Try
' if the value from the query string is not an integer, then of course
' it won't be found in the database...so no point in going further:
If pid < 0 Then Response.Redirect("page1.asp")

Dim count As Integer = 0 ' this will count how many matches on prodid there are

' okay, let's look in the DB...
Dim connstr As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Server.MapPath("./path/to/yourdb.mdb") & ";"
Using conn As New OleDbConnection(connstr)
Dim sql As String = "SELECT COUNT(*) FROM nameoftable WHERE nameofcolumn = " & pid
Dim cmd As New OleDbCommand(sql, conn)
conn.Open( )
count = CINT( cmd.ExecuteScalar() )
End Using
If count = 0 Then Response.Redirect("page1.aspx")

' DOSOMETHINGELSE

End Sub

The advantage to using COUNT(*) is that we will *always* get one and only one record from the query and by selecting *only* that value we can then just ask for ExecuteScalar() to return the one and only one value. Presto.