DataSet
is composed of
DataTables
so you need to specify the table like this:
ds.Tables["TableName"].Count > 0
or using index like this:
ds.Tables[0].Count > 0
While what works, I would still recommend you to
DataTable
instead of
DataSet
since you are only dealing with one database table. Also, make it a habit to put
objects
that eat resources such as
SqlConnection
,
SqlCommand
and
SqlDataAdapter
within a using statement to ensure that objects will be properly disposed and closed after they are used. Here's a quick example:
public DataTable Userlogin(Users objlogin){
DataTable dt = new DataTable();
string sqlStatement = "SELECT * FROM User_details WHERE [Login Id]=@loginid AND [Password]=@password";
using(SqlConnection connection = new SqlConnection(GetConnectionString())){
using(SqlCommand cmd = new SqlCommand(sqlStatement ,connection)){
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@loginid", objlogin.loginId);
cmd.Parameters.AddWithValue("@password", objlogin.Password);
using(SqlDataAdapter da = new SqlDataAdapter(cmd)){
da.Fill(dt);
}
}
}
return dt;
}
Then in your Business layer, you would need to return a DataTable too so you can do something like this in your UI layer:
BAL objBAL = new BAL();
DataTable dt = objBAL.Userlogin(objuser);
if(dt.Rows.Count > 0)
{
Session["User"] = loginid.Text;
Response.Redirect("~/Transactions.aspx");
}
else
{
Response.Write("<script>alert('Invalid Credentials!')</script> ");
}
Note that we're now using
dt.Rows.Count
to check if your query returns any data.