Click here to Skip to main content
15,891,943 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
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

C#
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
C#
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.";
		}
	}
}
Posted
Updated 10-Oct-15 6:14am
v3
Comments
F-ES Sitecore 10-Oct-15 12:59pm    
It's more likely to be your data or your inputs than the code. Use SQL profiler to see what SQL is being executed, chances are there is no matching data where ID = whatever you're passing.
Mudit Khetan 11-Oct-15 2:34am    
What is happening over here is that the query is being executed ok, but the updated data from the text fields does not get added to the SqlParameters.
ZurdoDev 10-Oct-15 20:20pm    
Debug the code to make sure you know exactly what the issue is. If you're sure the textbox is the old value your code is doing it somewhere. You'll need to track that down.

1 solution

The problem was, as I discovered that the page load event was firing before the btnUpdate event. In the former I placed the code to initialize my text fields WITHOUT CHECKING if it is a postback! When that check was applied the problem resolved.
Thank you both for help and concern.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900