0

I know there is so many similar questions but I searched for hours and I really couldn't make it work so I decided to ask.

My app.config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <connectionStrings>
        <add name="PractiseCS" 
             connectionString="Server=.;Database=PractiseCS;Trusted_Connection=True;" 
             providerName="System.Data.SqlClient"/>
    </connectionStrings>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
    </startup>
</configuration>

And in my code, I am trying to use this connection string

SqlConnection connection;
string connectionString;

public AllUsers()
{
    InitializeComponent();

    connectionString =
        ConfigurationManager.ConnectionStrings["PractiseCS"].ConnectionString;
}

private void AllUsers_Load(object sender, EventArgs e)
{
    DoTheThing();
}

private void DoTheThing()
{
    connection = new SqlConnection(connectionString);
    connection.Open();

    SqlCommand command = new SqlCommand("SELECT * FROM Users", connection);

    SqlDataReader read = command.ExecuteReader();

    while (read.Read())
    {
        ListViewItem LV = new ListViewItem();
        LV.Text = read["Id"].ToString();
        LV.SubItems.Add(read["Username"].ToString());
        LV.SubItems.Add(read["Password"].ToString());
        listView1.Items.Add(LV);
    }

    connection.Close();
}

Error:

System.Data.SqlClient.SqlException (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)

System.ComponentModel.Win32Exception (0x80004005): The system cannot find the file specified

I want this program to run in other computers as well, so I didn't write my local address. But I couldn't make it work on this way either.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Utkan
  • 129
  • 12
  • 1
    Your code is fine. The part we can't see is the connection string, which is probably where the error is. Lots of variables here. The sql server name. The network you're running on. Whether or not you have access to said sql server. – mariocatch May 03 '18 at 04:09
  • this might help you : https://stackoverflow.com/questions/21875730/how-to-solve-the-system-data-sqlclient-sqlexception-0x80131904-error – Keren Caelen May 03 '18 at 04:12
  • https://stackoverflow.com/questions/18060667/why-am-i-getting-cannot-connect-to-server-a-network-related-or-instance-speci?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – sebu May 03 '18 at 04:15
  • Your Servername = . So, do you have local server on the machine, on which this C# application is running. . corresponds to localhost. You need to have a default instance of SQL Server on the machine – Venkataraman R May 03 '18 at 04:28
  • 1
    Searching is OK but _actual troubleshooting_ is what gets things solved. For example, if you followed this guide https://blogs.msdn.microsoft.com/sql_protocols/2008/04/30/steps-to-troubleshoot-sql-connectivity-issues/, then at what step did it fail? – Nick.Mc May 03 '18 at 05:44

1 Answers1

-1

Try trusted_Connection="False"

Avi Turner
  • 10,234
  • 7
  • 48
  • 75
avinash
  • 164
  • 1
  • 7