Wrox Programmer Forums
|
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 4th, 2006, 05:12 PM
Authorized User
 
Join Date: Feb 2006
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default datareader trouble

Hi all,

I'm trying to create a datareader that will bring back specific information based on the querystring that is passed on the page. however i am having trouble adding the querystring as a parameter in the datareader, it seems it is easy to add it if it is in a textbox but not a querystring.

Here is the code, i want it to bring back results based on the page it is on, eg details.aspx?cust_ID=1

Dim db As SqlConnection
Dim selectcustomer As SqlCommand
Dim dtrcustomers As SqlDataReader

db = New SqlConnection("connectionstring")
db.Open()
selectcustomer = New SqlCommand("Select * from customers Where cust_ID = '@cust_ID'", db)
selectcustomer.Parameters.AddWithValue("@cust_ID", cust_ID)
    dtrcustomers = selectcustomer.ExecuteReader()
        If dtrcustomers.HasRows Then
            While dtrcustomers.Read()
                Response.Write(CStr(dtrcustomers.Item("cust_order" )))
                Response.Write("<BR>")
            End While
        Else
            Response.Write("There are no customers")
        End If
        dtrcustomers.Close()
        db.Close()

 
Old April 4th, 2006, 07:47 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default

I'm not too sure about adding parameters to text queries in that manner.
Try: ("Select * from customers Where cust_ID = '" + cust_ID + "'",db)
Adding Parameters in the manner you have is for Stored Proceedures, but I may be wrong, never tried or seen it like that before.
But also, If the cust_ID is a integer then the single quotes are not needed/permitted.
So it will be: ("Select * from customers Where cust_ID = " + cust_ID ,db)



======================================
They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================
 
Old April 5th, 2006, 06:22 PM
Authorized User
 
Join Date: Feb 2006
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

Many thanks for your reply. I'm using an example from a book and it made no mention of a stored procedure. So maybe it is not possible to add a querystring parameter as a parameter in a datareader. I just thought it was because you can have that kind of parameter if you use the sqldatasource.

Is there another way I can get at the information based on the cust_ID the page is on and output as a string? I'm new to asp.net 2.0 and i'm finding loads of neat controls and features i never knew existed!

Thanks

Michelle

 
Old April 6th, 2006, 02:47 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default

The parameter does not effect the reader as such, but rather the query.
If it is in a book I am probably wrong about the parameter, It just didn't look right to me.
Your code looks ok aside from that to me, try this:
Code:
Dim db As SqlConnection
Dim selectcustomer As SqlCommand
Dim dtrcustomers As SqlDataReader

db = New SqlConnection("connectionstring")
db.Open()
selectcustomer = New SqlCommand("Select * from customers Where cust_ID = '" & cust_ID & "'", db)
    dtrcustomers = selectcustomer.ExecuteReader()
        If dtrcustomers.HasRows Then
            While dtrcustomers.Read()
                Response.Write(CStr(dtrcustomers.Item("cust_order")))
                Response.Write("<BR>")
            End While
        Else
            Response.Write("There are no customers")
        End If
        dtrcustomers.Close()
        db.Close()
and if that doesn't work try without the single quotes '.
Code:
selectcustomer = New SqlCommand("Select * from customers Where cust_ID = " & cust_ID, db)
it depends on the type of the database ID "cust_ID", is it integer or varchar, etc?
Check out this link for some more info on the different ways of getting data.
BUT there is nothing wrong with how you are trying to do it, just have to keep trying until you get it right.
http://samples.gotdotnet.com/quickst...soverview.aspx


======================================
They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================





Similar Threads
Thread Thread Starter Forum Replies Last Post
datareader in c# ryan.thomson C# 6 June 30th, 2009 12:48 AM
datareader MunishBhatia ASP.NET 2.0 Professional 2 October 17th, 2007 07:05 AM
Datareader NitinJoshi ADO.NET 4 January 31st, 2005 08:34 AM
datareader surapongmax ADO.NET 1 October 8th, 2004 07:53 AM
Using DataReader() aadz5 ASP.NET 1.0 and 1.1 Basics 12 November 21st, 2003 06:32 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.