Inserting data into a database
-
Hi i am trying to insert data into a database using store procedure but it gives me this error "An SqlParameter with ParameterName '@Name' is not contained by this SqlParameterCollection" and here is my code string conString = @"Data Source=DAKESERVER;Initial Catalog=Customer;User ID=sa"; SqlConnection conCustomer = new SqlConnection(conString); conCustomer.Open(); try { SqlCommand cmdCustomer = new SqlCommand("AddCustomer"); cmdCustomer.Connection = conCustomer; cmdCustomer.CommandType = CommandType.StoredProcedure; cmdCustomer.Parameters["@Name"].Value = txtName.Text.ToString(); cmdCustomer.Parameters["@Surname"].Value = txtSurname.Text.ToString(); cmdCustomer.Parameters["@Address"].Value = txtAddress.Text.ToString(); cmdCustomer.Parameters["@Telephone"].Value = txtTelephone.Text.ToString(); if (cmdCustomer.ExecuteNonQuery() == 1) { lblMessage.Text = "Record inserted"; } else { lblMessage.Text = "record not inserted"; } } catch (Exception ex) { lblMessage.Text = ex.Message; } finally { conCustomer.Close(); }
Mamphekgo
-
Hi i am trying to insert data into a database using store procedure but it gives me this error "An SqlParameter with ParameterName '@Name' is not contained by this SqlParameterCollection" and here is my code string conString = @"Data Source=DAKESERVER;Initial Catalog=Customer;User ID=sa"; SqlConnection conCustomer = new SqlConnection(conString); conCustomer.Open(); try { SqlCommand cmdCustomer = new SqlCommand("AddCustomer"); cmdCustomer.Connection = conCustomer; cmdCustomer.CommandType = CommandType.StoredProcedure; cmdCustomer.Parameters["@Name"].Value = txtName.Text.ToString(); cmdCustomer.Parameters["@Surname"].Value = txtSurname.Text.ToString(); cmdCustomer.Parameters["@Address"].Value = txtAddress.Text.ToString(); cmdCustomer.Parameters["@Telephone"].Value = txtTelephone.Text.ToString(); if (cmdCustomer.ExecuteNonQuery() == 1) { lblMessage.Text = "Record inserted"; } else { lblMessage.Text = "record not inserted"; } } catch (Exception ex) { lblMessage.Text = ex.Message; } finally { conCustomer.Close(); }
Mamphekgo
Have u declare parameter @Name is StoredProcedure? Best Regard Pathan
---------------------------------------------------
-
Have u declare parameter @Name is StoredProcedure? Best Regard Pathan
---------------------------------------------------
yes i did declared it
Mamphekgo