2013-04-20 75 views
0

我想在LINQ更新語句中運行以下內容。LINQ文本框匹配數據庫

if (updateProfile.website == txtSite.Text) { } 
else { updateProfile.website = txtSite.Text; } 

這只是checkes,看是否文本框的值相匹配的在數據庫中,如果是的話那麼它的動作上,如果沒有它更新它。

我遇到的問題是,如果文本框是空的,它傳遞「」所以一個空白值。如果文本框是「」,我希望它繼續前進,這樣它會在數據庫中留下空值。

所有幫助非常感謝,謝謝。

回答

1

您可能需要使用要string.IsNullOrEmpty(),這幾乎做什麼,它說:

if(string.IsNullOrEmpty(txtSite.Text) || updateProfile.website == txtSite.Text) { } 
else { updateProfile.website = txtSite.Text; } 

另外,如果你只關心的價值""並希望從null明顯的對待它,你可以只檢查明確:

if (txtSite.Txt == "" || updateProfile.website == txtSite.Text) { } 
else { updateProfile.website = txtSite.Text; } 
+0

非常感謝,工作過一種享受! - 我剛剛使用了第一個建議:) – thatuxguy 2013-04-20 21:56:11

1

,或者你可以檢查你的文本框,這樣,然後添加您的條件

if (txtSite.Text.Trim().Length > 0) 
{ 
    if (updateProfile.website == txtSite.Text) { } 
    else { updateProfile.website = txtSite.Text; } 
}