1

I have the below code, that connects to a Sql database and insert's data into a table :

string firstNameV = txtFname.Text;
string surnameV = txtSname.Text;
string emailV = txtEmail.Text;

SqlConnection conn = new   SqlConnection(ConfigurationManager.ConnectionStrings["myConnectionString"].ToString());

SqlCommand cmd = new SqlCommand();

cmd.CommandText = "INSERT INTO EmailSignUp (Title,FirstName,Surname,Email,EstablishmentType,Interests) VALUES (@Title,@FirstName,@Surname,@Email,@EstablishmentType,@Interests)";

cmd.Parameters.Add("@Title", SqlDbType.NVarChar).Value = title;
cmd.Parameters.Add("@FirstName", SqlDbType.NVarChar).Value = firstNameV;
cmd.Parameters.Add("@Surname", SqlDbType.NVarChar).Value = surnameV;
cmd.Parameters.Add("@Email", SqlDbType.NVarChar).Value = emailV;
cmd.Parameters.Add("@EstablishmentType", SqlDbType.NVarChar).Value = eType;
cmd.Parameters.Add("@Interests", SqlDbType.NVarChar).Value = ins;

cmd.Connection = conn;

conn.Open();

cmd.ExecuteNonQuery();
conn.Close();

How do I check if an email being entered in the "txtEmail" text box already exists in my database, in the email column and then alert message saying email already exists so it doesn't get inserted into my database?

2
  • Call a stored procedure instead of inserting directly into the database. Test within the procedure to see if the email address already exists and return a value if it does/doesn't and whether the insert went okay. Commented Mar 16, 2014 at 22:05
  • And, put the opening of the connection into a try statement with the closing of the connection in finally. Commented Mar 16, 2014 at 22:07

5 Answers 5

6

Call this method in required textbox or area

public void EmailCheck()
    {
        string constring = ConfigurationManager.ConnectionStrings["ConnData"].ConnectionString;
        SqlConnection con = new SqlConnection(constring);
        SqlCommand cmd = new SqlCommand("Select * from EmailSignUp where EmailId= @EmailId", con);
        cmd.Parameters.AddWithValue("@EmailId", this.txtEmail.Text);
        con.Open();
        SqlDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            if (dr.HasRows == true)
            {
                MessageBox.Show("EmailId = " + dr[5].ToString() + " Already exist");
                txtEmail.Clear();
                break;
            }
        }

    }
Sign up to request clarification or add additional context in comments.

Comments

0

Try this

cmd.CommandText = "IF NOT EXISTS(SELECT * FROM EmailSignUp WHERE Email = '" 
+ txtEmail.Text + "') 
BEGIN
INSERT INTO EmailSignUp (Title,FirstName,Surname,Email,EstablishmentType,Interests) VALUES (@Title,@FirstName,@Surname,@Email,@EstablishmentType,@Interests) 
END";

Comments

0

Call a stored Procedure and inside the stored procedure you can check before insert

 IF NOT EXISTS(SELECT * FROM EmailSignUp WHERE Email =@email)   
 Begin
insert query here
 end

In another way you can check it in text changed event also

Comments

0

Create a procedure on SQL server and check whether the name exists or not

    CREATE PROCEDURE Procedure_Name 
@mystring varchar(100),
@isExist bit out
AS
BEGIN
    if exists(select column1 from tblTable1 where column1=@mystring)
    begin
    select @isExist=1
    end
    else
    begin
    select @isExist=0
    end


END
GO

This is a sample procedure. If @isExist=1 that means the value exist.otherwise not. create a method to call this procedure and go on... Happy Coding

Comments

0

This works for me:

Create a function Called CheckMail(string email)

public bool CheckMail(string email)

        {
            SqlConnection con = new SqlConnection("Data Source=*******; Initial Catalog=Your Database Name; Persist Security Info=True;User ID=****; Password=******");

                SqlCommand cmd = new SqlCommand("select email from  Table Name where email='"+email+ "'",con);

                con.Open();

            SqlDataReader sdr = cmd.ExecuteReader();

            if (sdr.Read())
            {
                return false;
            }
            else
            {
                return true;
            }            
        }

 

Then Implement in Button Click as 

Pass Textbox value in function that were created..

 

if (CheckMail(EmailTxt.Text))  
                {

Write Your insert code to database 

}

else

{

Error Message or Alert to Show Already Exists in database

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.