-4

I wrote an app and it works fine at my system but when I install at another computer it doesn't work and has this error:

Unhandled exception has occurred ...

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: SQL Network Interfaces. error Locating Server/Instance Specified)

Cœur
  • 37,241
  • 25
  • 195
  • 267
karim
  • 1
  • 3

3 Answers3

1

Probably in your configurations you have set the SQL Server name to localhost or the machine that you are trying to run the app on does not have connectivity to the SQL Server because of networking settings or user rights.

Here is an example straight from http://csharp.net-informations.com/:

using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
       public Form1()
       {
         InitializeComponent();
        }

private void button1_Click(object sender, EventArgs e)
{
    string connetionString = null; 
    SqlConnection connection ; 
    SqlDataAdapter adapter ; 
    DataSet ds = new DataSet(); 
    int i = 0;
    connetionString = "Data Source=ServerName;Initial                                                                                                                                                                       
    Catalog=DatabaseName;User ID=UserName;Password=Password"; 
    connection = new SqlConnection(connetionString); 
    try 
      { 
       connection.Open();
       adapter=new SqlDataAdapter("Your SQL Statement Here", connection); 
       adapter.Fill(ds); 
       connection.Close(); 
       for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++) 
        { 
         MessageBox.Show (ds.Tables[0].Rows[i].ItemArray[1].ToString()); 
        } 
       } catch (Exception ex) 
          { 
            MessageBox.Show(ex.ToString()); 
          } 
  }
 }
}

Take a look at the connectionString. There is where you need to enter your server details.

  • how do I set my connection string to work in another system? – karim Sep 05 '17 at 09:10
  • Well I suppose that in your app you are using a config file or somewhere within your code you have a db connection method that you are calling... You should speciffy there the proper servername (either DNS or IP) and pot number (about the port: make sure the port is opened in the firewall to accept inbound and outbound traffic). Also make sure that the database name and database user credentials are correct. – Cosmin Gherghel Sep 05 '17 at 09:32
  • Another consideration would be that at SQL server level you set the db access to Windows authenticated users. You should configure a username and password with full access to your db that is going to be used by your app. In the config that I was talking about above you must pass these user credentials when initializing the db connection. – Cosmin Gherghel Sep 05 '17 at 09:40
  • I use DataAdapter and drag table to form instead of writing ConnectionString. please tell me how to change connection settings in this terms. I am new in this. thanks – karim Sep 05 '17 at 10:04
  • Your DataAdapter needs a connection take a look at my next answer. – Cosmin Gherghel Sep 05 '17 at 10:10
  • I don't have connectionstring in my code but just in app.config please look at the answer I will put some part of code – karim Sep 06 '17 at 08:29
  • can you show contents of app.config?? – Cosmin Gherghel Sep 06 '17 at 13:08
0

Can have many reasons.

  • SQL server not allowing the ip to connect to it.
  • SQL settings are not set well (linking to localhost? Dont have ODBC setup (if needed), etc)
  • Problem in the network itself.
  • Firewall configuration (either on server or client)
  • Etc, etc, etc.
Mark Baijens
  • 13,028
  • 11
  • 47
  • 73
0

probably you set your connectionstring acording your pc information and this not work for other machine. change your connectionstring to relative type and also make sure your destination machine have sql-server here you can find your suitable connection string: https://www.connectionstrings.com/

afzali
  • 125
  • 1
  • 3
  • 12