SQL Open Connection Error

itzkhurram

New member
Joined
Oct 9, 2007
Messages
2
Programming Experience
1-3
I want simple connect VB.net windos application form to SQL Server 2000 Database(pubs) but error msg appear every time when i run the form


Error indicate the line conn.open()
Error:
An unhandled exception of type 'System.NullReferenceException' occurred in system.data.dll

Additional information: Object reference not set to an instance of an object.
VB.NET:
Expand Collapse Copy
Imports System.Data.SqlClient

Public Class Form1
    Inherits System.Windows.Forms.Form
    Dim conn As SqlConnection

#Region " Windows Form Designer generated code "

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        conn = New SqlConnection("server=(local);database=pubs;uid=sa;pwd=sa;")
        conn.Open()   *******  there is error 
    End Sub
End Class
 
Last edited by a moderator:
I can't see why you would be getting a null reference exception there. As long as you have a created an instance of the SQLConnection class, which you have.

The only thing I can think of is that the code must be erroring out in the constructor of the SQLConnection class and the instance isn't being created. Check the connection string. But to be honest the error states that conn is equal to nothing, which it shouldn't be based on your code.

Set a breakpoint and hover the cursor over the Conn variable as you step through the code you'll be able to see if it is actually assigned to nothing or whether the problem is elsewhere.
 
Back
Top