Jezun
Jezun

Reputation: 77

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

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?

Upvotes: 0

Views: 7232

Answers (2)

tezzo
tezzo

Reputation: 11105

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.

Upvotes: 1

David A. Gray
David A. Gray

Reputation: 1075

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.

Upvotes: 0

Related Questions