The Update form data is used to update the record in data base. Upon clicking the 'btnUpdate' on form, the event handler shows the old data in debug/trace mode, and therefore old data gets stored and new changed data does not.
The helper class DBUtility is in a separate C# file and namesapce, which is properly imported.
Helper Class:
public class DBUtility {
public static Int32 ExecuteNonQuery(string cmdName, CommandType cmdType, params SqlParameter[] pars) {
using (SqlConnection conn = new SqlConnection(connString)) {
using (SqlCommand cmd = new SqlCommand(cmdName, conn)) {
cmd.CommandType = cmdType;
cmd.CommandText = cmdName;
if (pars.Length > 0)
cmd.Parameters.AddRange(pars);
conn.Open();
return cmd.ExecuteNonQuery();
}
}
}
}
Problem Code: the btnUpdate_Click method
public partial class InsertStudent : System.Web.UI.Page {
private int studId;
protected void Page_Load(object sender, EventArgs e) {
studId = Int32.Parse(Request.Params.Get("id"));
DataTable table = DBUtility.ExecuteSelectTable("SELECT * FROM stud_master WHERE StudId = @ID", CommandType.Text, new SqlParameter[] { new SqlParameter("@ID", studId) });
txtID.Text = table.Rows[0].ItemArray[0].ToString();
txtFName.Text = table.Rows[0].ItemArray[1].ToString();
txtLName.Text = table.Rows[0].ItemArray[2].ToString();
txtEmail.Text = table.Rows[0].ItemArray[3].ToString();
txtCity.Text = table.Rows[0].ItemArray[4].ToString();
}
protected void btnUpdate_Click(object sender, EventArgs e) {
string updateCommand = "UPDATE stud_master SET firstName = @Fname, lastName = @Lname, emailId = @Email, city = @City WHERE StudID = @ID";
SqlParameter[] parameters = new SqlParameter[] {
new SqlParameter("@ID", txtID.Text),
new SqlParameter("@Fname", txtFName.Text),
new SqlParameter("@Lname", txtLName.Text),
new SqlParameter("@Email", txtEmail.Text),
new SqlParameter("@City", txtCity.Text)
};
DBUtility dbUtility = new DBUtility();
if (DBUtility.ExecuteNonQuery(updateCommand, CommandType.Text, parameters) > 0) {
lblStatus.Text = "Record Successfully Updated.";
}
else {
lblStatus.Text = "Cannot add new Record. Operation failed.";
}
}
}
When similar code in the Insert form is used it gets inserted.
Similar Code with no problem
public partial class InsertStudent : System.Web.UI.Page {
protected void btnInsert_Click(object sender, EventArgs e) {
string insertCommand = "INSERT INTO stud_master VALUES(@ID, @Fname, @Lname, @Email, @City)";
SqlParameter[] parameters = new SqlParameter[] {
new SqlParameter("@ID", txtID.Text),
new SqlParameter("@Fname", txtFName.Text),
new SqlParameter("@Lname", txtLName.Text),
new SqlParameter("@Email", txtEmail.Text),
new SqlParameter("@City", txtCity.Text)
};
Int32 rowsAffected = DBUtility.ExecuteNonQuery(insertCommand, CommandType.Text, parameters);
if (rowsAffected > 0) {
lblStatus.Text = "Record Successfully added.";
}
else {
lblStatus.Text = "Cannot add new Record. Operation failed.";
}
}
}