2

while (reader.Read())
{
    if (TextBox1.Text.CompareTo(reader["usernam"].ToString()) == 0&&TextBox2.Text.CompareTo(reader["passwd"].ToString()) == 0) // A little messy but does the job to compare your infos assuming your using a textbox for username and password
    {
        Label3.Text = "Redirecting";



        Response.Cookies["dbname"]["Name"] = reader["usernam"].ToString();
        Response.Cookies["dbname"].Expires = DateTime.Now.AddSeconds(10);
        Response.Redirect("index2.aspx");

    }
    else
    { Label3.Text = "NO"; }

}

When i try to compare both username (usernam) and password (passwd) I get this error. If i only compare the username with the db entries it works like a charm.

It will only give the error when actual data is used. E.I. If i enter [admin], [admin] in the log in web page it will give me the error, if i enter [asd], [asd] then the label would change to NO.

The idea behind the code is a log in page. I hope my explanation is good enough.

1
  • 1
    Please add the actual code to your question as text. Commented May 7, 2013 at 10:56

2 Answers 2

4

You are only selecting the username from your table. You are not selecting the password, hence it throws an exception when you try to retrieve it from the result set.

Change the query to this:

string selectString = "SELECT usernam, passwd FROM Table1";
Sign up to request clarification or add additional context in comments.

Comments

1

You can also use * instead of column names in your query. Here its just a small scenario. If there are multiple columns then you just need to use * for that. That will make your query simple.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.