Update database table based on textbox values

16,713

Page load event fire first before firing Click event of btnSave. Using IsPostBack eliminate that problem.

protected void Page_Load(object sender, EventArgs e)
{
   if (!IsPostBack)
   {
     Info();
   }
}
Share:
16,713
Chow.Net
Author by

Chow.Net

Updated on June 04, 2022

Comments

  • Chow.Net
    Chow.Net almost 2 years

    I am trying to update database table based on text-box values.But i am unable to get new modified values in database.

    Here is my code which displays information in text-boxes coming from database

    protected void Info()
    {
        string sID = Request.QueryString["ID"];
        string sSql = "select * from [fn_Items]('"+sID+"')";
        DataTable tbl1 = new DataTable();
        string sEr = myClass.FillupTable(sSql, ref tbl1);
        if (tbl1.Rows.Count > 0)
        {
            DataRow rd = tbl1.Rows[0];
            txtName.Text = rd["Name"].ToString();
            txtCategory.Text = rd["Category"].ToString();
            DateTime mStart;
            DateTime mFinish;
            Boolean b = false;
    
            b = DateTime.TryParse(rd["start"].ToString(), out mStart);
            b = DateTime.TryParse(rd["end"].ToString(), out mFinish);
            txtStart.Text = mStart.ToString("dd-MMM-yyyy");
            txtend.Text = mFinish.ToString("dd-MMM-yyyy");
            txtDescription.Text = rd["Description"].ToString();
    
        }
    }
    

    Now when the user modifies the text-box data and clicks save, the data should change in the database. Here is my click_event:

    protected void btnSave_Click(object sender, EventArgs e)
    {
        using (SqlConnection sqlConn = new 
            SqlConnection("Data Source=MSSQLSERVER2008;Initial Catalog=Items_2;Persist Security Info=True;User ID=sa;Password=password"))
        {
    
            string query = "UPDATE tbl_Items SET Name = @Name,Category=@Category,start = @start, end = @End,Description = @Description where ID = @ID ";
            SqlCommand cmd = new SqlCommand(query, sqlConn);
            cmd.Parameters.AddWithValue("@ID",txtID.Text);
            cmd.Parameters.AddWithValue("@Name", txtName.Text);
            cmd.Parameters.AddWithValue("@Category", Category.Text);
            cmd.Parameters.AddWithValue("@start", txtStart.Text);
            cmd.Parameters.AddWithValue("@end",txtend.Text);
            cmd.Parameters.AddWithValue("@Description", txtDescription.Text);
            cmd.Connection.Open();
            try
            {
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw new Exception("Error " + ex.Message);
            }
    
        }
    

    Here while updating, in text-boxes I am getting the values present in the database where I need to get the modified values. How do I get the modified values instead?

    • davids
      davids over 11 years
      When debugging, do you see the values in the click event? Are you sure they are labeled correctly?
    • Chow.Net
      Chow.Net over 11 years
      When Iam debugging I am getting the previous values which are from Info() . I am not getting the values that are changed.
    • Lunyx
      Lunyx over 11 years
      On a side note, you should validate your text inputs. Your current code is vulnerable to SQL injections. For example, let's say that in txtID.Text, I input the value 12345' drop table tbl_items , I would be able to drop your table.
    • davids
      davids over 11 years
      If the click event doesn't show the current values, then running the script will not update the db with those values either. You have to fix that first.
    • Win
      Win over 11 years
      I'll take a swing. :) You need !IsPostBack in Page Load event like - Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Info(); }}
    • Chow.Net
      Chow.Net over 11 years
      Yes that is what my problem is,,I can't understand why click event doesn't show the current values
    • Win
      Win over 11 years
      Please see my explanation in my answer.
    • Chow.Net
      Chow.Net over 11 years
      Thank You @Win yes i forgot to add Ispostback in my page load...Thanks a lot!!!!!