[英]Unable to update database table from placeholder textboxes
我想使用占位符文本框更新數據庫表,但是說它是2個文本框,並且用戶編輯了其中一個文本框,我確定如何更新表以知道用戶編輯了哪個文本框。 以及我需要跟蹤已編輯的號碼。 我正在從數據庫中讀取先前的值,然后在更新時添加該值。 但是我有這個,但它不是在更新表。
儲存程序
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
背后的代碼
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();
}
}
}
根據我從到目前為止的信息中了解的內容,僅在這里進行猜測。
您的第一個for循環是設置previousCourse變量,但是在每個循環中都將其寫入變量的方式覆蓋了,因此不需要for循環。 然后,該命令中未使用previousCourse變量來運行該存儲過程,這意味着您沒有查詢數據庫,也沒有使用有關previousCourse的信息。 添加@previousCourse參數時,您將在文本框中為其提供值,而不是數據庫中的值。
如果要存儲3個先前的課程,每個文本框一個,那么您將需要3個變量:previousCourse1,previousCourse2和previousCourse3。 調用dataReader一次,並將其中的每個字段讀入變量。
previousCourse1 = Convert.ToString(myReader["Course1"]);
previousCourse2 = Convert.ToString(myReader["Course2"]);
previousCourse3 = Convert.ToString(myReader["Course3"]);
如果任何用戶的課程少於3門,則數據庫中的這些字段將為空白或為空。 它們在dataReader和參數中也將為空或為空,因此當您再次更新數據庫時,它們仍將為空或為空。 您不必弄清楚哪些已更改。 只需使用您擁有的所有內容進行更新。 如果它們沒有變化,那么它們將被更新為以前的值。
設置參數時,請勿打擾for循環或計數器。 每次都設置它們。 我看不到您如何命名您的文本框,但我假設它們是txtCourse1,txtCourse2,txtCourse3。 如果文本框中的現有值與相應的previousCourse相同,則無需添加參數。
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++;
}
令人費解的是您的selectedTextBoxCount包含了容器中的所有文本框,因此,如果容器中有3個文本框,則emptyTextBoxCount始終為0。我認為上述建議將處理空文本框,而無需您分開做。
或者,也許您可以這樣做:
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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.