4

I have stored proc as below:

ALTER PROC pr_Update_Users_Nomination ( @UserID AS VARCHAR(100), @Nominated AS BIT ) AS UPDATE User SET isNominated = @Nominated WHERE EMPID = @UserID; 

I want to call this procedure from c# code: Below is the code I am trying:

void OpenConnection() { string Nominated = "False"; //Connection String string sConnString = System.Configuration.ConfigurationManager.ConnectionStrings["ConString1"].ConnectionString; SqlConnection mySqlCon = new SqlConnection(sConnString); SqlCommand mySqlCom = mySqlCon.CreateCommand(); //Call the stored proc and provide in parameters mySqlCom.CommandText = "EXECUTE pr_Update @UserID @Nominated"; mySqlCom.Parameters.Add("@UserID", SqlDbType.VarChar, 20).Value = UserID; mySqlCom.Parameters.Add("@Nominated", SqlDbType.Bit).Value = Nominated; mySqlCon.Open(); mySqlCom.ExecuteNonQuery(); mySqlCon.Close(); } 

I get an error saying

Incorrect Syntax near @Nominated 

4 Answers 4

5

first, when executing a procedure with parameter(s), separate the parameters with a comma

EXECUTE pr_Update @UserID, @Nominated 

second, modify your code into this,

 string sConnString = System.Configuration.ConfigurationManager.ConnectionStrings["ConString1"].ConnectionString; using(SqlConnection mySqlCon = new SqlConnection(sConnString)) { using(SqlCommand mySqlCom = new SqlCommand()) { mySqlCom.Connection = mySqlCon; mySqlCom.CommandText = "pr_Update"; mySqlCom.CommandType = CommandType.StoredProcedure; mySqlCom.Parameters.Add("@UserID", SqlDbType.VarChar, 20).Value = UserID; mySqlCom.Parameters.Add("@Nominated", SqlDbType.Bit).Value = Nominated; try { mySqlCon.Open(); mySqlCom.ExecuteNonQuery(); } catch(SqlException ex) { // do something with the exception // don't hide it } } } 
Sign up to request clarification or add additional context in comments.

Comments

4

You are missing a comma (,) between the parameters.

It should be

mySqlCom.CommandText = "EXECUTE pr_Update @UserID, @Nominated"; mySqlCom.Parameters.Add("@UserID", SqlDbType.VarChar, 20).Value = UserID; mySqlCom.Parameters.Add("@Nominated", SqlDbType.Bit).Value = Nominated; 

Alternatively, since all you are doing is calling a stored proc, you could do:

mySqlCom.CommandType = CommandType.StoredProcedure ; mySqlCom.CommandText = "pr_Update"; //no need to specify parameter names mySqlCom.Parameters.Add("@UserID", SqlDbType.VarChar, 20).Value = UserID; mySqlCom.Parameters.Add("@Nominated", SqlDbType.Bit).Value = Nominated; 

Comments

4

Give only name of stored procedure, as you are adding parameter in statements after this. Also set CommandType.

 mySqlCom.CommandText = "pr_Update"; mySqlCom.CommandType = CommandType.StoredProcedure; 

2 Comments

Don't forget to set mySqlCom.CommandType = CommandType.StoredProcedure
Thanks @Fabio GouW, I was doing that mean while.
1

You are invoking wrong SQL. You should set the command text of command to pr_Update only:

mySqlCom.CommandText = "pr_Update"; 

And set type command type to stored procedure:

mySqlCom.CommandType = CommandType.StoredProcedure; 

See MSDN page for more.

2 Comments

Then how will i send my In parameters ?
The way you did it. Only setting the CommandText was wrong.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.