2013-07-26 60 views
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(); 
     } 
    } 

回答

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(); 
}   
相關問題