Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
I am using this code in my button event. But i am receiving this error Object reference not set to an instance of an object.[]
 
Try
            Dim cmd As SqlCommand
            Dim con As SqlConnection = New SqlConnection("Data Source=ALI-PC\SQLEXPRESS;Initial Catalog=estate_portal;Integrated Security=True")
            Dim sqlstring As String
            sqlstring = "INSERT INTO [Proprties] ([Address], [Price], [Size], [Beds], [Bath], [Status], [Opening_Status], [Image], [PostedBy]) VALUES ('" + Textbox4.Text + "', '" + Textbox5.Text + _
                "', '" + Textbox5.Text + "', '" + Textbox5.Text + "', '" + Textbox5.Text + "', '" + Textbox5.Text + "', '" + Textbox5.Text + "', '" + Textbox5.Text + "', '" + Textbox5.Text + "')"
            con.Open()
            cmd = New SqlCommand(sqlstring, con)
            cmd.ExecuteNonQuery()
            Errors.Text = ("Success")
            dr.Close()
        Catch ex As Exception
            Errors.Text = (ex.Message)
        End Try
Posted 18-Jan-13 4:23am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

This is one of the easiest exceptions 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.
 
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"[^]
 
—SA
  Permalink  
Comments
Abhinav S at 19-Jan-13 1:47am
   
Valid point.
Sergey Alexandrovich Kryukov at 19-Jan-13 17:30pm
   
Thank you, Abhinav.
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try debugging your source code. You will find you are accessing a property or method on an object that is null.
In this case looks like you have not instantiated the variable dr.
  Permalink  
Comments
roxtarali at 18-Jan-13 10:35am
   
Yeah you was right it i forget to con.close instead of dr.close :P well thanks :)
Abhinav S at 18-Jan-13 11:37am
   
You are welcome.
Marcus Kramer at 18-Jan-13 11:29am
   
My 5.
Abhinav S at 18-Jan-13 11:37am
   
Thank you.
Espen Harlinn at 18-Jan-13 11:46am
   
5'ed!
Abhinav S at 18-Jan-13 12:03pm
   
Thank you Espen.
Sergey Alexandrovich Kryukov at 18-Jan-13 19:40pm
   
Correct, my 5, but I have some extra advice on the topic; please see Solution 2.
—SA
Abhinav S at 19-Jan-13 1:46am
   
Thank you.

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



Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 18 Jan 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