【问题标题】:unable to update when 0(zero) value is stored for empty fields in c#当 C# 中的空字段存储 0(零)值时无法更新
【发布时间】:2017-04-14 11:05:12
【问题描述】:

我有一个非强制性的电话文本框字段,数据类型是 int。如果我将电话字段留空并提交表单,电话列将在数据库中存储为 0(零)。我面临更新问题: 在电话为“0”时更新时,相应的行不会更新。 我需要将电话的默认值设置为空吗?如果是请解释一下。

这是我的更新查询

cmd.CommandText = "UPDATE client_final SET companyname = @companyname, url = @url, industry = @industry, contactperson1 = @contactperson1, contactperson2 = @contactperson2, designation = @designation, fax = @fax, phone = @phone, mobile = @mobile, emailid1 = @emailid1, emailid2 = @emailid2, baddress = @baddress, bcity = @bcity, bstate = @bstate, bzipcode = @bzipcode, bcountry = @bcountry, regaddress = @regaddress, regcity = @regcity, regstate = @regstate, regzipcode = @regzipcode, regcountry = @regcountry WHERE client_id = @client_id";
                    cmd.Parameters.AddWithValue("@client_id", txtClientId.Text);
                    cmd.Parameters.AddWithValue("@companyname", txtcompanyname.Text);
                    cmd.Parameters.AddWithValue("@url", txturl.Text);
                    cmd.Parameters.AddWithValue("@industry", drpindustry.Text);
                    cmd.Parameters.AddWithValue("@contactperson1", txtcontactperson1.Text);
                    cmd.Parameters.AddWithValue("@contactperson2", txtcontactperson2.Text);
                    cmd.Parameters.AddWithValue("@designation", txtdesignation.Text);
                    cmd.Parameters.AddWithValue("@fax", txtfaxnumber.Text);
                    cmd.Parameters.AddWithValue("@phone", txtphone.Text);
                    cmd.Parameters.AddWithValue("@mobile", txtmobile.Text);
                    cmd.Parameters.AddWithValue("@emailid1", txtemailid1.Text);
                    cmd.Parameters.AddWithValue("@emailid2", txtemailid2.Text);
                    cmd.Parameters.AddWithValue("@baddress", txtbaddress.InnerText);
                    cmd.Parameters.AddWithValue("@bcity", txtbcity.Text);
                    cmd.Parameters.AddWithValue("@bstate", txtbstate.Text);
                    cmd.Parameters.AddWithValue("@bzipcode", txtbzipcode.Text);
                    cmd.Parameters.AddWithValue("@bcountry", bddlCountries.Text);
                    cmd.Parameters.AddWithValue("@regaddress", txtraddress.InnerText);
                    cmd.Parameters.AddWithValue("@regcity", txtrcity.Text);
                    cmd.Parameters.AddWithValue("@regstate", txtrstate.Text);
                    cmd.Parameters.AddWithValue("@regzipcode", txtrzipcode.Text);
                    cmd.Parameters.AddWithValue("@regcountry", rddlCountries.Text);

【问题讨论】:

  • 更新时出现什么错误?
  • @Dheeraj Sharma 错误未显示,但单击按钮时所有字段均变为空白。
  • 可能是您的代码而不是查询中存在一些错误,因为任何字段中的 0 都不会阻止查询执行,请详细说明您的问题。
  • 我已经添加了我的更新查询,如果查询有问题,那么我不应该在任何情况下工作。
  • 你的代码很好,问题出在其他地方,你可以使用 try catch 块并使用断点来抓取错误。

标签: c# sql-server database sql-update


【解决方案1】:

因为您的 phone 列的数据类型是 int,而 int 的默认值为 0。请在数据库中将此列指定为可为空,然后一切正常。

【讨论】:

    【解决方案2】:
    DECLARE @sampletable TABLE (ID INT IDENTITY(1,1),phonenumber INT)
    
    INSERT INTO @sampletable VALUES('') -- here is your insertion
    
    SELECT * FROM @sampletable          -- as you said column is updated with 0 if sent blank
    
    UPDATE @sampletable SET phonenumber = '' WHERE ID = 1 --if we update, this works fine
    
    SELECT * FROM @sampletable          -- again it is updated with 0 because we passed blank
    
    --Where is the problem??
    

    【讨论】:

      【解决方案3】:

      在任何表格列中,如果您不想标记为强制,则必须设置允许 null,因为如果它考虑默认值 "0" 就像在您的场景中一样,而不是 NULL 那么它可能会在后期产生问题。最好的方法是为您的场景设置 Allow Null

      例如,您有其他列表示 TypeId,它的类型是 INT,所以现在如果用户没有在此列中输入,最好设置 NULL而不是 0。因为有时用户使用 Enum 对象,该对象可能与任何类型相关联的值为 0。

      【讨论】:

        【解决方案4】:

        我认为它适用于您的场景

        如下所示替换更新查询,

        UPDATE client_final 
        SET companyname = @companyname, 
            url = @url, 
            industry = @industry, 
            contactperson1 = @contactperson1, 
            contactperson2 = @contactperson2, 
            designation = @designation, 
            fax = @fax, 
            phone = NullIf(@phone, ’0’), 
            mobile = @mobile, 
            emailid1 = @emailid1, 
            emailid2 = @emailid2, 
            baddress = @baddress, 
            bcity = @bcity, 
            bstate = @bstate, 
            bzipcode = @bzipcode, 
            bcountry = @bcountry, 
            regaddress = @regaddress, 
            regcity = @regcity, 
            regstate = @regstate, 
            regzipcode = @regzipcode, 
            regcountry = @regcountry 
        WHERE 
            client_id = @client_id;
        

        使用NullIf函数更新Null值而不是0

        【讨论】:

          猜你喜欢
          • 2015-07-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-09-20
          相关资源
          最近更新 更多