Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone

I am working on a project and I am having trouble checking if one email address exists in my user table and I want to validate it before submitting the values. I have attached my code below. Thanks in advance.

C#
SqlConnection lytCom = new SqlConnection(ConfigurationManager.ConnectionStrings["constrlyt"].ConnectionString);
lytCom.open();
SqlCommand check_User_Name = new SqlCommand("SELECT COUNT(email) FROM [company] WHERE ([email] = @email)", lytCom);
            check_User_Name.Parameters.AddWithValue("@email", txtEmail.Text);
            int UserExist = (int)check_User_Name.ExecuteScalar();
            lblStatus.Text = UserExist.ToString();
            if (UserExist > 0)
                    {
                        lblStatus.Text = "Email Already Exists";
                        btnReg.Enabled = false;
                        lytCom.Close();
                    }
            else
                    {
                

                       btnReg.Enabled = true;
                        SqlCommand sqlcmd = new SqlCommand("insert into company (company_name,email,admin_password,date_joined) values ('" + txtCompName.Text + "','" + txtEmail.Text + "','" + lblEncryptedText.Text + "','" + System.DateTime.Now.ToString() + "')", lytCom);
                        sqlcmd.CommandType = CommandType.Text;
                        sqlcmd.ExecuteNonQuery();
                        lytCom.Close();
                        Response.Redirect("LogIn.aspx");
                        ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Welcome');", true);

                }
Posted
Updated 23-Dec-15 8:10am
v3
Comments
Mike Meinz 23-Dec-15 8:41am    
You don't say what kind of problem you are having so it is difficult to know how to help you.

I did spot two things that may or may not make a difference.
I would change these two lines to this:

SqlCommand check_User_Name = new SqlCommand("SELECT COUNT(*) FROM [company] WHERE ([email] = @email)", lytCom);
check_User_Name.Parameters.AddWithValue("email", txtEmail.Text.Trim().ToLower());
Faysel Kedir 23-Dec-15 8:52am    
Dear i want to validate whether given email address exists or not in the database.
Mike Meinz 23-Dec-15 8:57am    
Yes, you said that in your question. But you said you had a problem. What is the problem? Are you getting an error message? If so, what is it?
Faysel Kedir 23-Dec-15 9:37am    
currently my code is leting the user to insert same email address again and again
Mike Meinz 23-Dec-15 9:59am    
1. Did you make the changes that I suggested in my first comment?
SqlCommand check_User_Name = new SqlCommand("SELECT COUNT(*) FROM [company] WHERE ([email] = @email)", lytCom);
check_User_Name.Parameters.AddWithValue("email", txtEmail.Text.Trim().ToLower());
2. Is this problem caused by the fact that one or more of the letters in an email address in the database are of a different case then the letters entered by the user? See #5 below.
3. Make sure there are no spaces before or after the email address in the database.
4. The SQL INSERT statement should use SQLParameters like you do for the SQL SELECT statement. All ".Text" properties on the right side of an assignment statement should be coded as ".Text.Trim()" so that extraneous spaces are removed. Also, using SQLParameters prevents SQL Injection Attacks.
Like this:
SqlCommand sqlcmd = new SqlCommand("INSERT INTO company (company_name,email,admin_password,date_joined) VALUES(@CompName,@Email,@Encrypted,@TimeStamp);", lytCom);
sqlcmd.Parameters.AddWithValue("CompName",txtCompName.Text.Trim());
sqlcmd.Parameters.AddWithValue("Email",txtEmail.Text.Trim().ToLower());
sqlcmd.Parameters.AddWithValue("Encrypted",lblEncryptedText.Text.Trim());
sqlcmd.Parameters.AddWithValue("TimeStamp",System.DateTime.Now.ToString());
5. Run this SQL UPDATE statement in SQL Management Studio to ensure there are no extraneous spaces and all email addresses are in all lower-case.
UPDATE company SET email=LOWER(LTRIM(RTRIM(email)))

1 solution

Use ExecuteScalar to run something like this:
C#
using (SqlCommand cmd = new SqlCommand("SELECT COUNT(*) FROM MyTable WHERE EmailColumn=@EM", con)
   {
   cmd.Parameters.AddWithValue("@EM", emailTheUserTyped);
   int count = (int) cmd.ExecuteScalar();
   if (count != 0)
      {
      // It's in there...
      }
   }
 
Share this answer
 
Comments
Faysel Kedir 26-Dec-15 2:49am    
Appreciate all the support and help from all of you, it meant a lot!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900