I am checking username from DB table when a user enters username in registration page an textbox OnTextChanged event fired which checks if username is already exists or not. Here is code :
public void HandleTextbox1OnTextChanged(Object sender, EventArgs e)
{
string _connString = ConfigurationManager.AppSettings["connString"];
string username = txtUserName.Text;
int result = 0;
using (SqlConnection conn = new SqlConnection(_connString))
{
conn.Open();
SqlCommand cmd = new SqlCommand("select * from ref_CourseRegistration_Users where nm_UserName=@nm_UserName", conn);
cmd.Parameters.AddWithValue("@nm_UserName", username);
cmd.ExecuteNonQuery();
result = (int)cmd.ExecuteScalar();
if (result > 0)
{
lblMessage.Text = "username already exists, please enter different User Name!";
lblMessage.ForeColor = Color.Red;
}
else
{
}
}
}
it works fine when username exists but when username doesn't exists it shows an error "Object reference not set to an instance of an object." if i am missing anything?