0

I have a database. I'm fetching two values from database in the sqlcommand, but one problem I'm facing is that how to get the adminstatus from the table in the reader()..

my code

protected void signin_Click(object sender, EventArgs e)
    {

    string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

    using (SqlConnection conn = new SqlConnection(connectionString))
    using (SqlCommand comm = new SqlCommand("SELECT password,adminstatus FROM usertable WHERE userid = @username", conn))
    {
        comm.Parameters.AddWithValue("@username", logintext.Text);

        conn.Open();

        using (SqlDataReader reader = comm.ExecuteReader())
        {
            if (reader.Read())
            {
                string password = reader.GetString(0);
                //int st =Convert.ToInt32(reader.GetString(1));
                //int st = reader.GetInt32(0);
                if (password == logpasstext.Text)
                {
                    Session["userid"] = logintext.Text;
                   // iframestyle.Attributes["src"] = "userpage.aspx";
                    iframestyle.Attributes["src"] = "userpage.aspx";
                    logdiv.Attributes["style"] = "display:none;";
                }
              /*  else if (password == logpasstext.Text && st == 1)
                {
                    Session["userid"] = logintext.Text;
                    // iframestyle.Attributes["src"] = "userpage.aspx";
                    iframestyle.Attributes["src"] = "adminpage.aspx";
                    logdiv.Attributes["style"] = "display:none;";
                }*/

                else
                {
                    errorsignin.Visible = true;
                    errorsignin.Text = "INVALID LOGIN";
                    logdiv.Attributes["style"] = "display:block;";
                }
            }
            else
            {
                errorsignin.Visible = true;
                errorsignin.Text = "INVALID LOGIN";
                logdiv.Attributes["style"] = "display:block;";
            }
        }
    }
}

i couldn't get the adminstatus from the table using reader...

2
  • 3
    Do not store passwords in plain text Commented Mar 12, 2014 at 15:09
  • What do you mean "couldn't get the adminstatus"? Are you getting an error? Are you getting a null? If the latter, are you sure that isn't the actual value in the database? Your commented out code looks fine (other than you should probably use reader.GetInt32(1) if you know it's an int). Commented Mar 12, 2014 at 15:10

3 Answers 3

1

Try using string indexer with SQLDataReader. Just specify the column name you want to retreive

 string adminstatus= Convert.ToString(reader["AdminStatus"]); 
// Assuming AdminStatus is a column name
Sign up to request clarification or add additional context in comments.

Comments

0

You can use reader.GetOrdinal(ColumnName) to avoid magic numbers.

I'd then go with

//if adminstatus is a SQL string like the other field
reader.GetString(reader.GetOrdinal("adminstatus")); // returns string

//if adminstatus is for example a SQL bit 
reader.GetBoolean(reader.GetOrdinal("adminstatus")); // returns bool

Comments

0
string adminStatus = reader.GetString(1)

where "1" is the position of the field in the query

or better :

string adminStatus = reader.GetString(reader.GetOrdinal("adminstatus"))

with reader.GetOrdinal("adminstatus") returning the position of the field in the query

You will have to replace "GetString" with the method corresponding to the data type (for example, GetInt32 or GetFloat or GetBoolean)

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader(v=vs.110).aspx

1 Comment

what do you mean by "not working" ? do you have an error message?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.