Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i'm getting this error while am running this code below "Object reference not set to an instance of an object."

C#
con.Close();
                con.Open();
                String ps = EncodePassword(txtPaswrd.Text);
                SqlDataAdapter da = new SqlDataAdapter("select * from employee where Email='" +txtEmail.Text+ "' and poste='"+txtposte.Text+"' and password='" + ps + "'", con);
                DataSet ds = new DataSet();
                da.Fill(ds, "users");
                DataTable dt = ds.Tables["users"];
Posted
Updated 13-Feb-13 6:13am
v4
Comments
Richard C Bishop 13-Feb-13 11:46am    
You will need to post the rest of the code. There are 4 variables there that we cannot see the declaration and assigning of values to.
fjdiewornncalwe 13-Feb-13 12:47pm    
1) When using your debugger, which line does the error occur on?
2) Please use paramaterized queries. The syntax you are using is wide open for sql injection attacks.
3) Without more information there isn't a whole lot we can do for you.

Object reference not set to an instance of an object

This error happens when you try to use a property or call a method of an object that is null. More details: here[^]

A simple use of Visual studio DEBUGGER can tell you the object because of which it is happening. Just look at the stack trace and put a debugger on that line. Check the objects of that line and see if any one is null and you are trying to use that objects property. Handle the same.
 
Share this answer
 
Comments
Maciej Los 13-Feb-13 13:04pm    
Short and to the point! +5!
fjdiewornncalwe 13-Feb-13 16:03pm    
My 5. I couldn't have been bothered to provide that much answer in this case as the OP is clearly just to lazy to use google. Cheers.
Espen Harlinn 12-Mar-13 17:54pm    
5'ed!
You did not show where the exception with the message "Object reference not set to an instance of an object" is thrown.

Not to worry. This is one of the very easiest cases to detect and fix. It simply means that some member/variable of some reference type is dereferenced by using and of its instance (non-static) members, which requires this member/variable to be non-null, but in fact it appears to be null. Simply execute it under debugger, it will stop the execution where the exception is thrown. Put a break point on that line, restart the application and come to this point again. Evaluate all references involved in next line and see which one is null while it needs to be not null. After you figure this out, fix the code: wither make sure the member/variable is properly initialized to a non-null reference, or check it for null and, in case of null, do something else.

Please see also: want to display next record on button click. but got an error in if condition of next record function "object reference not set to an instance of an object"[^].

Good luck,
—SA
 
Share this answer
 
Comments
Espen Harlinn 12-Mar-13 17:55pm    
5'ed!
Sergey Alexandrovich Kryukov 12-Mar-13 18:00pm    
Thank you, Espen.
—SA

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