I am working on a website where i have some textbox with ReadOnly value as false i.e. it is editable. On page load the original name(value) of the user is loaded from the database which he/she can change/update if there is some mistake in it. But when i change the value and click on submit, it still takes the value that was previously stored instead of the new data given. The .aspx code snippet of the textbox:
<div class="form-group" style="width: 80%">
<div class="input-group mb-2">
<div class="input-group-prepend">
<div class="input-group-text headDetails">Trainee Name</div>
</div>
<asp:TextBox ID="TextBox2" runat="server" CssClass="form-control"></asp:TextBox>
</div>
</div>
.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
string UserId = Session["new2"].ToString();
Response.Write(UserId);
try
{
mycon = new MySqlConnection("server=localhost;database=trainee;user id=abcd;password=abcde@gea;");
MyCm = new MySqlCommand("select * from trainee_master where trainee_master.Reference_Number='" + UserId.ToString() + "'", mycon);
myad = new MySqlDataAdapter(MyCm);
DataTable dt = new DataTable();
myad.Fill(dt);
if (dt.Rows.Count > 0)
{
TextBox1.Text = dt.Rows[0][0].ToString();
TextBox2.Text = dt.Rows[0][1].ToString();
DropDownList1.Text = dt.Rows[0][2].ToString();
DropDownList2.Text = dt.Rows[0][3].ToString();
TextBox5.Text = dt.Rows[0][9].ToString();
TextBox6.Text = dt.Rows[0][4].ToString();
TextBox7.Text = dt.Rows[0][5].ToString();
TextBox8.Text = dt.Rows[0][6].ToString();
TextBox9.Text = dt.Rows[0][7].ToString();
TextBox10.Text = dt.Rows[0][8].ToString();
}
//TextBox2.Focus();
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
protected void Button1_Click(object sender, EventArgs e) //Submit button
{
try
{
string s1 = TextBox1.Text;
Response.Write(s1);
string s2 = TextBox2.Text;
Response.Write(s2);
string s3 = DropDownList1.Text;
Response.Write(s3);
string s4 = DropDownList2.Text;
Response.Write(s4);
string s5 = TextBox5.Text;
Response.Write(s5);
string s6 = TextBox6.Text;
Response.Write(s6);
string s7 = TextBox7.Text;
Response.Write(s7);
string s8 = TextBox8.Text;
Response.Write(s8);
string s9 = TextBox9.Text;
Response.Write(s9);
string s10 = TextBox10.Text;
Response.Write(s10);
string sk = "update trainee_master set Name='" + TextBox2.Text + "',Discipline='" + DropDownList1.Text + "',Branch='" + DropDownList2.Text + "',Mobile_No='" + TextBox6.Text + "',Email='" + TextBox7.Text + "',Guide_PNo='" + TextBox8.Text + "',Guide_Name='" + TextBox9.Text + "',Project_Title='" + TextBox10.Text + "',College_Name='" + TextBox5.Text + "' where trainee_master.Reference_Number='" + TextBox1.Text + "'";
cmd1 = new MySqlCommand(sk, mycon);
mycon.Open();
cmd1.ExecuteNonQuery();
Response.Write("<script>window.alert('Record updated Successfully')</script>");
mycon.Close();
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
}
}
How to make the textbox dynamically editable?