Dominik H Dominik H - 4 years ago 99
SQL Question

Search for a value from column one, and return value from column two on the same row in VB.NET

So, the user inputs their email in case they forgot their password, the input is taken into a query where it selects the place where Email is equal to the email that is inputted by the user, but how do I also get the password value in column two to return to the user?

The code I'm using is this:

Try
If Not dbconn Is Nothing Then dbconn.Close()
dbconn.ConnectionString = String.Format("server={0}; port=3306; user id={1}; password={2}; database={3}; pooling=true", server, userName, password, DatabaseName)

dbconn.Open()

query = String.Format("SELECT * FROM Testers WHERE Email='" + forgotEmail + "'")
Try
dbcomm = New MySqlCommand(query, dbconn)
dbread = dbcomm.ExecuteReader()
While dbread.Read()

End While
dbread.Close()

Catch ex As Exception
MsgBox(ex.ToString)
End Try
Catch ex As Exception
MsgBox(ex.ToString)
End Try


But what do I do after the query point?

Answer Source

Try this:

   String password;

   While dbread.Read()

       password = reader.GetString(reader.GetOrdinal("password_column"))
       'Do something with password

   End While

Of course, the while loop should only execute once in this case (i.e. there should only be one record containing that email address).

You can also reduce this:

query = String.Format("SELECT * FROM Testers WHERE Email='" + forgotEmail + "'")

to

query = String.Format("SELECT password_column FROM Testers WHERE Email='" + forgotEmail +"'")

where password_column is the name of that field.


Of course, when working with passwords, one should always be extremely careful. Also check out Plutonix' comment!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download