Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
i'm getting this error while am running this code below "Object reference not set to an instance of an object."
 
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 13-Feb-13 6:42am
Edited 13-Feb-13 7:13am
Maciej Los150.8K
v4
Comments
richcb at 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.
Marcus Kramer at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
Maciej Los at 13-Feb-13 13:04pm
   
Short and to the point! +5!
Marcus Kramer at 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 at 12-Mar-13 17:54pm
   
5'ed!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
Comments
Espen Harlinn at 12-Mar-13 17:55pm
   
5'ed!
Sergey Alexandrovich Kryukov at 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)



Advertise | Privacy | Mobile
Web01 | 2.8.1411022.1 | Last Updated 12 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100