Reputation: 997
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
Upvotes: 0
Views: 1619
Reputation: 997
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
Upvotes: 0
Reputation: 1900
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!");
Upvotes: 1