0

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

Additional information: 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)

I used these codes:

public partial class Form1 : Form
{
    SqlConnection con = new SqlConnection();
    public Form1()
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = "Data Source=SQLEXPRESS;Initial Catalog=StudentInformation;Integrated Security=True";

        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        // TODO: This line of code loads data into the 'sTUDENTDataSet.login' table. You can move, or remove it, as needed.  
        //this.loginTableAdapter.Fill(this.sTUDENTDataSet.login);  
        SqlConnection con = new SqlConnection("Data Source=SQLEXPRESS;Initial Catalog=StudentInformation;Integrated Security=True");
        con.Open();

        {
        }
    }

    private void btnLogin_Click_1(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = "Data Source=SQLEXPRESS;Initial Catalog=StudentInformation;Integrated Security=True";
        con.Open();
        string UserId = txtUsername.Text;
        string UserPass = txtPassword.Text;
        SqlCommand cmd = new SqlCommand("Select UserId,UserPass from Login where UserId='" + txtUsername.Text + "'and UserPass='" + txtPassword.Text + "'", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            MessageBox.Show("Login sucess!");
            Form2 form = new Form2();
            form.Show();
        }
        else
        {
            MessageBox.Show("Invalid Login Information. Please check username and password");
        }
        con.Close();
    }

The error here is the con.Open(); that belongs here:

SqlConnection con = new SqlConnection();
            con.ConnectionString = "Data Source=SQLEXPRESS;Initial Catalog=StudentInformation;Integrated Security=True";
            con.Open();

I tried removing it because I don't know what else to do and the second error is on da.Fill(dt); So I guess the only problem that should really be fixed is the con.Open();

What should I do?

Community
  • 1
  • 1
Jezun
  • 79
  • 2
  • 10
  • If you are on running this code in your localhost try changing the data source to: localhost\SQLEXPRESS and also verify that the Catalog name is correct. – Alejandro Ocampo Jul 30 '15 at 15:18
  • 1
    Looks like the DB is in your local machine. Try to connect to that Database server using SQL server management studio and windows authentication. I feel like the host name is not correct. If it's correct, check if all the SQL server services are up and running. – Varun Paul Jul 30 '15 at 15:22
  • Also please read this: https://en.wikipedia.org/wiki/SQL_injection – n8wrl Jul 30 '15 at 15:28
  • 'SqlConnection con = new SqlConnection(); con.ConnectionString = "local host=SQLEXPRESS;Initial Catalog=StudentInformation;Integrated Security=True"; con.Open();' it says localhost not supported – Jezun Jul 30 '15 at 15:29
  • change SQLEXPRESS to dot (.) or localhost please, then give feedback to us – vahid kargar Jul 30 '15 at 15:32
  • I did change but the error is still the same which is the con.Open(); – Jezun Jul 30 '15 at 15:38

2 Answers2

1

The error is in your connection string.

As a DataSource you have to specify SERVER\INSTANCE; SQLEXPRESS is usually an instance name in default installation, so try:

con.ConnectionString = "Data Source=.\SQLEXPRESS;Initial Catalog=StudentInformation;Integrated Security=True";

., (LOCAL), LOCALHOST and YourMachineName are all equivalent, referring to your own machine as a server. If your database is on another PC you have to specify its name.

tezzo
  • 10,858
  • 1
  • 25
  • 48
  • I tried .SQLEXPRESS instead of .\SQLEXPRESS because it will be an error for the \ But still, the error is the same, which is the con.Open(); – Jezun Jul 30 '15 at 15:40
  • just a note: the error is in `con.Open()` but it refers to a server that 'was not found or was not accessible' and server is specified via `con.ConnectionString`. – tezzo Jul 30 '15 at 15:48
  • where do you install SQL SERVER? Is it on your PC? – tezzo Jul 30 '15 at 15:49
  • verify your server/instance name: http://stackoverflow.com/questions/141154/how-can-i-determine-installed-sql-server-instances-and-their-versions – tezzo Jul 31 '15 at 06:49
0

Your connection string looks incomplete. While it names a server (SQLEXPRESS), it omits any mention of which data base.

Although it refers instead to LocalDB, perhaps comparing the working connection string below against yours will suggest to you what you need to add.

Data Source=(LocalDB)\v11.0;AttachDbFilename="$$WorkingDirectory$$\RGUNC_Tag_Browser\RGUNC_Tags.mdf";Integrated Security=True

The bottom line is that the error message is telling you that it cannot locate your data base with the information supplied in your connection string.

David A. Gray
  • 1,039
  • 12
  • 19