로컬 SQL 서버 데이터베이스를 데이터 소스로 사용하여 올바르게 실행되고있는 비주얼 스튜디오 프로젝트를 만들고 있습니다.로컬 SQL 서버 데이터베이스가있는 Visual Studio 로그인 페이지
프로젝트의 로그인 양식을 만들어야합니다.
양식에 사용자 이름 텍스트 상자와 암호 텍스트 상자가 있으며 사용자가 세부 정보를 입력 한 다음 '로그인'버튼을 누르면 select sql 문을 실행해야합니다.
어떻게해야합니까?
내가 시도한 코드는 다음과 같습니다. "SqlDataReader dr = cmd.ExecuteReader();"라는 줄에 NullReferenceException이 발생합니다.
nullreferenceexception을 어떻게 해결합니까?
감사합니다.
private void button1_Click(object sender, EventArgs e)
{
try
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=MARKO-PC\\SQLEXPRESS;Initial Catalog=IS2B_G8_FundMeDB;Integrated Security=True";
con.Open();
String sql = "Select * from APPLICANT where applicant_ID_passport [email protected] AND password = @password";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.Add(new SqlParameter("@user", txtUserName.Text));
cmd.Parameters.Add(new SqlParameter("@password", txtPassword.Text));
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows == true)
{
MessageBox.Show("Login Successful");
}
else
{
MessageBox.Show("Login Failed");
}
}
catch (SqlException sqle)
{
MessageBox.Show("Sql Exception");
}
}
유사한 문제/용액 [여기 (http://stackoverflow.com/questions/40347457/what-is-wrong-with-my-c-sharp-login-code/ 사용해 40348686 # 40348686). – IMCI