question

LeifNystrm-1147 avatar image
0 Votes"
LeifNystrm-1147 asked Cathyji-msft commented

Problem with SqlConnection

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-general
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


Is this a new ".NET Core" or traditional ".NET Framework" application?


0 Votes 0 ·

Hi @LeifNystrm-1147,

Any update for this thread? Just checking in to see if the answer(s) helped. If not, please let us know.

0 Votes 0 ·
ErlandSommarskog avatar image
0 Votes"
ErlandSommarskog answered

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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Cathyji-msft avatar image
0 Votes"
Cathyji-msft answered

Hi @LeifNystrm-1147,

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"
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.