protected void btnLogin_Click(object sender, EventArgs e) { string query = "Select Count(*) From LoginInfo Where lo_name = @username And lo_password = @password"; int userId = 0; using (SqlConnection conn = new SqlConnection(conStr)) { using (SqlCommand cmd = new SqlCommand(query, conn)) { cmd.CommandType = CommandType.Text; cmd.Parameters.AddWithValue("@username", txtUsername.Text); cmd.Parameters.AddWithValue("@password", txtPassword.Text); cmd.Connection = conn; conn.Open(); userId = Convert.ToInt32(cmd.ExecuteScalar()); conn.Close(); } if(userId>0) { Response.Redirect("Default.aspx"); } else { lable1.Text = "Login Fialed"; } } }
userId = Convert.ToInt32(cmd.ExecuteScalar());
object objValue = cmd.ExecuteScaler(); if (objValue != DbNull.Value) { userId = Convert.ToInt32(objValue); }
userId = (int) cmd.ExecuteScalar();
(int)cmd.ExecuteScalar();
int value = cmd.ExecuteScalar() as int; if (value != null) // do something else // indicate that something goes wrong, // for example, throw more informative exception // then the cast exception
Convert
int.Parse
int.TryParse
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)