[英]How to update data in a sql server database with values from textboxes when only some of the textboxes are empty and some are filled?
We have a form on our aspx page with a number of textboxes. 我们的aspx页面上有一个表单,其中包含许多文本框。 We would like to UPDATE several tables in our sql server database with the values inputted into these text boxes however if any of the text boxes are left blank we would like to leave the data for that column as it is, currently it won't accept empty values. 我们想在我们的sql server数据库中更新几个表,并在这些文本框中输入值,但是如果任何文本框留空,我们希望保留该列的数据,目前它不接受空值。
If all textboxes are filled, it functions correctly, if not, we get the error: Input string was not in a correct format. 如果所有文本框都已填充,它将正常运行,否则,我们会收到错误:输入字符串的格式不正确。
Code fired when submit button is clicked: 单击提交按钮时触发的代码:
protected void buttonClicked(object sender, EventArgs e)
{
string CS = ConfigurationManager.ConnectionStrings["G4StowawaysConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(CS);
conn.Open();
String sql5 = "" ;
if (!@BalanceOutstanding.Text.Equals(null) | !@Comments.Text.Equals(null))
{
sql5 += "UPDATE BookingView SET ";
if (!@BalanceOutstanding.Text.Equals(null))
{
sql5 += " [Balance_Outstanding]=@BalanceOutstanding,";
}
if (!@Comments.Text.Equals(null))
{
sql5 += " [Comments]=@Comments ";
}
sql5 += "WHERE [Booking_Ref] = @BookingRef;";
}
if (!@BoatID.Text.Equals(null) | !@SpecialRequests.Text.Equals(null) | !@LeadPassenger.Text.Equals(null) | !@Duration.Text.Equals(null) | !@Pets.Text.Equals(null) | !@Children.Text.Equals(null) | !@Passengers.Text.Equals(null))
{
sql5 += "UPDATE vBoat_Booking SET";
if (!@BoatID.Text.Equals(null))
{
sql5 += " [BoatID]=@BoatID,";
}
if (!@SpecialRequests.Text.Equals(null))
{
sql5 += " [Special_Request]=@SpecialRequests,";
}
if (!@LeadPassenger.Text.Equals(null))
{
sql5 += " [Lead_PassengerID]=@LeadPassenger,";
}
if (!@Duration.Text.Equals(null))
{
sql5 += " [Duration_In_Hours]=@Duration,";
}
if (!@Pets.Text.Equals(null))
{
sql5 += " [Number_of_pets]=@Pets,";
}
if (!@Children.Text.Equals(null))
{
sql5 += " [Number_of_children]=@Children,";
}
if (!@Passengers.Text.Equals(null))
{
sql5 += " [Number_of_passengers]=@Passengers ";
}
sql5 += "WHERE [Booking_Ref] = @BookingRef;";
}
SqlCommand cmd2 = new SqlCommand(sql5, conn);
//add our parameters to our command object
cmd2.Parameters.AddWithValue("@BookingRef", Convert.ToInt32(BookingRef.Text));
cmd2.Parameters.AddWithValue("@BoatID", Convert.ToInt32(BoatID.Text));
cmd2.Parameters.AddWithValue("@LeadPassenger", Convert.ToInt32(LeadPassenger.Text));
cmd2.Parameters.AddWithValue("@Duration", Convert.ToInt32(Duration.Text));
cmd2.Parameters.AddWithValue("@Pets", Convert.ToInt32(Pets.Text));
cmd2.Parameters.AddWithValue("@Children", Convert.ToInt32(Children.Text));
cmd2.Parameters.AddWithValue("@Passengers", Convert.ToInt32(Passengers.Text));
cmd2.Parameters.AddWithValue("@SpecialRequests", SpecialRequests.Text);
cmd2.Parameters.AddWithValue("@BalanceOutstanding", Convert.ToInt32(BalanceOutstanding.Text));
cmd2.Parameters.AddWithValue("@Comments", Comments.Text);
cmd2.ExecuteNonQuery();
conn.Close();
}
You are using trailing commas in your if blocks and should be using leading commas. 您在if块中使用尾随逗号,并且应使用前导逗号。 Except for the first field that is added to your dynamic SQL. 除了添加到动态SQL的第一个字段。 You can use a bool to determine if the code segment needs a leading comma. 您可以使用bool来确定代码段是否需要前导逗号。
Another option is to create the dynamic SQL in a stored procedure. 另一种选择是在存储过程中创建动态SQL。 If you do this, you will want to use parameters much like you're using them in your C# code. 如果这样做,您将需要使用与C#代码中使用的参数非常相似的参数。 Use sp_executesql instead of exec. 使用sp_executesql而不是exec。 This allows you to define parameters and give those parameters a value. 这允许您定义参数并为这些参数赋值。 Go here to see an example of using sp_executesql with parameters. 转到此处查看将sp_executesql与参数一起使用的示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.