ALI Sajjad Rizvi
ALI Sajjad Rizvi

Reputation: 185

Textbox value does not get updated in asp.net

I have some Textboxes in my page. I am getting values from Database in and updating Textboxes in my page load. I have a code to update values in database behind update button. the problem is that when I change the textbox value and click update button, the value in textbox is again the original one. It retains its value. My code is

protected void Page_Load(object sender, EventArgs e)
    {
        int id = Convert.ToInt32(Session["DocumentID"]);
        Connection conn = new Connection();
        string query = "SELECT * from Document where DocumentID='" + id + "'";
        SqlCommand sqlcom = new SqlCommand(query, conn.conopen());
        SqlDataAdapter daexp = new SqlDataAdapter(sqlcom);
        System.Data.DataTable dtexp = new System.Data.DataTable();
        daexp.Fill(dtexp);
        TextBox1.Text = dtexp.Rows[0][1].ToString();
        TextBox3.Text = dtexp.Rows[0][2].ToString();
        TextBox6.Text = dtexp.Rows[0][3].ToString();
        TextBox4.Text = dtexp.Rows[0][4].ToString();
        TextBox5.Text = dtexp.Rows[0][5].ToString();
        TextBox7.Text = dtexp.Rows[0][6].ToString();
        TextBox7.ReadOnly = true;
    }



protected void Button1_Click(object sender, EventArgs e)
    {
    int id = Convert.ToInt32(Session["DocumentID"].ToString());
    if (FileUpload1.HasFile)
    {
        HttpPostedFile Image = FileUpload1.PostedFile;
        string contentType = Image.ContentType;
        if (contentType.Equals("application/octet-stream"))
        {
            contentType = "application/pdf";
        }
        else if (contentType.Equals("application/vnd.openxmlformats-officedocument.wordprocessingml.document"))
        {
            contentType = "application/msword";
        }
        int nFileLen = Image.ContentLength;
        byte[] myData = new byte[nFileLen];
        Image.InputStream.Read(myData, 0, nFileLen);
        Connection con = new Connection();
        con.conopen();
        string query = "UPDATE Document SET Setup='" + TextBox1.Text + "', ReferenceNO='" + TextBox3.Text + "', DocumentDate='" + TextBox6.Text + "', Subject='" + TextBox4.Text + "', NameOfInitiator='" + TextBox5.Text + "', Document=@Doc, FolderID='" + DropDownList1.Text + "', DocTypeID='" + DropDownList4.Text + "', DirectorateID='" + DropDownList3.Text + "', OrganizationID='" + DropDownList2.Text + "' WHERE DocumentID='" + id + "'";
        SqlCommand sqlcom = new SqlCommand(query, con.conopen());
        sqlcom.Parameters.AddWithValue("@Doc", myData);
        sqlcom.ExecuteNonQuery();
        Label12.Text = "Document Updated Successfully";
        Label12.ForeColor = System.Drawing.Color.Green;
        Label12.Visible = true;
        Label12.Text = "Document Updated Successfully";
        Label12.ForeColor = System.Drawing.Color.Green;
        Label12.Visible = true;
    }
    else
    {
        Connection conn = new Connection();
        string query = "UPDATE Document SET Setup='" + TextBox1.Text + "', ReferenceNO='" + TextBox3.Text + "', DocumentDate='" + TextBox6.Text + "', Subject='" + TextBox4.Text + "', NameOfInitiator='" + TextBox5.Text + "', FolderID='" + DropDownList1.Text + "', DocTypeID='" + DropDownList4.Text + "', DirectorateID='" + DropDownList3.Text + "', OrganizationID='" + DropDownList2.Text + "' WHERE DocumentID='" + id + "'";
        SqlCommand sqlcom = new SqlCommand(query, conn.conopen());
        sqlcom.ExecuteNonQuery();
    }
  }

Upvotes: 3

Views: 7048

Answers (3)

RBILLC
RBILLC

Reputation: 190

Try to refer to the value Attribute:

TextBox1.Attributes("value")

Upvotes: 0

toan
toan

Reputation: 47

I know: Order Event of asp.net PreInit -Init -InitComplete -PreLoad -LoadPage -Control events -LoadComplete -PreRender -SaveStateComplete -Render -Unload

Therefore , When u onckick button, Webpage will ispostback. after, call pageload -> EventButton. value of textbox change, because your pageload set value for textbox.

Edit code:

protected void Page_Load(object sender, EventArgs e)
    {
if(!Page.IsPostBack){
    int id = Convert.ToInt32(Session["DocumentID"]);
    Connection conn = new Connection();
    string query = "SELECT * from Document where DocumentID='" + id + "'";
    SqlCommand sqlcom = new SqlCommand(query, conn.conopen());
    SqlDataAdapter daexp = new SqlDataAdapter(sqlcom);
    System.Data.DataTable dtexp = new System.Data.DataTable();
    daexp.Fill(dtexp);
    TextBox1.Text = dtexp.Rows[0][1].ToString();
    TextBox3.Text = dtexp.Rows[0][2].ToString();
    TextBox6.Text = dtexp.Rows[0][3].ToString();
    TextBox4.Text = dtexp.Rows[0][4].ToString();
    TextBox5.Text = dtexp.Rows[0][5].ToString();
    TextBox7.Text = dtexp.Rows[0][6].ToString();
    TextBox7.ReadOnly = true;

} }

Upvotes: 0

Syed Ali Taqi
Syed Ali Taqi

Reputation: 4974

You are updating the values of Textboxes from database in your Page_Load. Every time page is posted back it gets values from database and sets in TextBoxes. You need to add a check and only update values and set to TextBoxes if page is loaded first time and not posted back. Like this:

protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
//your code here...
}
}

Upvotes: 5

Related Questions