Yuo
Yuo

Reputation: 31

Stored Procedures and Variables

I made a stored procedure which has the following parameter: @UserName

Would that mean, in the ASP.NET, I would have to do the following code to update the value of username?

dbCommand.Parameters.Add("@UserName", SqlDbType.VarChar).Value = userName;

Or would this make a new parameter? If this just makes a new parameter, how can I just set the Parameter's value?

Upvotes: 0

Views: 71

Answers (4)

Chains
Chains

Reputation: 13167

It depends on your implementation. Here are a few common cases:

If you're using a datasource in your mark-up, then just use the wizard to configure your datasource, and it will discover your parameter automatically. Then you can see how it adds your parameter to your datasource's mark-up.

If you're using a datasource + code-behind, you can add parameters as you have in your post. As long as you don't ALSO have the parameter in your datasource mark-up, this will be fine.

If you're using a datasource + code-behind, you can assign values to parameters it already has like this: .Parameters["@UserName"].defaultvalue = "some value"

If you're NOT using a datasource, and doing everything in code-behind, then you can do this: .Parameters.AddWithValue("@UserName", "some value, some text value of a control, etc.")

Upvotes: 0

Pete M
Pete M

Reputation: 2048

That would create a new parameter. Parameters is a collection that you can access by name, so you can do something like this to update an existing param:

dbCommand.Parameters["@UserName"] = userName;

Upvotes: 1

jli
jli

Reputation: 6623

You can do something like dbCommand.Parameters.AddWithValue("@UserName", userName); or

dbCommand.Parameters.Add("@UserName", SqlDbType.VarChar);
dbCommand.Parameters["@UserName"].Value = userName;

Upvotes: 2

hunter
hunter

Reputation: 63562

dbCommand.Parameters.Add(new SqlParameter("@UserName", SqlDbType.VarChar) 
    { Value = userName });

Upvotes: 1

Related Questions