Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.[]

VB
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

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
 
Share this answer
 
Comments
Abhinav S 19-Jan-13 1:47am    
Valid point.
Sergey Alexandrovich Kryukov 19-Jan-13 17:30pm    
Thank you, Abhinav.
—SA
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.
 
Share this answer
 
Comments
roxtarali 18-Jan-13 10:35am    
Yeah you was right it i forget to con.close instead of dr.close :P well thanks :)
Abhinav S 18-Jan-13 11:37am    
You are welcome.
fjdiewornncalwe 18-Jan-13 11:29am    
My 5.
Abhinav S 18-Jan-13 11:37am    
Thank you.
Espen Harlinn 18-Jan-13 11:46am    
5'ed!

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