2014-01-23 68 views
0

我在我的一個程序中有以下IF條件,我設置條件以驗證強制文本字段是否爲空,如果是,顯示錯誤消息,但即使強制字段爲空仍然保存記錄,而不管必填字段。IF語句似乎有問題

if (!txt_teacherid.Equals(null) && !txt_teacherid.Equals("") && !txt_teacherfname.Equals(null) && !txt_teacherfname.Equals("") && !txt_teacherlname.Equals(null) && !txt_teacherlname.Equals("") && !txt_teacherdob.Equals(null) && !txt_teacherdob.Equals("") && !txt_teachernationality.Equals(null) && !txt_teachernationality.Equals("") && !txt_teacheraddress.Equals(null) && !txt_teacheraddress.Equals("")) 
{ 
    String teacherid = txt_teacherid.Text.Trim(); 
    String teacherfname = txt_teacherfname.Text.Trim(); 
    String teacherlname = txt_teacherlname.Text.Trim(); 
    String teachergender = opt_gender.SelectedItem.Value.ToString(); 
    String teachercivilstatus = opt_civilstatus.SelectedItem.Value.ToString(); 
    String teacherdob = txt_teacherdob.Text.Trim(); 
    String teachernationality = txt_teachernationality.Text.Trim(); 
    String teacheraddress = txt_teacheraddress.Text.Trim(); 
    String teachercontactno = txt_teachercontactno.Text.Trim(); 
    String teacherqualification = txt_teacherqualification.Text.Trim(); 
    String teacherexperience = txt_teacherexperience.Text.Trim(); 
    String teacherjobtitle = txt_teacherjobtitle.Text.Trim(); 
    String teacherjoindate = txt_teacherjoindate.Text.Trim(); 
    String imgpath = (String)Session["imagepath"]; 

    DBConnection db = new DBConnection(); 
    db.getConnection(); 
    db.executeUpdateQuery("INSERT INTO Teacher (TeacherID,TeacherFirstName,TeacherLastName,TeacherGender,TeacherDOB,TeacherCivilStatus,TeacherNationality,TeacherQualification,TeacherExperience,TeacherJobTitle,TeacherAddress,TeacherContactNo,TeacherJoinDate,ImagePath) VALUES ('" + teacherid + "','" + teacherfname + "','" + teacherlname + "','" + teachergender + "','" + teacherdob + "','" + teachercivilstatus + "','" + teachernationality + "','" + teacherqualification + "','" + teacherexperience + "','" + teacherjobtitle + "','" + teacheraddress + "','" + teachercontactno + "','" + teacherjoindate + "','" + imgpath + "')"); 
    Session["imagepath"] = null; 
    Page.ClientScript.RegisterStartupScript(this.GetType(), "Call my function", "recordInserted();window.location.href='AdminRegisterTeacher.aspx'", true); 
    //Response.Redirect("AdminRegisterTeacher.aspx"); 
} 
else 
{ 
    InnerError ie = new InnerError(); 
    ie.throwError("Oops! There was an error, Make sure you have filled all mandatory data"); 
} 
+2

http://msdn.microsoft.com/en-us/library/ system.string.isnullorwhitespace(v = vs.110).aspx –

+1

使用'string.IsNullOrEmpty'或'string.IsNullOrWhiteSpace'。另外,不要使用''「'文字,使用'string.Empty'。 – Jodrell

+3

...你在if語句中檢查'txt_teacherid'而不是'txt_teacherid.Text'。 – MikeSmithDev

回答

5

if (!txt_teacherid.Equals(null) && !txt_teacherid.Equals("")...是錯誤,因爲您正在檢查控制txt_teacherid而不是文本

應該僅僅是

if (!String.IsNullOrEmpty(txt_teacherid.Text.Trim())...) 

或使用String.IsNullOrWhiteSpace(.NET 4及以上):

if (!String.IsNullOrWhiteSpace(txt_teacherid.Text) && ...) 

還要注意,你應該使用參數化查詢,以防止SQL注入。

+0

我相信這是檢查是否控制初始化之前閱讀其值 – Jodrell

+0

@Jodrell可能,但不太可能,因爲他也檢查'txt_teacherid.Equals(「」)'。 – MikeSmithDev

+0

@Jodrell代碼的意圖和問題看起來是檢查控件的_value_以查看是否必填字段已空。如果是這樣,檢查是做錯了,這是給出正確結果的答案。此外,不需要檢查控制是否被初始化。 –

1

String的方法是.IsNullOrEmpty(),它將返回一個布爾值。你嘗試過使用它嗎?

因此將是:

if (!txt_teacherid.IsNullOrEmpty() && !txt_teacherfname.IsNullOrEmpty()&& !txt_teacherlname..IsNullOrEmpty() && !txt_teacherdob.IsNullOrEmpty() && !txt_teachernationality.IsNullOrEmpty() && !txt_teacheraddress.IsNullOrEmpty()) 
{ 
    //do database stuff here 
} 
+0

我在VS2010的建議列表中找不到這樣的方法 –

+0

對不起,我弄錯了,它是一個靜態方法,所以會是String.IsNullOrEmpty(txt_teacherid)...etc – TylerD87

2

應使用下列之一:

  • string.IsNullOrEmpty()
  • string.IsNullOrWhiteSpace()
1

使用下面的代碼:

if (!String.IsNullOrEmpty(txt_teacherid) && !String.IsNullOrEmpty(txt_teacherfname) && !String.IsNullOrEmpty(txt_teacherlname) && !String.IsNullOrEmpty(txt_teacherdob) && !String.IsNullOrEmpty(txt_teachernationality) && !String.IsNullOrEmpty(txt_teacheraddress)) 
{ 
    \\Save Data 
} 
else 
{ 
    \\show error 
} 
0

使用下面的代碼

if (!txt_teacherid.Text.Equals(null) && !txt_teacherid.Text.Equals("") && !txt_teacherfname.Text.Equals(null) && !txt_teacherfname.Text.Equals("") && !txt_teacherlname.Text.Equals(null) && !txt_teacherlname.Text.Equals("") && !txt_teacherdob.Text.Equals(null) && !txt_teacherdob.Text.Equals("") && !txt_teachernationality.Text.Equals(null) && !txt_teachernationality.Text.Equals("") && !txt_teacheraddress.Text.Equals(null) && !txt_teacheraddress.Text.Equals("")) 

代替現有的條件