Problem with SqlConnection

Leif Nyström 21 Reputation points
2021-10-01T22:08:24.603+00:00

I have a problem that i cant understand since I have use the same code before and the same database.
I get an error message telling me "System.NullReferenceException: 'Object reference has not been given to an instance of an object.'
I have tried to find solutions, but nothing is working. What am I doing wrong?

I would very much appreciate you proffessional view on this.

The code where I get the problem looks like this:

Row 155: Dim DBCon As SqlConnection = New SqlConnection(" Data Source = DESKTOP - 97B62HO;Initial Catalog=gameBase;User ID=Leffeq;Password=Password1;")

I can connect to the database through the SQL Server Object Explorer with the same credentials.

System.NullReferenceException: 'Object reference has not been given to an instance of an object.'
at System.Data.SqlClient.SqlConnection.ConnectionString_Set(DbConnectionPoolKey key)
at System.Data.SqlClient.SqlConnection.set_ConnectionString(String value)
at System.Data.SqlClient.SqlConnection..ctor(String connectionString, SqlCredential credential)
at System.Data.SqlClient.SqlConnection..ctor(String connectionString)
at myNewWorld.Form1.Form1_Painta(Object sender, PaintEventArgs e)
at C:\Users\dator\source\repos\myNewWorld\Form1.vb:row 155
at System.Windows.Forms.Control.OnPaint(PaintEventArgs e)
at System.Windows.Forms.Form.OnPaint(PaintEventArgs e)
at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
at System.Windows.Forms.Control.WmPaint(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,696 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Erland Sommarskog 100.9K Reputation points MVP
    2021-10-02T09:23:01.207+00:00

    That looks like a problem with the connection pool. That is, some internal error that can be difficult to track down.

    However, there are two things in the connection string that looks suspect (well, three, if we count the password, but I take for granted that it's fake):

    • The leading space.
    • The space in the computer name.
      Try using the SqlConnectionStringBuilder to build the connection string and see if that helps.
    0 comments No comments

  2. CathyJi-MSFT 21,086 Reputation points Microsoft Vendor
    2021-10-04T01:25:15.067+00:00

    Hi @Leif Nyström ,

    > Data Source = DESKTOP - 97B62HO;Initial Catalog=gameBase;User ID=Leffeq;Password=Password1;

    Is this a default SQL server instance? Please make sure the server name is spelled correct. Does the server name has two space? If this is a named instance, you need to add that like Server=node1\SQL2019.

    Did you try to connect SQL DB via an IP address?

    connetionString="Data Source=IP_ADDRESS,PORT; Initial Catalog=DatabaseName;User ID=UserName;Password=Password"  
    
    0 comments No comments