reagan123 Posted December 30, 2005 Posted December 30, 2005 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 } } Quote
Mister E Posted December 30, 2005 Posted December 30, 2005 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) ); Quote
reagan123 Posted January 3, 2006 Author Posted January 3, 2006 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. Quote
Mister E Posted January 3, 2006 Posted January 3, 2006 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. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.