Saturday, May 2, 2015

web form not updating database with stored procedure

Hello

i have a problem with the web form updating the database i have a stored procedure which i need to connect to. If i execute the procedure in the SQL it will update the database but when i run the web form i get my catch error "could not update database". I have read so much on the net and my code seem ok but i,m just so lost.

stored procedure

PROCEDURE [dbo].[UpdateCustomer]
@ID INT,
@Firstname VARCHAR(30),
@Surname VARCHAR(30),
@Age INT

AS

BEGIN
UPDATE Customer

SET Firstname = @Firstname,
        Surname = @Surname,
        Age = @Age


WHERE CustID = @ID

END

update code

 try
            {
                SqlCommand command = new SqlCommand();

                command.Connection = conn;
                command.CommandType = CommandType.StoredProcedure;
                command.CommandText = "UpdateCustomer";
                command.Connection.Open();

                SqlParameter param = new SqlParameter();
                param.ParameterName = "@ID";
                param.SqlDbType = SqlDbType.Int;
                param.Direction = ParameterDirection.Input;
                param.Value = txtCustID.Text;
                command.Parameters.Add(param);



                command.Parameters.AddWithValue("@CustID", txtCustID.Text.ToString());
                command.Parameters.AddWithValue("@Firstname", txtFirstname.Text);
                command.Parameters.AddWithValue("@Surname", txtSurname.Text);
                command.Parameters.AddWithValue("@Gender", Gender.Text.ToString());
                command.Parameters.AddWithValue("@Age" ,txtAge.Text.ToString());
                command.Parameters.AddWithValue("@Address1", txtAddress1.Text.ToString());
                command.Parameters.AddWithValue("@Address2", txtAddress2.Text.ToString());
                command.Parameters.AddWithValue("@City", txtCity.Text.ToString());
                command.Parameters.AddWithValue("@Phone", txtPhone.Text.ToString());
                command.Parameters.AddWithValue("@Mobile", txtMobile.Text.ToString());
                command.Parameters.AddWithValue("@Email", txtEmail.Text.ToString());

                 command.ExecuteNonQuery();

                lblMessage.Text = "Your Record(s) Have been Updated";
                command.Connection.Close();
            }
            catch
            {
                lblMessage.Text = "Your Record was not updated please try again";
            }

Thank you for your help

No comments:

Post a Comment