Click here to Skip to main content
13,192,426 members (54,474 online)
Rate this:
Please Sign up or sign in to vote.

Can anyone tell me about the error Object reference not set to an instance of an object.

In my application I am getting this error while inserting records into the database.

Here is my Data layer class.
In data layer it is throwing the error in Throw block.

 public int Insert(string _Type, string _Name, string _Address, string Postal_Address, string _Phone, string _Fax)
            SqlConnection con = new SqlConnection(constr);
            SqlCommand cmd = new SqlCommand("usp_proc", con);
            cmd.CommandType = CommandType.StoredProcedure;
                string Docket_ID;
                cmd.Parameters.AddWithValue("@_Type", _Type);
                cmd.Parameters.AddWithValue("@_Name", _Name);
                cmd.Parameters.AddWithValue("@_Address", _Address);
                cmd.Parameters.AddWithValue("@Postal_Address", Postal_Address);
                cmd.Parameters.AddWithValue("@_Fax", _Fax);
                SqlParameter p = new SqlParameter("@Emp_ID", SqlDbType.Int);
                p.Direction = ParameterDirection.Output;
                Emp_ID = p.Value.ToString();
                return cmd.ExecuteNonQuery();

Please Let me know if my question asking is not clear to solve.
Thank you,
Posted 19-Jun-12 23:48pm
Agustee 20-Jun-12 7:36am
pls check whether you passed the value for all the parameters
ryanb31 20-Jun-12 8:06am
Just step through the code and see what is causing it.

1 solution

Rate this: bad
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.
VJ Reddy 20-Jun-12 22:29pm
Good answer. 5!
Manas Bhardwaj 21-Jun-12 4:56am
correct +5

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.171017.2 | Last Updated 20 Jun 2012
Copyright © CodeProject, 1999-2017
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