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//
protected void btnUpdate_Click(object sender, EventArgs e)
{
if (Request.QueryString["MODE"] != null)
{
SqlConnection Conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyLocal"].ConnectionString);
Conn.Open();
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";
SqlCommand cmd = new SqlCommand(sql, Conn);
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);
cmd.ExecuteNonQuery();
Conn.Close();
Response.Redirect("Customers.aspx");
}
if (Request.QueryString["id"] == null)
{
SqlConnection Conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyLocal"].ConnectionString);
Conn.Open();
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)";
SqlCommand cmd = new SqlCommand(sql, Conn);
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);
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
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 (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 = "";
SqlConnection Conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyLocal"].ConnectionString);
Conn.Open();
string sql = "SELECT * FROM [db_owner].[Customer] Where CustomerID=@CustomerID";
SqlCommand cmd = new SqlCommand(sql, Conn);
cmd.Parameters.AddWithValue("@CustomerID", Customer_ID());
SqlDataReader dr = null;
dr = cmd.ExecuteReader();
if (dr.Read() == false)
{
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();
}
}