Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

comparing textbox values with dbvalues

Status
Not open for further replies.

xorl

Programmer
Aug 8, 2006
10
SE
hey

I have another problem and it *will* be the last....

when i try to compare values from the textboxes and the values from the database i get....nothing. Not sure if it has something todo with my SELECT statement.

============================================

using System;
using System.Data;
using System.Web.UI.WebControls;
using System.Net.Sockets;
using MySql.Data;
using MySql.Data.MySqlClient;

namespace FrmLogin
{

public class SqlConn
{
private MySqlConnection sqlconn;
private MySqlCommand sqlcmd;
private string connstr;

public SqlConn(string server, string userid, string password)
{
connstr = String.Format("server={0}; userid={1}; password={2}; database=USERS", server, userid, password);


}

public void Connect()
{
if (null != sqlconn)
{
sqlconn.Close();
}
try {
sqlconn = new MySqlConnection(connstr);
sqlconn.Open();
} catch (MySqlException sqlex) {
throw new Exception("Couldn't connect to server: " +sqlex.Message);
} catch (SocketException sex) {
sex = null;
throw new Exception("Check your connection to the database....");
}
}


public void Auth(TextBox Usrname, TextBox Password)
{
string syntax = "SELECT username FROM info WHERE username=@username";
MySqlDataReader daread = null;
sqlcmd = new MySqlCommand(syntax, sqlconn);
daread = sqlcmd.ExecuteReader();

/* MySqlParameter going;

going = sqlcmd.Parameters.Add("@username", MySqlDbType.VarChar);
going.Direction = ParameterDirection.Input;
going.Value = Usrname.Text; */
try
{

while (daread.Read())
{
if (going.Value == Usrname.Text)
{
Password.Text = "........";
} else {
Password.Text = "not right";
}
}

} catch(MySqlException ex) {
throw new Exception(ex.Message);
} finally {
if (daread != null) daread.Close();
}
}


}
}

=========================================

this was my last attempt. i can get the values normally through the sqlcmd.GetString(0) and that works but i cant compare even with mysqlparameters :/.
 
are you certain that going.Value is a string?y converting it to a string when you compare

Code:
    while (daread.Read()) 
    {      
           if (going.Value.ToString() == Usrname.Text)
           {
                   Password.Text = "........";
           }  else {
                   Password.Text = "not right";
           }
    }

You could always tweak your SQL statement so you don't have to do any comparisions.


Code:
"SELECT count(*) FROM info WHERE username=@username and password = @password";


Of course you need to add a parameter for password. Then if the count returned is > 0, it's a success.

 
going.value.tostring() doesnt work so im gonna make some changes in the code tomorrow. going to use stored procedures more and implement them in the textbox params.

The other 1 might just work but i have to do a comparison in the txtboxes also.

thanks. :)

xorl
 
sigh scratch that "implement them in the textbox params".
Sounds kinda strange hehehe.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top