在我的Windows窗体应用程序中,我已将文本框放入组框中,并使用以下代码在文本框为空的情况下将“N/A”添加到文本框中。Windows窗体应用程序中的更新语句不起作用?
foreach (Control t in groupBox8.Controls)
{
if (t is TextBox)
{
if (t.Text == "")
t.Text = "N/A";
}
}
我试图更新使用下面的代码现有的记录:
SqlCommand cmd = new SqlCommand("UPDATE semester set [email protected]_name, [email protected]_code, [email protected]_marks, [email protected]_name, [email protected]_code, [email protected]_marks, [email protected]_name, [email protected]_code, [email protected]_marks, [email protected]_name, [email protected]_code, [email protected]_marks, [email protected]_name, [email protected]_code, [email protected]_marks, [email protected]_name, [email protected]_code, [email protected]_marks where [email protected] and [email protected]");
cmd.Connection = con;
cmd.Parameters.AddWithValue("@course", "ABC");
cmd.Parameters.AddWithValue("@semester", 1);
cmd.Parameters.AddWithValue("@paper1_name", textBox190.Text);
cmd.Parameters.AddWithValue("@paper1_code", textBox188.Text);
if (textBox186.Text == "N/A")
cmd.Parameters.AddWithValue("@paper1_marks", 0);
else
cmd.Parameters.AddWithValue("@paper1_marks", textBox186.Text);
cmd.Parameters.AddWithValue("@paper2_name", textBox189.Text);
cmd.Parameters.AddWithValue("@paper2_code", textBox187.Text);
if (textBox185.Text == "N/A")
cmd.Parameters.AddWithValue("@paper2_marks", 0);
else
cmd.Parameters.AddWithValue("@paper2_marks", textBox185.Text);
cmd.Parameters.AddWithValue("@paper3_name", textBox183.Text);
cmd.Parameters.AddWithValue("@paper3_code", textBox181.Text);
if (textBox180.Text == "N/A")
cmd.Parameters.AddWithValue("@paper3_marks", 0);
else
cmd.Parameters.AddWithValue("@paper3_marks", textBox180.Text);
cmd.Parameters.AddWithValue("@paper4_name", textBox177.Text);
cmd.Parameters.AddWithValue("@paper4_code", textBox176.Text);
if (textBox175.Text == "N/A")
cmd.Parameters.AddWithValue("@paper4_marks", 0);
else
cmd.Parameters.AddWithValue("@paper4_marks", textBox175.Text);
cmd.Parameters.AddWithValue("@paper5_name", textBox172.Text);
cmd.Parameters.AddWithValue("@paper5_code", textBox171.Text);
if (textBox170.Text == "N/A")
cmd.Parameters.AddWithValue("@paper5_marks", 0);
else
cmd.Parameters.AddWithValue("@paper5_marks", textBox170.Text);
cmd.Parameters.AddWithValue("@paper6_name", textBox179.Text);
cmd.Parameters.AddWithValue("@paper6_code", textBox174.Text);
if (textBox169.Text == "N/A")
cmd.Parameters.AddWithValue("@paper6_marks", 0);
else
cmd.Parameters.AddWithValue("@paper6_marks", textBox169.Text);
try
{
con.Open();
if (cmd.ExecuteNonQuery() > 0)
{
chk = 1;
}
else
{
label25.Text = "[Error: Cannot save the data]";
label25.Visible = true;
chk = 0;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
cmd.Dispose();
}
但我在此面临的一个问题。每当其他部分执行显示错误消息。
if (cmd.ExecuteNonQuery() > 0)
{
chk = 1;
}
else
{
label25.Text = "[Error: Cannot save the data]";
label25.Visible = true;
chk = 0;
}
而在数据库(学期表)中,也没有记录正在更新。请帮忙。我正在使用sql服务器。
没有“ASP.NET Windows窗体”应用程序这样的事情。看起来像你有Windows窗体与Web窗体混淆。我会更新标题... – mason
你有一个catch块。你有错误吗? – mason
@mason我是新来的,所以可能会出现错误,但我发现这个图像http://aspxtutorial.com/image.axd?picture=2010%2F7%2Fcsharp%2F1.png请看看我正在创建。 –