2014-01-23 69 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("")) 

代替现有的条件