Click here to Skip to main content
15,891,881 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi All ,How Are You , i hope doing well :)

C#
SqlConnection mySqlConnection = new SqlConnection(ConnectionString);
            SqlCommand mySqlCommand = new SqlCommand("InsertData", mySqlConnection);
            mySqlCommand.CommandType = CommandType.StoredProcedure;
            mySqlCommand.Parameters.Add("@UsrID", SqlDbType.NVarChar).Value = "ID0001";
            mySqlCommand.Parameters.Add("@UsrFName", SqlDbType.NVarChar).Value = "FirstName"; 
            mySqlCommand.Parameters.Add("@UsrLName", SqlDbType.NVarChar).Value = "LastName";


Can i using this code without put Parameter Name (@ParameterName) and Parameter Type (SqlDbType.ParameterType)
just , i want to send the values to Stored Procedure


my greetings ,
Posted

In order to send the parameters to a stored procedure, you have to give two bits of information: the parameter name, and the parameter value. If you miss out the name, the SP does not know where to put the value, and if you miss out the value, the SP does not know what to put in!

You have a choice: Use Parameters.Add as you are in your example, or use Parameters.AddWithValue instead:
C#
SqlConnection mySqlConnection = new SqlConnection(ConnectionString);
SqlCommand mySqlCommand = new SqlCommand("InsertData", mySqlConnection);
mySqlCommand.CommandType = CommandType.StoredProcedure;
mySqlCommand.Parameters.AddWithValue("@UsrID", "ID0001");
mySqlCommand.Parameters.AddWithValue("@UsrFName", "FirstName");
mySqlCommand.Parameters.AddWithValue("@UsrLName", "LastName");
 
Share this answer
 
Comments
M.Shawamreh 15-Jan-12 8:58am    
thank you
Have you tried by this way :

C#
SqlConnection mySqlConnection = new SqlConnection(ConnectionString);
            SqlCommand mySqlCommand = new SqlCommand("exec InsertData ID0001 , FirstName, LastName", mySqlConnection);
            mySqlCommand.CommandType = CommandType.Text;


Change the code so that it build the command string dynamically.

However the method in your question is the standard way to do this.

Also, have a look at this page :
http://msdn.microsoft.com/en-us/library/fksx3b4f%28v=vs.80%29.aspx[^]
 
Share this answer
 
v2
Comments
M.Shawamreh 15-Jan-12 8:58am    
Thank you very much :) , my greetings
Amir Mahfoozi 15-Jan-12 9:02am    
You're welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900