2017-04-14 21 views
0

我有一個非強制電話文本框字段,數據類型爲int。如果我將電話域留空並提交表單,則電話號碼欄將在數據庫中存儲0(零)。我在更新時遇到問題: ,當手機爲'0'時更新,各行未更新。 我是否需要將手機的默認值設置爲空?如果是這樣請解釋。當0(零)值存儲爲c#中的空字段時無法更新

這裏是我的更新查詢

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); 
+0

當你更新? –

+0

@Dheeraj Sharma錯誤沒有顯示,但當點擊按鈕時,所有字段變爲空白。 – Nirmala

+0

有可能代碼中存在一些錯誤而不是查詢,因爲在任何字段中都有0不會停止執行查詢,請詳細說明您的問題。 –

回答

1

爲您的數據類型的手機列類型爲int,對於int默認值爲0,請指定此列數據庫可爲空,然後一切都應該工作。

0

在任何表列,如果你不希望標記爲強制性的,那麼你必須設置允許空,因爲如果考慮缺省值「0」像您的情況而不是NULL話,那可就在後期製造問題。最佳方法是爲您的場景設置允許爲空

比如你有另一列說爲TYPEID,它的類型是INT,所以現在如果用戶不在此列輸入,最好是設置NULL而不是0。因爲有些時候用戶使用的Enum對象可能是與任何類型關聯的0值。

+0

仍然無法使用我檢查了允許空位 – Nirmala

1
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?? 
0

我認爲它會在你的情況下工作

替換更新查詢像下面,

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

相關問題