-1

I'm totally new in C#. I'm having troubles with connecting my application with a SQL Server database, the problem occurs when I click the button which checks or updates my database.

I tried with 2 connection string methods I found on internet but neither of them work. Now maybe I understood it wrongly. I think it could be the SERVER part of code or the other version Data Source where I made a mistake but not quite sure. Basically I've put my VPS' IP address there (which I use in nodejs to connect to database as well).

Those are my 2 connection string methods I used which doesn't work:

const string connectionString = "SERVER = **.***.76.9; DATABASE = dbName; USER ID = root; PASSWORD =myPassword";

const string connectionString = "Data Source=**.**.76.9; Initial Catalog = dbName; User ID = root; Password=myPassword";

I'm also using sqlConn.Open(); to open connection which causes the error as I mentioned above.

System.Data.SqlClient.SqlException
HResult=0x80131904

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

Source=.Net SqlClient Data Provider
StackTrace:
at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, DbConnectionPool pool, String accessToken, Boolean applyTransientFaultHandling, SqlAuthenticationProviderManager sqlAuthProviderManager) at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection) at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection) at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource1 retry) at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource1 retry) at System.Data.SqlClient.SqlConnection.Open() at myProject.Forms.activation.isActivated(String key) in C:\Users\PC\Desktop\myProject\WindowsClient\myProject\Forms\activation.cs:line 24 at myProject.Forms.activation.activateSoftware(String key) in C:\Users\PC\Desktop\myProject\WindowsClient\myProject\Forms\activation.cs:line 35 at myProject.Forms.LoginForm.button1_Click(Object sender, EventArgs e) in C:\Users\PC\Desktop\myProject\WindowsClient\myProject\Forms\LoginForm.cs:line 100 at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.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.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.Run(Form mainForm) at myProject.Program.Main() in C:\Users\PC\Desktop\myProject\WindowsClient\myProject\Program.cs:line 21

Inner Exception 1:
Win32Exception: The network path was not found

This is my code:

    public static bool isActivated(string key)
    {
        using (SqlConnection sqlConn = new SqlConnection(connectionString))
        {
            string checkForActivationQuery = "SELECT activated FROM activationTable WHERE serialKey =@key";

            SqlCommand cmd = new SqlCommand(checkForActivationQuery, sqlConn);
            cmd.Parameters.AddWithValue("@key", key);

            sqlConn.Open();

            int result = Convert.ToInt32(cmd.ExecuteScalar());

            if (result > 0)
            {
                return true;
            }

            return false;
        }
    }
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Onyx
  • 15
  • 6
  • 1
    `The network path was not found` Sounds like the path doesn't work from your desktop. – LarsTech Jul 07 '22 at 22:08
  • @LarsTech sorry, what does that means exactly? What should I do to fix it? – Onyx Jul 07 '22 at 22:09
  • The following may be helpful: https://stackoverflow.com/a/71199793/10024425 – Tu deschizi eu inchid Jul 07 '22 at 22:39
  • @user9938 but I am using database from my Virtual Private Server, not localhost – Onyx Jul 07 '22 at 22:44
  • It seems that you may not be the one that manages the VPS. If not, you'll need to get the relevant information from the person who manages it. In your post it's not clear where your application is running from. Is it running on the VPS? On your desktop? In the post referenced in my previous comment, there is a connection string (for SQL Server) for TCP. – Tu deschizi eu inchid Jul 08 '22 at 01:25
  • Does this answer your question? [Cannot Connect to Server - A network-related or instance-specific error](https://stackoverflow.com/questions/18060667/cannot-connect-to-server-a-network-related-or-instance-specific-error) – Charlieface Jul 08 '22 at 01:48
  • 1
    You need to work out why it's not responding. Go through all the things it needs: can you ping the machine? Does it have TCP opened? Which port? Is it a named instance, if so is it on a static port? Is Remote Connections enabled? Is there a firewall blocking it? – Charlieface Jul 08 '22 at 01:50
  • @user9938 application is running on my desktop but I want to connect it’s database to my VPS… – Onyx Jul 08 '22 at 07:22
  • From one of your other [posts](https://stackoverflow.com/questions/72538716/using-import-returns-unexpected-token-import): _I just bought a Ubuntu VPS_. In another [post](https://stackoverflow.com/questions/72547067/is-it-possible-to-use-one-specific-mysql-result-as-an-object-nodejs) you mention that your database server is MySQL. According to [SQL Server on Linux](https://docs.microsoft.com/en-us/sql/linux/sql-server-linux-overview?view=sql-server-ver16), SQL Server runs on Linux, but it's not clear if your VPS is running both MySQL and SQL Server. Is the database server MySQL or SQL Server? – Tu deschizi eu inchid Jul 08 '22 at 12:45
  • What kind of project did you create? Are you using .NET Framework or .NET? See [here](https://dotnet.microsoft.com/en-us/learn/dotnet/what-is-dotnet-framework) for more information. – Tu deschizi eu inchid Jul 08 '22 at 12:47

1 Answers1

0

You can first try the other way; use

Data source =“serverName”;initial catalog=“dbname”; integrated security=true”

If it works, then the problem is probably in your credentials (username, password).

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
KSDev
  • 11
  • 1
  • 4
  • Hi, thanks for reply. Unfortunatelly, I have the same error when I tried with Integrated security =true – Onyx Jul 07 '22 at 22:18
  • @Onyx could you attach code how you make query to db? – KSDev Jul 07 '22 at 22:39
  • check updated question, posted it there. – Onyx Jul 07 '22 at 22:42
  • I would say that if you are able to access db with sql management studio via windows authentication or sql auth and execute your query, then the problem is only in connection string – KSDev Jul 07 '22 at 23:15
  • https://prnt.sc/VqzQY0Gy_OgN here on Login I have to put IP address of my VPS db right? I did that but it doesn't work. I tried connecting to db from my VPS nodejs project and works fine. First time using SQL server management studio – Onyx Jul 07 '22 at 23:22
  • application works when using local sql server from sql management studio but once I change connection string to my vps' database, it throws error above. – Onyx Jul 07 '22 at 23:43
  • @ No, at login section you should put the user you have inside server https://prnt.sc/WbRW9dtRgik9 under the security > logins folder – KSDev Jul 08 '22 at 10:14
  • but how does that makes my application connect to my VPS...? – Onyx Jul 08 '22 at 16:34