seeker_1234567
seeker_1234567

Reputation: 79

updated values are not getting stored in the textbox

    protected void Page_Load(object sender, EventArgs e)
    {
        cnst = "Data Source=IBM369-R9WAKY5;Initial Catalog=anudatabase;Integrated Security=True";
        cn = new SqlConnection(cnst);

        cn.Open();
        st = "select * from patient_db where unique_id = 123";
        cmd = new SqlCommand(st, cn);
        dr = cmd.ExecuteReader();
        if (dr.Read())
        {
            Label9.Text = dr.GetString(1);
            Label10.Text = dr.GetInt16(2).ToString();
            Label11.Text = dr.GetString(6);
            Label12.Text = dr.GetString(7);
            TextBox1.Text = dr.GetString(3);
            TextBox2.Text = dr.GetDecimal(4).ToString();

        }
        cn.Close();
    }

//Button click function

protected void Button1_Click(object sender, EventArgs e)
    {
        cn.Open();           
        st = "update patient_db set address ='" + TextBox1.Text + "' ,phone=" + TextBox2.Text+"where unique_id=123";
        cmd = new SqlCommand(st, cn);
        int result2 = cmd.ExecuteNonQuery();

        if (Convert.ToBoolean(result2))
        {
            result1.Text = "details updated successfully";
        }
        cn.Close();
    }

After assigning the value to textbox from the database,if I type some other new value in the text box,it is not taking the new value,it still persists with the old value.May i know the reason and solution for this? thanks in advance

Upvotes: 0

Views: 75

Answers (3)

Tim Schmelter
Tim Schmelter

Reputation: 460340

Write a method LoadData, move the code from page_load into this method. Then call this method from page_load wrapped in a if(!IsPostBack)-check. Call this method also from the button-click event handler after you've updated the values.

private void LoadData()
{
    using (var cn = new SqlConnection("Data Source=IBM369-R9WAKY5;Initial Catalog=anudatabase;Integrated Security=True"))
    {
        cn.Open();
        using(var cmd = new SqlCommand("select * from patient_db where unique_id = 123", cn))
        using (var dr = cmd.ExecuteReader())
        {
            if (dr.Read())
            {
                Label9.Text = dr.GetString(1);
                Label10.Text = dr.GetInt16(2).ToString();
                Label11.Text = dr.GetString(6);
                Label12.Text = dr.GetString(7);
                TextBox1.Text = dr.GetString(3);
                TextBox2.Text = dr.GetDecimal(4).ToString();
            }
        }
    }
}    

protected void Page_Load(object sender, EventArgs e)
{
  if(!Page.IsPostBack)
  {
      LoadData();
  }
}


protected void Button1_Click(object sender, EventArgs e)
{
    // ... update
    LoadData();
}

Important notes:

  • Also use the using-statement for every object implementing IDisposable like the connection or the datareader. On that way all unmanaged resources are disposed properly. Even in case of an error.
  • If 123 is just an example and actually is a value provided by the user use sql-parameters to prevent sql-injection. No, use them always.

Upvotes: 4

Urvi
Urvi

Reputation: 250

You need to check page.Ispostback property in your page Load. Here is your code.

protected void Page_Load(object sender, EventArgs e)
{
  if(!Page.IsPostBack)
  {
    cnst = "Data Source=IBM369-R9WAKY5;Initial Catalog=anudatabase;Integrated Security=True";
    cn = new SqlConnection(cnst);

    cn.Open();
    st = "select * from patient_db where unique_id = 123";
    cmd = new SqlCommand(st, cn);
    dr = cmd.ExecuteReader();
    if (dr.Read())
    {
        Label9.Text = dr.GetString(1);
        Label10.Text = dr.GetInt16(2).ToString();
        Label11.Text = dr.GetString(6);
        Label12.Text = dr.GetString(7);
        TextBox1.Text = dr.GetString(3);
        TextBox2.Text = dr.GetDecimal(4).ToString();

    }
    cn.Close();
   }
}

When you click on button then it will first call PageLoad event. So it will again set your old value to textbox and then it will call update method. So Update method will update old value to database.

Upvotes: 0

Mitat Koyuncu
Mitat Koyuncu

Reputation: 928

Add IsPostBack control at Page_Load method. Because before Button_Click event Page_Load event firing.

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        cnst = "Data Source=IBM369-R9WAKY5;Initial Catalog=anudatabase;Integrated Security=True";
        cn = new SqlConnection(cnst);

        cn.Open();
        st = "select * from patient_db where unique_id = 123";
        cmd = new SqlCommand(st, cn);
        dr = cmd.ExecuteReader();
        if (dr.Read())
        {
            Label9.Text = dr.GetString(1);
            Label10.Text = dr.GetInt16(2).ToString();
            Label11.Text = dr.GetString(6);
            Label12.Text = dr.GetString(7);
            TextBox1.Text = dr.GetString(3);
            TextBox2.Text = dr.GetDecimal(4).ToString();
        }
        cn.Close();
    }
}

Upvotes: 0

Related Questions