I am using Visual Studio 2008 to create a windows app. I want to create a login form in a disconnected manner. I am facing a problem that when I am trying to check the DataTable is filled or not it is showing that it is filled always.
Code for login button:
try
{
//getting connectionString
mcc = new myConnectionClass();
conString = mcc.MyConnection;
//opening connection con
con = new SqlConnection(conString);
string queryString = "select * from LOGIN_TABLE where LOGIN_ID = @LOGIN_ID and LOGIN_PASSWORD = @LOGIN_PASSWORD";
//dataset ds , SqlDataAdapter sda
ds = new DataSet();
sda = new SqlDataAdapter();
//sqlcommand
cmd = new SqlCommand(queryString, con);
cmd.Parameters.Add(new SqlParameter("LOGIN_ID", txtLogin.Text));
cmd.Parameters.Add(new SqlParameter("LOGIN_PASSWORD", txtPass.Text));
sda.SelectCommand = cmd;
sda.Fill(ds, "LOGIN_TABLE");
//datatable dt
dt = ds.Tables["LOGIN_TABLE"];
//checking is datatable is empty
if (dt != null)
{
MessageBox.Show("Login Successed");
}
else
{
MessageBox.Show("Login Failed");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
ds.Clear();
dt.Clear();
}
Your error lies in the check for
it’s allways true if your LOGIN_TABLE exist
you need to check for number of rows.
Let me say also that, from a security point of view, storing login e password on a database is not a good idea.