Click here to Skip to main content
13,005,920 members (95,302 online)
Rate this:
Please Sign up or sign in to 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.[]

            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 + "')"
            cmd = New SqlCommand(sqlstring, con)
            Errors.Text = ("Success")
        Catch ex As Exception
            Errors.Text = (ex.Message)
        End Try
Posted 18-Jan-13 4:23am
Rate this: bad
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"[^]

Abhinav S 19-Jan-13 1:47am
Valid point.
Sergey Alexandrovich Kryukov 19-Jan-13 17:30pm
Thank you, Abhinav.
Rate this: bad
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.
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.
Marcus Kramer 18-Jan-13 11:29am
My 5.
Abhinav S 18-Jan-13 11:37am
Thank you.
Espen Harlinn 18-Jan-13 11:46am
Abhinav S 18-Jan-13 12:03pm
Thank you Espen.
Sergey Alexandrovich Kryukov 18-Jan-13 19:40pm
Correct, my 5, but I have some extra advice on the topic; please see Solution 2.
Abhinav S 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

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