I am trying to build a login screen using a data reader. For some reason it is not returning rows when in fact there are rows in the database. I have confirmed this using SQL Server Managerment Studo Express. Can anyone tell me what I am missing?
Here is my code:
private String ConnString
{
get
{
//HDS ConnectionString comes from the Web.Config
return ConfigurationManager.Conne
ctionStrin
gs["zxcCon
nectionStr
ing"].Conn
ectionStri
ng;
}
}
protected void btnLogin_Click(object sender, Infragistics.WebUI.WebData
Input.Butt
onEventArg
s e)
{
Boolean valid;
Boolean npi;
SqlDataReader reader = null;
SqlConnection con = null;
SqlCommand cmd = null;
String physicianNPI = "";
try
{
//Open Connection to the database
con = new SqlConnection(ConnString);
// Set up a command with the given query and associate this with the current connection
string CommandText = "SELECT PHYSICIAN_NPI FROM PROVIDER WHERE USERNAME = @username AND PASSWORD = @password";
cmd = new SqlCommand(CommandText);
con.Open();
cmd.Connection = con;
cmd.Parameters.Add("@usern
ame", SqlDbType.VarChar).Value = tbUserName.Text;
cmd.Parameters.Add("@passw
ord", SqlDbType.VarChar).Value = tbPassword.Text;
reader = cmd.ExecuteReader();
valid = false;
string test;
test =reader.GetString(0);
while (reader.Read())
{
valid = true;
physicianNPI = reader.GetString(0);
}
if (valid)
{
Session.Add("physicianNPI"
, physicianNPI);
Response.Redirect("Portal.
aspx");
}
else
{
lblLoginError.Text = "Invalid Username/Password";
}
}
finally
{
// Close data reader object and database connection
if (reader != null)
reader.Close();
if (con.State == ConnectionState.Open)
con.Close();
}
}
Start Free Trial