Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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.
 
 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 19-Jun-12 23:48pm
Comments
Agustee at 20-Jun-12 7:36am
   
pls check whether you passed the value for all the parameters
ryanb31 at 20-Jun-12 8:06am
   
Just step through the code and see what is causing it.

1 solution

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
VJ Reddy at 20-Jun-12 22:29pm
   
Good answer. 5!
Manas Bhardwaj at 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
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 860
1 OriginalGriff 427
2 Abhinav S 303
3 thatraja 225
4 CPallini 208
0 Sergey Alexandrovich Kryukov 8,122
1 OriginalGriff 4,520
2 Peter Leow 3,744
3 Maciej Los 3,515
4 Er. Puneet Goel 3,107


Advertise | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 20 Jun 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid