A
Assimalyst
Hi,
I'm relatively new to ADO, so forgive me if I am going about this in
the wrong way.
I have a login.aspx page, with a textbox 'usernameTxtBx'
I would like to extract a value from a single table in an SQL database.
The database has two columns of interest usrNo and usrName, they are in
the same table.
I would like to set the value from usrNo as an int where the value in
the usrName column matches that entered in the usernameTxtBx.
I have attempted to use a datareader in c#. Here's the relevent code:
// Declare conn from Web.Config
SqlConnection conn = new
SqlConnection(ConfigurationSettings.AppSettings["strConn"]);
conn.Open();
// Set username as a string
string Username = usernameTxtBx.Text;
//find usrNo
SqlCommand cmd = new SqlCommand("SELECT usrNo FROM tblUser WHERE
usrName = 'Username'", conn);
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
int UserNo = (int)dr["UserNo"];
conn.Close();
This gives the following error:
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.
Exception Details: System.IndexOutOfRangeException: UserNo
In debugging the value of UserNo appears to get set to 0 regardless of
the login used, where i would expect the value to equal 1 or 3 or
similar integer.
Any ideas what i'm doing wrong? am I even on the right track??
Thanks
I'm relatively new to ADO, so forgive me if I am going about this in
the wrong way.
I have a login.aspx page, with a textbox 'usernameTxtBx'
I would like to extract a value from a single table in an SQL database.
The database has two columns of interest usrNo and usrName, they are in
the same table.
I would like to set the value from usrNo as an int where the value in
the usrName column matches that entered in the usernameTxtBx.
I have attempted to use a datareader in c#. Here's the relevent code:
// Declare conn from Web.Config
SqlConnection conn = new
SqlConnection(ConfigurationSettings.AppSettings["strConn"]);
conn.Open();
// Set username as a string
string Username = usernameTxtBx.Text;
//find usrNo
SqlCommand cmd = new SqlCommand("SELECT usrNo FROM tblUser WHERE
usrName = 'Username'", conn);
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
int UserNo = (int)dr["UserNo"];
conn.Close();
This gives the following error:
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.
Exception Details: System.IndexOutOfRangeException: UserNo
In debugging the value of UserNo appears to get set to 0 regardless of
the login used, where i would expect the value to equal 1 or 3 or
similar integer.
Any ideas what i'm doing wrong? am I even on the right track??
Thanks