how to check emailid already exist while registering on textbox leave event when new user in winforms

Amita Kharat 21 Reputation points
2021-11-24T08:21:16.16+00:00

152221-2021-11-24.png

Developer technologies Windows Forms
{count} votes

Accepted answer
  1. Jack J Jun 25,296 Reputation points
    2021-11-25T01:59:55.13+00:00

    @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:

    152408-11.gif

    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.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.