0

For some odd reason, my while loop is being completely ignored. The breaking point shows that it at least is getting to it, but not even attempting to go within. It's probably something simplistic that I'm missing.

namespace website
{
public partial class Account : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Login_Click1(object sender, EventArgs e)
    {
        pullfrom();
    }



    private void pullfrom()
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = @"Data Source=WSERV2012CNTRLR\SQLEXPRESS,1433; Initial Catalog=Dackup; User ID=user; Password=password"; 
        string username = user.Text.ToString();
        string password = pass.Text.ToString(); //Clients username and password     
        try
        {
            con.Open();
        }

         catch
        {
            correct.Text = "Not able to connect. Please try again in 5 minutes".ToString();
            correct.Visible = true; 
        }

        SqlCommand command = new SqlCommand("Select * from dackupinfo where username=@username and password=@password;", con);
        command.Parameters.Add("@username", SqlDbType.VarChar).Value = username;
        command.Parameters.Add("@password", SqlDbType.VarChar).Value = password;
        SqlDataReader reader = command.ExecuteReader();
        while(reader.Read()) // Continues without going into the while loop.
        {

            // Console.WriteLine(reader["susername"].ToString());
            // Console.WriteLine(reader["spassword"].ToString()); //sftpserver/username
            string finder = reader["username"].ToString();
            if (finder == null)
            {
                correct.Text = "Incorrect Credentials".ToString(); // Need to look into; as I can't get it to work. 
                correct.Visible = true;

            }
            if (finder != null)
            {
                Response.Redirect("UserCenter.aspx");

            }
            reader.Close();
        }
        con.Close();


    }


}

}`

Vash
  • 332
  • 3
  • 14
  • 2
    Isn't it simple. Read returns false meaning no item to read. Check your sql statement & and make sure that data does exist in your DB. – I4V Aug 24 '13 at 23:48
  • It's very simple. Just wasn't thinking in the terms of a null reference not being read. – Vash Aug 25 '13 at 00:32

1 Answers1

0

Fixed. Here's the correct code.

         SqlDataReader reader = command.ExecuteReader();
        if (reader.Read())
        {
            if (reader != null)
            {
                Response.Redirect("UserCenter.aspx");
            }
        }
            else
            {
                correct.Text = "Incorrect Credentials".ToString(); 
                correct.Visible = true;
            }

        reader.Close();
        con.Close();
Vash
  • 332
  • 3
  • 14
  • 1
    OP's are not allowed to post answers generally. Yes there is a condition, that If you were bugged heavily by the issue and few of us were unable to resolve the issue, and you are about to realize that it is not going to an easy task and anyhow you resolve the issue.. in that case to help others , you can post your solution as answer.. –  Aug 25 '13 at 04:27