@Amita Kharat , Based on your description, you want to check if the EmailId already exists in the database when you register the information in the textbox leave event.
I suggest that you could use SqlReader.HasRows to check it.
Here is a code example you could refer to.
private void txtEmailId_Leave(object sender, EventArgs e)
{
SqlConnection connection = new SqlConnection(connstr);
connection.Open();
string sql = string.Format("select * from EmailDetail where EmailId={0}", txtEmailId.Text);
SqlCommand command = new SqlCommand(sql,connection);
SqlDataReader reader = command.ExecuteReader();
if(reader.HasRows)
{
MessageBox.Show("The EmailId already exists in the database");
}
else
{
MessageBox.Show("The EmailId does not exist in the database");
}
}
I created a simple table so that we can make a test.
The following is tested result:
Best Regards,
Jack
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.