0

I have a registration form that I am using to register new user. No problem creating the username (email) and password and inserting it mysql DB. I am however wondering how would I compare the value inserted into the textbox with value of a column called Email.

Example I insert the email into textbox: [email protected] and hit Next button that connects to mysql DB. I want to compare values and if Email: [email protected] exist in DB table, let user know! Is this possible ?

Thanks

1
  • Is there some reason you can't just run a select against the DB prior to attempting the insert, and if any matching rows are returned, give the user the "Already exists" message back? Commented Jan 14, 2013 at 19:49

2 Answers 2

1

First Step:

Create Procedure FindString(
@MyString nvarchar(50))
As
Begin
Select * From MyTable
Where Value = @MyString
End

Make a class:

public class ReadData
{
    public bool FindString(string myString)
    {
        SqlConnection connection = new SqlConnection();
        connection.ConnectionString = "Server=..."; //Your connection string
        SqlCommand command = new SqlCommand();
        command.Connection = connection;
        command.CommandType = CommandType.StoredProcedure;
        command.CommandText = "FindString";
        command.Parameters.AddWithValue("@MyString", myString);
        try
        {
            connection.Open();
            SqlDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                return true;
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            if (connection.State == ConnectionState.Open)
                connection.Close();
        }                
        return false;
    }
}

Use the class. for example :

ReadData r = new ReadData();

if (r.FindString("Shahingg"))
    MessageBox.Show("I Found it!");
else
    MessageBox.Show("I can't Find it!");
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I actually found it shortly after. Your example works as well!
0

I found the answer:

 Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button7.Click

    con = New MySqlConnection("Database=;" & _
                            "Data Source=;" & _
                            "User Id=;Password=;")


    con.Open()
    Try

        Query = "SELECT Email FROM users WHERE Email='[email protected]'"

        cmd = New MySqlCommand(Query, con)

        reader = cmd.ExecuteReader()

        If reader.HasRows Then
            MessageBox.Show("Email taken")
            '  While reader.Read
            'MysqlData.Text = MysqlData.Text & reader.Item("Email")
            ' End While
        Else
            MessageBox.Show("Email does not exist")
        End If
    Catch ex As Exception


    End Try

End Sub

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.