multiple stored procedure parameters...???

reagan123

Active member
Joined
Feb 16, 2005
Messages
38
I am trying to execute a sql server stored procedure from a c# windows app. I need to send it 3 parameters, is this the correct way to do that or am taking the long route.

any feedback greatly appreciated!
Thanks

Code:
private void btnUpdate_Click(object sender, System.EventArgs e)
		{
			try
			{
				string strConnection = ConfigurationSettings.AppSettings["ConnectionString"];
				SqlConnection cnn = new SqlConnection(strConnection);

				SqlCommand objCommand = new SqlCommand("usp_InsertSoftware", cnn);
				objCommand.CommandType = CommandType.StoredProcedure;

				SqlParameter objParameter = new SqlParameter("@softwareName",SqlDbType.VarChar, 100);
				objCommand.Parameters.Add(objParameter);
				objParameter.Direction = ParameterDirection.Input;
				objParameter.Value = this.txtSoftware.Text;

				SqlParameter objParameter1 = new SqlParameter("@binderID",SqlDbType.Int);
				objCommand.Parameters.Add(objParameter);
				objParameter1.Direction = ParameterDirection.Input;
				objParameter1.Value = this.cboBinders.SelectedValue;

				SqlParameter objParameter2 = new SqlParameter("@subscriptionID",SqlDbType.Int);
				objCommand.Parameters.Add(objParameter2);
				objParameter2.Direction = ParameterDirection.Input;
				objParameter2.Value = this.cboSubscriptions.SelectedValue;

				cnn.Open();
				objCommand.ExecuteScalar();
				MessageBox.Show("Software has been added");

				cboSoftware.DataSource= null;
				bindSoftware();
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
			finally
			{
				//clean up 
			}

			
		}
 
I typically just do the following and it has always worked fine:
Code:
objCommand.Parameters.Add( new SqlParameter("@StringParam", "SomeString") );
objCommand.Parameters.Add( new SqlParameter("@IntParam", 9) );
 
thanks, that seems to work... is all the other stuff i had initially just extra fluff. is there any reason why i need to specify that info?

thanks for the help.
 
reagan123 said:
thanks, that seems to work... is all the other stuff i had initially just extra fluff. is there any reason why i need to specify that info?
You essentially had the same thing. Were both creating instances of the SqlParameter class, its just that I didnt put them into an instance variable. You really only need to do that if you need to access the properties. In my example, I didnt do that -- I just relied on the default property values.
 
Back
Top