简体   繁体   English

如果只有一些文本框为空且有些文本框已填满,如何使用文本框中的值更新sql server数据库中的数据?

[英]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.

相关问题 如何在C#WinForm文本框中显示/激发SQL Server数据库中的数据(列总和) - How to display/ fire data (sum column values) from SQL Server database in C# WinForm textBoxes 如何将数据更新到mysql虽然有些文本框是空白的? (将保留空白文本框的数据) - How to update data to mysql although some textboxes are blank? (The data of blank textboxes will be kept) 在 .NET MVC 文本框中显示一些数据库数据 - Displaying some of the database data in .NET MVC textboxes 使用存储过程使用文本框中的值更新SQL Server数据库 - Updating SQL Server database with values from textboxes using stored procedure 如何在asp中文本框为空的情况下阻止将数据添加到数据库中? - how to stop data from adding into the database even when the textboxes are empty in asp? 使用insert命令参数时是否可以将某些文本框保留为空以保存数据 - Is it possible to leave some textboxes empty to save data when using insert command parameter 在datagridview中显示一些值,而不是在文本框中显示 - Show some values into datagridview instead of Textboxes 仅使用填充的文本框执行搜索 - Perform search with filled textboxes only 如何仅从选定的文本框中更新数据库以获取选择性信息? (asp.net C#) - How to update database for selective information from selected textboxes only? (asp.net c#) 如何将多个动态文本框值保存到SQL Server数据库中 - How to save multiple dynamic textboxes value into SQL Server database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM