IF声明似乎有问题

时间:2014-01-23 16:59:24

标签: c# asp.net if-statement

我的一个程序中有以下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");
}

5 个答案:

答案 0 :(得分: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注入。

答案 1 :(得分:2)

您应该使用以下其中一项:

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

答案 2 :(得分: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
}

答案 3 :(得分: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
}

答案 4 :(得分: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(""))

取代现有条件