Etienne
Etienne

Reputation: 7201

Working with a QueryString and placing value in Stored Procedure?

I am working with ASP.NET and SQL Server 2005.

I know how to create a stored proceudre but i do not know what to place in a stored procedure when i need to make use of a QueryString.

My SQL Statement in CODE =

"SELECT * FROM TableName WHERE ID = '" + Request.QueryString("ID") + "'" 

Now what must i place in my stored procedure to get this to work? I want to call a stored procedure and thus do not want to use this code in my code behind.

Thanks in advance!

Upvotes: 0

Views: 2936

Answers (4)

Etienne
Etienne

Reputation: 7201

The answer for this problem is located here

Upvotes: 0

maxy
maxy

Reputation: 438

SqlCommand cmd = new SqlCommand("storedprocedure_name",con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@parameter_of-procedure",Request.QueryString"ID"]);
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = mycommand;
DataSet dataset = new DataSet();
adapter.Fill(dataset);


//using sqldatareader
SqlCommand cmd = new SqlCommand("yourstoredproc", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@parameters_of_procedure",Request.QueryString["ID"]);
con.open()
sqldatareader dr=cmd.executereader();
gridview1.datasouce=dr;
gridview1.databind();
con.close();

Upvotes: 0

Himadri
Himadri

Reputation: 8876

Try the following code:

SqlCommand mycommand = new SqlCommand("yourstoredproc", con);
mycommand.CommandType = CommandType.StoredProcedure;
mycommand.Parameters.AddWithValue("@yourparam", Request.QueryString["ID"]);
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = mycommand;
DataSet dataset = new DataSet();
adapter.Fill(dataset);

Upvotes: 1

Paul
Paul

Reputation: 5576

Create an sqlcommand object

set the commandtext = "StoredProcName"

add an sqlParameter with the name of your sproc parameter - set its type

set its value to Request.QueryString("ID")

check this out for full instructions:

http://support.microsoft.com/kb/306574

Upvotes: 1

Related Questions