2

I have a form that should edit the information in the database. First, I query the database using an URL parameter to get the record and display them in textboxes so I could update them. That part of the code works the issue is when I press submit to update the data after changing what's on the textboxes. Only LastMod column gets updated. I think the page_load fires twice and overwrites what's on the textboxes before the button click event fires.

Here's the code.

public partial class Edit : System.Web.UI.Page
{
    int aID;
    SqlConnection conn = new SqlConnection(@connectionstring);

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        { 
            conn.Open();
            aID = Convert.ToInt16(Request.QueryString["ID"]);
            string sql = "select * from Contacts where ID = '" + aID + "' ";

            SqlDataAdapter adap = new SqlDataAdapter(sql, conn);
            DataSet ds = new DataSet();

            adap.Fill(ds);

            txtFName.Text = ds.Tables[0].Rows[0]["Fname"].ToString();
            txtLName.Text = ds.Tables[0].Rows[0]["Lname"].ToString();
            txtEmail.Text = ds.Tables[0].Rows[0]["EmailAdd"].ToString();
            lblLastMod.Text = ds.Tables[0].Rows[0]["LastMod"].ToString();
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
            DateTime date = DateTime.Now;
            SqlCommand sqlComm = new SqlCommand();
            sqlComm = conn.CreateCommand();

            sqlComm.CommandText = @"UPDATE Contacts SET Fname=@FName, Lname = @LName, EmailAdd = @Eadd,LastMod = @LMod WHERE ID=@ID";
            sqlComm.Parameters.Add("@FName", SqlDbType.NChar);
            sqlComm.Parameters["@FName"].Value = txtFName.Text.Trim();
            sqlComm.Parameters.Add("@LName", SqlDbType.NChar);
            sqlComm.Parameters["@LName"].Value = txtLName.Text.Trim();
            sqlComm.Parameters.Add("@Eadd", SqlDbType.NChar);
            sqlComm.Parameters["@Eadd"].Value = txtEmail.Text.Trim();
            sqlComm.Parameters.Add("@LMod", SqlDbType.DateTime);
            sqlComm.Parameters["@LMod"].Value = date;
            sqlComm.Parameters.Add("@ID", SqlDbType.Int);
            sqlComm.Parameters["@ID"].Value = aID;

            conn.Open();
            sqlComm.ExecuteNonQuery();
            conn.Close();
    }
}

I tried adding !IsPostback in my Page_Load method but nothing gets updated even the LastMod record.

Thanks for any help I get.

9
  • Side note: using sqlComm.Parameters.AddWithValue() will half the number of lines. You don't need to specify the data types that way. Commented Feb 5, 2016 at 21:09
  • Noted Sir, I'll take that into account. Commented Feb 5, 2016 at 21:14
  • 3
    By putting breakpoint check each value in each line Commented Feb 5, 2016 at 21:14
  • 1
    @gunr2171: You should check out Can we stop using AddWithValue() already? and stop using .AddWithValue() - it can lead to unexpected and surprising results... Commented Feb 5, 2016 at 21:25
  • 2
    Why are you using SqlDbType.NChar instead of SqlDbType.NVarChar? Commented Feb 5, 2016 at 21:28

1 Answer 1

2

You declare a variable, aID, at the top of the page, and you set a value in it in the page load. You then also use it in the click, but you never re-set it. Therefore, for postback, it will not have a value. Move this to page load above your "if not ispostback"

protected void Page_Load(object sender, EventArgs e)
    {
       aID = Convert.ToInt16(Request.QueryString["ID"]);
        if (!IsPostBack)
        { 
            conn.Open();

            string sql = "select * from Contacts where ID = '" + aID + "' ";
Sign up to request clarification or add additional context in comments.

6 Comments

aID = Convert.ToInt16(Request.QueryString["ID"]); this will work if it's being set in a different page so what you are saying is not 100% correct without seeing where the OP initially sets the value of the querystring.. also I am wondering why the OP is using Convert.ToInt16 vs Convert.ToInt32
@MethodMan: it's a variable, it won't retain the value between postbacks as far as I recall and he uses it again in the Click event
Convert.ToInt16(Request.QueryString["ID"]); this will have a value if it's not a Post back meaning on the initial Page_Load to persist the value I would recommend assigning the Request.QueryString value to ViewState["aID"] = Convert.ToInt32(Request.QueryString["ID"])
A button click causes a postback does it not? Am I mistaken?
Surprisingly it worked. I completely missed that while using breaks. Excuse my noobishness in debugging I'm trying to learn. Thank you for all the help.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.