Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, I have a problem with my homework assignment.
I am able to access the database and insert new customer but it doesn't work when i try to update any record.
if anyone can tell me what is wrong with my code.
thank you so much

this is all my code

//this is my problem//
C#
protected void btnUpdate_Click(object sender, EventArgs e)
    {

        if (Request.QueryString["MODE"] != null)
        {
          
            
        

          
          //int CustomerID = Convert.ToInt32(Request.QueryString["id"]);

             

            //Declare the connection object
            SqlConnection Conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyLocal"].ConnectionString);

            //Make the connection 
            Conn.Open();

            //Define you query
            //  string updateSQL = "UPDATE [dbo].[xSample]" + "  " + "Set Region = " + txtFirstName.Text + " " + "SalesID= 1000";                                  
            // string sql = "UPDATE [dbo].[Customer] SET [FirstName] = @FirstName   WHERE CustomerID=@CustomerID";
            string sql = "UPDATE [Customer] SET [FirstName] = @FirstName,  [LastName] = @LastName, [Email] = @Email, [Password] = @Password, [Address1] = @Address1, [Address2] = @Address2, [City] = @City, [State] = @State, [zip] = @zip, [Phone] = @Phone, [Fax] = @Fax  WHERE CustomerID=@CustomerID";
     
            
            //Declare the Command
            SqlCommand cmd = new SqlCommand(sql, Conn);

            //Add the parameters needed for the SQL query
            cmd.Parameters.AddWithValue("@CustomerID", Customer_ID());
            cmd.Parameters.AddWithValue("@FirstName", txtFirstName.Text);
            cmd.Parameters.AddWithValue("@LastName", txtLastName.Text);
            cmd.Parameters.AddWithValue("@Email", txtEmailA.Text);
            cmd.Parameters.AddWithValue("@Password", txtPasswordA.Text);
            cmd.Parameters.AddWithValue("@Address1", txtAddress1.Text);
            cmd.Parameters.AddWithValue("@Address2", txtAddress2.Text);
            cmd.Parameters.AddWithValue("@City", txtCity.Text);
            cmd.Parameters.AddWithValue("@State", txtState.Text);
            cmd.Parameters.AddWithValue("@Zip", txtZip.Text);
            cmd.Parameters.AddWithValue("@Phone", txtPhone.Text);
            cmd.Parameters.AddWithValue("@Fax", txtFax.Text);
                        
            //Execute the query

            cmd.ExecuteNonQuery();

            Conn.Close();
            Response.Redirect("Customers.aspx");
            
            
        }

        

        if (Request.QueryString["id"] == null)
        {
            //Declare the connection object
            SqlConnection Conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyLocal"].ConnectionString);

            //Make the connection 
            Conn.Open();

            //Define you query
            //string sql = "UPDATE [db_owner].[Customer] SET FirstName=@FirstName, LastName=@LastName, Email=@Email, Password=@Password, Address1=@Address1, Address2=@Address2, City=@City, State=@State, Zip=@Zip, Phone=@Phone, Fax=@Fax  Where CustomerID=@CustomerID";
            string sql = "INSERT INTO Customer(FirstName, LastName, Email, Password, Address1, Address2, City, State, Zip, Phone, Fax) VALUES(@FirstName, @LastName, @Email, @Password, @Address1, @Address2, @City, @State, @Zip, @Phone, @Fax)";
            //string sqlN = "INSERT INTO [Customer] ([FirstName], [LastName], [Email], [Password], [Address1], [Address2], [City], [State], [Zip], [Phone], [Fax]) VALUES (@FirstName, @LastName, @Email, @Password, @Address1, @Address2, @City, @State, @Zip, @Phone, @Fax)";
            //Declare the Command
            SqlCommand cmd = new SqlCommand(sql, Conn);

            //Add the parameters needed for the SQL query

            cmd.Parameters.AddWithValue("@FirstName", txtFirstName.Text);
            cmd.Parameters.AddWithValue("@LastName", txtLastName.Text);
            cmd.Parameters.AddWithValue("@Email", txtEmailA.Text);
            cmd.Parameters.AddWithValue("@Password", txtPasswordA.Text);
            cmd.Parameters.AddWithValue("@Address1", txtAddress1.Text);
            cmd.Parameters.AddWithValue("@Address2", txtAddress2.Text);
            cmd.Parameters.AddWithValue("@City", txtCity.Text);
            cmd.Parameters.AddWithValue("@State", txtState.Text);
            cmd.Parameters.AddWithValue("@Zip", txtZip.Text);
            cmd.Parameters.AddWithValue("@Phone", txtPhone.Text);
            cmd.Parameters.AddWithValue("@Fax", txtFax.Text);



            //Execute the query

            cmd.ExecuteNonQuery();

            Conn.Close();
            Response.Redirect("Customers.aspx");

        }
          
    }

the problem in this code not in the update. anyone can tell me what is wrong with it
C#
public int Customer_ID()

{
    int CustomerID = 0;
   CustomerID= int.Parse(Request.QueryString["id"]);

  return CustomerID;

}





protected void Page_Load(object sender, EventArgs e)
{
   this.Master.HighlightMenu = "Customers";

   // if (Request.QueryString["MODE"] != null)
   // {
    if (Customer_ID()!=0)
    {


                txtFirstName.Text = "";
                txtLastName.Text = "";
                txtEmailA.Text = "";
                txtEmailB.Text = "";
                txtCity.Text = "";
                txtPasswordA.Text = "";
                txtPasswordB.Text = "";
                txtFax.Text = "";
                txtAddress1.Text = "";
                txtAddress2.Text = "";
                txtState.Text = "";
                txtZip.Text = "";
                txtPhone.Text = "";


        //Declare the connection object
        SqlConnection Conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyLocal"].ConnectionString);

        //Make the connection
        Conn.Open();

        //Define you query
        string sql = "SELECT * FROM [db_owner].[Customer] Where CustomerID=@CustomerID";

        //Declare the Command
        SqlCommand cmd = new SqlCommand(sql, Conn);

        //Add the parameters needed for the SQL query
        cmd.Parameters.AddWithValue("@CustomerID", Customer_ID());

        //Declare the DataReader
        SqlDataReader dr = null;

        //Fill the DataReader
        dr = cmd.ExecuteReader();

        //Get the data
        if (dr.Read() == false)
        {
            //No Records
            dr.Close();
            Conn.Close();
            return;
        }
        txtFirstName.Text = dr["FirstName"].ToString();
        txtLastName.Text = dr["LastName"].ToString();
        txtEmailA.Text = dr["Email"].ToString();
        txtEmailB.Text = dr["Email"].ToString();

        txtPasswordA.Text = dr["Password"].ToString();
        txtPasswordB.Text = dr["Password"].ToString();
        txtAddress1.Text = dr["Address1"].ToString();
        txtAddress2.Text = dr["Address2"].ToString();
        txtCity.Text = dr["City"].ToString();
        txtState.Text = dr["State"].ToString();
        txtZip.Text = dr["Zip"].ToString();
        txtFax.Text = dr["Fax"].ToString();
        txtPhone.Text = dr["Phone"].ToString();


        dr.Close();
        Conn.Close();
    }
}
Posted
Updated 10-Mar-12 1:40am
v5

my problem was i forgot to put postback in when the page load.


if (!IsPostBack)
{
 
Share this answer
 
Comments
loony malik 7-Jun-13 12:21pm    
Thank you so much, it saved my life!
cmd.Parameters.AddWithValue("@CustomerID", CustomerID);


Where are you setting CustomerID? If it is not set it will always be 0 and the WHERE clause will not match any record.

You should also learn about using statements

C#
using(SqlConnection conn = new SqlConnection(...))
{

}
 
Share this answer
 
v2
Comments
Mike988 9-Mar-12 11:13am    
this is my problem . the customerID is equal to querystring id.

CustomerID = Request.QueryString["id"];
Mike988 9-Mar-12 11:19am    
Do you know how i setting customerID to make it work,
i tried to put CustomerID = Request.QueryString["id"];
but it still doesn't work
[no name] 9-Mar-12 11:31am    
Where are you setting this? You haven't included that in the code snippet that I see.

QueryString is also a string. If the CustomerID in the database is not a string then you will need to convert it.
Mike988 9-Mar-12 11:37am    
i updated .
CustomerID = Request.QueryString["id"];
how can i convert the querystring to int. i think that is might be the problem.
Mike988 9-Mar-12 11:39am    
like that or ? int CustomerID = Convert.ToInt32(Request.QueryString["id"]);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900