Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

 


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 
		}

		
	}

Posted
I typically just do the following and it has always worked fine:
objCommand.Parameters.Add( new SqlParameter("@StringParam", "SomeString") );
objCommand.Parameters.Add( new SqlParameter("@IntParam", 9) );

Posted

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.

Posted
thanks' date=' 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?[/quote']You essentially had the same thing. We're both creating instances of the SqlParameter class, it's just that I didn't put them into an instance variable. You really only need to do that if you need to access the properties. In my example, I didn't do that -- I just relied on the default property values.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...