0

我已经进行了验证,以防止在数据库为空时将数据插入数据库,但它仍然向数据库输入空白表单?

protected void Button1_Click(object sender, EventArgs e)
    {
        ProjectTestEntities User_save = new ProjectTestEntities();
        User ins = new User();

        ins.name = TextBox1.Text;
        ins.email = TextBox2.Text;
        ins.phone = TextBox3.Text;
        ins.gender = RadioButtonList1.SelectedValue;
        ins.password = TextBox4.Text;

        if (ins.name == null || ins.email == null || ins.gender == null || ins.password == null)
        {
            Label1.Text = "Incomplete input";
        }
        else
        {
            User_save.Users.AddObject(ins);
            User_save.SaveChanges();
        }
    }
4

1 回答 1

1

尝试使用string.IsNullOrEmpty()

if (string.IsNullOrEmpty(ins.name == null) || 
    string.IsNullOrEmpty(ins.email == null) ||
    string.IsNullOrEmpty(ins.gender == null) ||
    string.IsNullOrEmpty(ins.password == null))
{
    Label1.Text = "Incomplete input";
}
else
{
    User_save.Users.AddObject(ins);
    User_save.SaveChanges();
}        
于 2013-07-26T08:06:34.107 回答