Click here to Skip to main content
15,914,416 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am getting error on my code given below(VS 2010). Error is "{"Object reference not set to an instance of an object."}. However the same code runs fine in VS 2008. Please help me. Thanks in advance.


C#
////DAL
 public DataTable LoadUserName()
    {
        DataTable dTable = null;

        try
        {
            con.Open();

            String selectStr = "select uCode,uID,uPassword from tUsers";
            SqlDataAdapter da = new SqlDataAdapter(selectStr, con);
            
            dTable = new DataTable();

            da.Fill(dTable);

            con.Close();   

        }
        catch (Exception e)
        {
            String Str = e.Message;
        }

        return dTable;
    }

///BAL


public bool authenticate(string username, string password)
    {
        LoginDAL dal = new LoginDAL();
        DataTable dTable = dal.LoadUserName();

        bool found = false;
        
        foreach (DataRow dRow in dTable.Rows)                 ///Getting error on this line
        {
            if ((username == dRow["uID"].ToString()) &&
                         password == dRow["uPassword"].ToString())
            {
                userID =Convert.ToInt32(dRow["uCode"].ToString());
                found = true;
            }
        }
        if
            (!found)
        {
            return false;
        }

        return found;

    }    
Posted
Comments
Anurag Sinha V 5-Apr-13 16:26pm    
Hey,
Put breakpoints in both the functions and try to debug at which point you're facing this error.
then you might understand where you're wrong..Try it
Anurag Sinha V 5-Apr-13 16:29pm    
ohh..My bad..didn't see that you have written 'Error on this line'...
close the connection in finally{} block and then try...
apurba001 5-Apr-13 18:08pm    
ok thank you

1 solution

This is really really inefficient code.
just for the sake of solution, your dTable has no rows set a breakpoint and have a look.
The connection.close might be the problem
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900