Data duplicate verification not working.

Maxicus

New member
Joined
Apr 4, 2019
Messages
2
Programming Experience
Beginner
Good Day everyone and thank you in advance.

I am developing a registration webform and having some problem with the code to verify if an e-mail address already exists, before it saves to the database.

this is what i have so far:
C#:
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = @"Data Source =(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\MAIN.mdf;Integrated Security=True";
        conn.Open();

        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "select * from [Users]";
        cmd.Connection = conn;

        SqlDataReader rd = cmd.ExecuteReader();
        while (rd.Read())
        {
            if (rd[2].ToString() == TextBox2.Text)
            {
                flag = true;
                break;
            }
        }
        if (flag == true)
            Label1.Text = "find";
        else
            Label1.Text = "nof find";

    }

518
519


When you click register, the code is suppose to search the database for the same email in the textbox (TextBox2) and if the same e-mail address is found change the Label(Label1) text to Find. If not it will specify that it is "not find" and register the user into the database (that code i still need to write).

The problem I have is that it always specify that the email address is "not find" even if it is in the database.

I have tried changing the folllowing:
if (rd[1].ToString() == TextBox2.Text)
if (rd[2].ToString() == TextBox2.Text)
if (rd[3].ToString() == TextBox2.Text)

but the label remains to change to "Not Find".

I am very new to C# and visual studio. only started self learning this month. (just mentioning it.)

Thanks again
 
I was able to rewrite the code to something else that does work, however with this code I am unable to verify the e-mail addresses, but it works fine to validate the username.

C#:
    protected void Button1_Click(object sender, EventArgs e)
    {
        using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MainConnectionString"].ConnectionString))
        {
            con.Open();
            bool exists = false;
            // create a command to check if the username exists
            using (SqlCommand cmd = new SqlCommand("select count(*) from [Users] where Username = @Username", con))
            {
                cmd.Parameters.AddWithValue("Username", TextBox1.Text);
                exists = (int)cmd.ExecuteScalar() > 0;
            }

            // if exists, show a message error
            if (exists)
            {
                Label1.Visible = true;
                Label2.Visible = false;
            }
            else
            {
                try
                {
                    string insertQuery = "insert into Users(Username,Email,Password) values(@Uname,@Email,@Password)";
                    SqlCommand com = new SqlCommand(insertQuery, con);
                    com.Parameters.AddWithValue("@Uname", TextBox1.Text);
                    com.Parameters.AddWithValue("@Email", TextBox2.Text);
                    com.Parameters.AddWithValue("@Password", TextBox3.Text);
                    com.ExecuteNonQuery();
                    Response.Redirect("2.Manager.aspx");
                    Label2.Visible = true;
                    Label1.Visible = false;
                    Label2.Text = "New user has been added";
                    con.Close();
                }
                catch (Exception ex)
                {
                    Response.Write("error:" + ex.ToString());
                }
                con.Close();
            }
        }
    }
}
 
If you want to count records that satisfy one or both or two conditions then add both conditions to your WHERE clause and separate them with an OR operator. The WHERE clause is basically just an if statement: if a record satisfies the following expression, include it in the result set.
 
Back
Top Bottom