Reputation: 3315
In this code I read the patient infos from database with patientId parameter passed with URL. Then I fill the 5 textboxes with this values at Page_Load. But at Update_Click the code can't reach the updated textbox values, it reads only initial values of the textboxes. I saw it at debugging. No problems at database level. When I remove the Page_Load code and leave the textboxes empty initially, the code from Update_Click can reach the user input and update the values. What is the problem?
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
public partial class admin_Testadmin : System.Web.UI.Page
{
Patient patient = new Patient();
protected void Page_Load(object sender, EventArgs e)
{
String cs = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
using (MySqlConnection cn = new MySqlConnection(cs))
{
MySqlCommand command = new MySqlCommand(StoredProcedures.select_patient, cn);
command.CommandType = CommandType.StoredProcedure;
int pid = int.Parse(Request.QueryString["patientId"]);
command.Parameters.AddWithValue("pid", pid);
command.Parameters["pid"].Direction = ParameterDirection.Input;
cn.Open();
MySqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
txtName.Text = patient.Name = (String)reader[0];
txtSurname.Text = patient.Surname = (String)reader[1];
txtFathersname.Text = patient.Fathersname = (String)reader[2];
txtStatus.Text = patient.Status = (String)reader[3];
txtSuccessDescription.Text = patient.SuccessDescription = (String)reader[4];
patient.Id = (int)reader[5];
}
reader.Close();
cn.Close();
}
}
protected void Update_Click(object sender, EventArgs e)
{
String cs = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
using (MySqlConnection cn = new MySqlConnection(cs))
{
MySqlCommand command = new MySqlCommand(StoredProcedures.update_patient, cn);
command.CommandType = CommandType.StoredProcedure;
patient.Name = txtName.Text.Trim();
patient.Surname = txtSurname.Text.Trim();
patient.Fathersname = txtFathersname.Text.Trim();
patient.Status = txtStatus.Text.Trim();
patient.SuccessDescription = txtSuccessDescription.Text;
patient.Id = int.Parse(Request.QueryString["patientId"]);
command.Parameters.Add("pname", MySqlDbType.VarChar).Value = patient.Name;
command.Parameters.Add("psurname", MySqlDbType.VarChar).Value = patient.Surname;
command.Parameters.Add("pfathersname", MySqlDbType.VarChar).Value = patient.Fathersname;
command.Parameters.Add("pstatus", MySqlDbType.VarChar).Value = patient.Status;
command.Parameters.Add("psuccessdescription", MySqlDbType.Text).Value = patient.SuccessDescription;
command.Parameters.AddWithValue("pid", patient.Id);
command.Parameters["pid"].Direction = ParameterDirection.Input;
cn.Open();
if (command.ExecuteNonQuery() > 0)
{
ResultLabel.Text = "";
ResultLabel.Text = "Update success";
}
cn.Close();
}
}
}
Upvotes: 0
Views: 68
Reputation: 51330
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
return;
// Rest of your code here
}
In the case of a postback, you do not want to overwrite what the user typed.
Here are some docs about the ASP.NET page life cycle.
Upvotes: 1