C# 无法从占位符文本框更新数据库表

C# 无法从占位符文本框更新数据库表,c#,asp.net,asp.net-placeholder,placeholder-control,C#,Asp.net,Asp.net Placeholder,Placeholder Control,我想使用占位符文本框更新数据库表,但假设它是2个文本框,用户编辑其中一个文本框,我确定如何更新表以知道用户编辑了哪一个。我还需要跟踪编辑的号码。我从数据库中读取以前的值,然后在更新时添加该值。然而,我有这个,但它不是更新表 存储过程 update Student set Course1= @Course1, Course2= @Course2,Course3= @Course3, lastUser=@user, lastUpdate=@lastupdate, previous_Cours

我想使用占位符文本框更新数据库表,但假设它是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门,则数据库中的这些字段将为空或null。在dataReader和参数中,它们也将为空或null,因此当您再次更新数据库时,它们仍然为空或null。你不必弄清楚哪些已经改变了。只要用你所拥有的更新它们就行了。如果它们没有改变,那么它们将被更新为与以前相同的值

在设置参数时,不要麻烦使用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);
}

我们真的需要看到更多的代码来理解您试图做什么以及问题是什么。另外,请说明代码运行时会发生什么。这就是我的代码。它不会更新表,也不会给今天发出错误消息发生了什么?什么意思?注释删除我想读取正在编辑的文本框中的值,所以我有sql数据读取器来存储以前的课程。但我猜我做得不对也许不是,我只是按照我所看到的去做,但它并不全在那里。你能发布html和所有隐藏的代码吗?这些是占位符控件文本框不是所有usernum都有3个过程,如何区分以更新?我如何在这里调用哪个变量cmd.Parameters.AddWithValue$@previous_course{counter++},previousCourse