Reputation:
I would like to update database table using placeholder textboxes but say it is 2 textboxes and the user edits one of the textboxes, I'm sure how to update the table to know which one the user edited. as well as i need to keep track of the number that was edited. I was reading the previous value from the database and then adding that value when I'm updating. However I have this but it is is not updating the table.
Stored Procedure
update Student set Course1= @Course1, Course2= @Course2,Course3= @Course3,
lastUser=@user, lastUpdate=@lastupdate, previous_Course1=@previous_Course1,previous_Course2=@previous_Course2,previous_Course3=@previous_Course3 where userNum=@userNum
Code Behind
protected void btnUpdate_Click(object sender, EventArgs e)
{
string previousCourse = null;
foreach (TextBox textBox in ContentPlaceHolder1.Controls.OfType<TextBox>())
{
SqlCommand com2 = new SqlCommand("select course1,course2,course3 from Course where loadsheetnum= '" + txtuserNum.Text + "'", connection);
SqlDataReader myReader = null;
myReader = com2.ExecuteReader();
while (myReader.Read())
{
previousCourse = Convert.ToString(myReader["Course1"]);
}
myReader.Close();
}
int maxPossibleTextBoxCount = 3;
int selectedTextBoxCount = ContentPlaceHolder1.Controls.OfType<TextBox>().Count();
int emptyTextBoxCount = maxPossibleTextBoxCount - selectedTextBoxCount;
using (var cmd = new SqlCommand("PP_updateStudent", connection))
{
cmd.Parameters.AddWithValue("@userNum", txtuserNum.Text);
cmd.Parameters.Add("@lastupdate", SqlDbType.DateTime).Value = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
int counter = 1;
foreach (TextBox textBox in ContentPlaceHolder1.Controls.OfType<TextBox>())
{
cmd.Parameters.AddWithValue($"@previous_Course{counter++}", previousCourse );
cmd.Parameters.AddWithValue($"@Course{counter++}", textBox.Text);
}
if (emptyTextBoxCount > 0)
{
for (int i = 0; i < emptyTextBoxCount - 1; i++)
{
cmd.Parameters.AddWithValue($"@Course{counter++}", System.DBNull.Value);
cmd.Parameters.AddWithValue($"@previous_Course{counter++}", System.DBNull.Value);
}
}
cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
}
}
}
Upvotes: 0
Views: 126
Reputation: 641
Just taking a guess here, based on what I understand from the information so far.
Your first for loop is setting the previousCourse variable but the way it's written the variable is overwritten in each loop, so there's no need for the for loop. Then the previousCourse variable is not used in the command to run the stored procedure, which means you've queried the database for nothing and you're not using the information about the previousCourse. When you add the @previousCourse parameter you're giving it the value from the text box, not the value from the database.
If you want to store 3 previous courses, one for each text box, then you'll need 3 variables: previousCourse1, previousCourse2 and previousCourse3. Call the dataReader once and read each field from it into your variables.
previousCourse1 = Convert.ToString(myReader["Course1"]);
previousCourse2 = Convert.ToString(myReader["Course2"]);
previousCourse3 = Convert.ToString(myReader["Course3"]);
If any users have fewer than 3 courses, those fields will be blank or null in the database. They'll also be blank or null in the dataReader and the parameters, so when you update the database again they'll still be null or blank. You don't have to figure out which ones have changed. Just update them all with what you have. If they didn't change then they will be updated to the same value they were before.
When setting the parameters, don't bother with the for loop or the counter. Just set them all every time. I can't see how you've named your text boxes, but I assume they are txtCourse1, txtCourse2, txtCourse3. If the existing value in the text box is the same as the corresponding previousCourse, then you don't have to add the parameter.
if (txtCourse1.Text != previousCourse1) {
cmd.Parameters.AddWithValue($"@previous_Course1, previousCourse1);
cmd.Parameters.AddWithValue($"@Course1", txtCourse1.Text);
counter++;
}
if (txtCourse2.Text != previousCourse2) {
cmd.Parameters.AddWithValue($"@previous_Course2, previousCourse2);
cmd.Parameters.AddWithValue($"@Course2", txtCourse2.Text);
counter++;
}
if (txtCourse3.Text != previousCourse3) {
cmd.Parameters.AddWithValue($"@previous_Course3, previousCourse3);
cmd.Parameters.AddWithValue($"@Course3", txtCourse3.Text);
counter++;
}
Another thing that's puzzling is your selectedTextBoxCount includes all of the text boxes in the container, so if you have 3 textboxes in the container, then the emptyTextBoxCount will always be 0. I think the suggestions above will deal with the empty text boxes without your having to do it separately.
Or, maybe you can do this:
if (txtCourse1.Text != previousCourse1 && txtCourse1.Text != String.Empty) {
cmd.Parameters.AddWithValue($"@previous_Course1, previousCourse1);
cmd.Parameters.AddWithValue($"@Course1", txtCourse1.Text);
} else if (txtCourse1.Text == String.Empty){
cmd.Parameters.AddWithValue($"@previous_Course1, System.DBNull.Value);
cmd.Parameters.AddWithValue($"@Course1", System.DBNull.Value);
}
if (txtCourse2.Text != previousCourse2 && txtCourse2.Text != String.Empty) {
cmd.Parameters.AddWithValue($"@previous_Course2, previousCourse2);
cmd.Parameters.AddWithValue($"@Course2", txtCourse2.Text);
} else if (txtCourse2.Text == String.Empty){
cmd.Parameters.AddWithValue($"@previous_Course2, System.DBNull.Value);
cmd.Parameters.AddWithValue($"@Course2", System.DBNull.Value);
}
if (txtCourse3.Text != previousCourse3 && txtCourse3.Text != String.Empty) {
cmd.Parameters.AddWithValue($"@previous_Course3, previousCourse3);
cmd.Parameters.AddWithValue($"@Course3", txtCourse3.Text);
} else if (txtCourse3.Text == String.Empty){
cmd.Parameters.AddWithValue($"@previous_Course3, System.DBNull.Value);
cmd.Parameters.AddWithValue($"@Course3", System.DBNull.Value);
}
Upvotes: 1