parameter passing asp.net vb

B

Ben Barnes

hi,

I'm a relative newbie to .net but have been an asp developer for a few
years. I'm starting the slow and somehwat painful process of learning
how to do everything I used to do in asp in .net.

i'm using visual studio and am simply trying to do the following:

Pass a value into a stored procedure or a sql query which has been is
passed via a querystring.

I've achieved this by setting the value of the parameter in the
desinger generated code but I would like to be able to handle this in
the page_load. I'd like to check for the existence of a paramater and
pass this value to the query.

I'm sure it's really easy, it certainly is in classic asp but i'm
really struggling with this one.

Anyone help?
 
C

Curt_C [MVP]

page_load.....

if(Request.QueryString("val") != null)
{
string myVal = Request.QueryStirng("val");
...your other stuff....
}
 
B

benbarnes007

Hi Curt,

Thanks. the above I can handle but it is passing the value back into
the query to be the parameter that I'm struggling with.

So, it's a case of putting myVal back as the value of
Me.SqlCommand1.Parameters.Add
 
K

Karl Seguin

Ben,
It isn't too complicated, but it can certainly be confusing at first.
Personally I'm not the biggest fan of design time stuff, I realize it's much
quicker and simpler, but if you're like me, you like having a fine grain
control. Additionally, you learn a lot more by doing it yourself.

Having said that, I'm not 100% sure what step you are having a hard time
with. Is it simply getting a value from the querystring? or assigning it as
a parameter? Without knowing what you have, I'll show you a basic way of
doing the entire thing:

(code not compiled, typing it off the top of my head)

Sub Page_Load
dim id as integer = 0
if Request.QueryString("userId") is nothing then
id = -1 'some default value
else
id = Convert.ToInt32(Request.QueryString("userId")) 'could throw an
exception if it isn't a valid it, so you might wanna try/catch this
end if

dim connection as new SqlConnection(CONNECTION_STRING)
dim command as new SqlCommand(connection, "STORED_PROCEDURE_NAME")
command.commandType = commandType.StoredProcedure
command.Parameters.Add("@UserId", SqlDbType.Int).Value = id 'here's where
you assign the variable to the sproc

dim ds as new DataSet
dim da as new SqlDataAdapter(command)
try
connection.Open()
da.Fill(ds)
finally
connection.dispose()
command.dispose()
da.dispose()
end try


you now have a DataSet (ds) with the data..

The above code should be moved into a utility function, which returns a
dataset, so that you can reuse it, but other than that, all should be good.

lemme know if anything specific irkes you.

Karl
 
B

benbarnes007

Hi Karl,

Spot on, that looks like it will do the trick! thanks

The thing is I'm doing this through Visual Studio and am draggin the
tables onto the designer surface and hooking things up that way.
Getting data out is fine and foring a parameter is also ok but getting
the value of the parameter to be passedin at deisgn time is a touch
more tricky.

Thanks your help, I'll give it a go, but it looks like it will do the
job perfectly. Thanks for taking the time to reply..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top