Click here to Skip to main content
15,896,541 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

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.

C#
public int Insert(string _Type, string _Name, string _Address, string Postal_Address, string _Phone, string _Fax)
       {
           SqlConnection con = new SqlConnection(constr);
           con.Open();
           SqlCommand cmd = new SqlCommand("usp_proc", con);
           cmd.CommandType = CommandType.StoredProcedure;
           try
           {
               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("@_Phone",_Phone);
               cmd.Parameters.AddWithValue("@_Fax", _Fax);
            ;

               SqlParameter p = new SqlParameter("@Emp_ID", SqlDbType.Int);
               p.Direction = ParameterDirection.Output;
               cmd.Parameters.Add(p);
               Emp_ID = p.Value.ToString();
               return cmd.ExecuteNonQuery();

           }
           catch
           {
               throw;
           }
           finally
           {
               cmd.Dispose();
               con.Close();
               con.Dispose();
           }
       }


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

1 solution

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
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)



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