2011-12-19 3 views
0

나는 ASP.NET에서 내 웹 사이트에 대한 로그인 enterance에 대한 if 문을 만들려고합니다.asp.net : 왜 if 문이 작동하지 않습니까?

시간이 지나면 무언가가 null 인 경우 명령문을 입력하지 않는다는 사실을 무시합니다.

어디서 잘못 됐는지 말해 줄 수 있습니까?

connection.Open(); // 이름 * * *** 문자열 firstName = FirstNameTextBox.Text; string sqlquery = ("사용자 이름, 성, 사용자 이름, 암호) VALUES (@ 이름, @ 성, @ 사용자 이름, @ 암호)");

 SqlCommand command = new SqlCommand(sqlquery , connection); 
     command.Parameters.AddWithValue("FirstName", firstName); 
     //LastName************ 
     string lastName = LastNameTextBox.Text; 
     command.Parameters.AddWithValue("LastName", lastName); 
     //Username************* 
     string username = UsernameTextBox.Text; 
     command.Parameters.AddWithValue("UserName", username); 
     //Password************* 
     string password = PasswordTextBox.Text; 
     command.Parameters.AddWithValue("Password", password); 

     if (lastName != null || username != null || firstName != null || password != null) 
     { 


      if (PasswordTextBox.Text == ReTypePassword.Text) 
      { 
       Session["UserEnter"] = FirstNameTextBox.Text; 


       command.ExecuteNonQuery(); 
       Response.Redirect("HomeAfter.aspx"); 
      } 
      else if (PasswordTextBox.Text != ReTypePassword.Text) 
      { 
       ErrorLabel.Text = "Sorry, You didnt typed your password correctly. Please type again."; 
      } 
      else 
      { 
       ErrorLabel.Text = "Some Error has accured."; 
      } 
     } 
     else 
     { 
      ErrorLabel.Text = "Please fill all of the fields."; 
     } 
     connection.Close(); 
    } 
+1

당신은 [String.IsNullOrWhiteSpace] (http://msdn.microsoft.com/en-us/library/system.string.isnullorwhitespace.aspx) 대신 사용할 수 있습니다. –

답변

1

텍스트 상자는 null을 반환하지 않습니다. 빈 문자열을 반환하므로 AND 문을 사용하여 if 문을 !string.IsNullOrEmpty(string)으로 만들어야합니다. 해결 방법은 다음과 같습니다

if (!string.IsNullorEmpty(lastName) && !string.IsNullorEmpty(username) && !string.IsNullorEmpty(firstName) && !string.IsNullorEmpty(password)) 
{ 
     //code here 
} 
else 
{ 
ErrorLabel.Text = "Please fill all of the fields."; 
} 
+0

고마워요! 당신은 내 하루를 만들었 어 :) – thormayer

+0

괜찮아. 기본에 도움이 되었기 때문에 기쁩니다. :) – Mariusz

2

귀하의 AND의 대신 OR의를 사용하는 경우는

if (lastName != null && username != null && firstName != null && password != null) 
+0

텍스트 상자에서 null 값을 반환하지 않습니다. – Mariusz

0

Textbox.Text) (null 값을 반환 String.IsNullOrEmpty와 DOIT하려고 나던;

0

빈 텍스트 상자의 값은 null이 아닌 빈 문자열입니다.

관련 문제