Hi there.! I've been working on our Software Analysis Development project for how many days and sleepless nights, the application is an Online Shopping. I'm using Microsoft Visual Studio 2010 as my environment, then I used C# as language for the site. I used Microsoft SQL 2008 R2 as my server and I made Database on the SQL Sever Management Studio with the tables namely dbo.login, dbo.cart, dbo.userdetails, dbo.dresses.

When I log in as administrator on the Log In page (Log In.aspx), there is an error shown on the response and I can't continue with it. As well as when I run the other pages with similar codes, the same error was shown.

Here's the code for the Log In.aspx.cs, please do check, I really need you help.. Please...

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class Log_In : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        int flag = 0;
        SqlConnection con = new SqlConnection("Data Source=(localhost);Initial Catalog=chenezboutique;Integrated Security=true;");      // access database
        SqlCommand com = new SqlCommand("select * from login", con);            // access login table from the database
        con.Open();
        if (con.State == ConnectionState.Open)
        {
            SqlDataReader dtr;                  // check database for user's account
            dtr = com.ExecuteReader();
            while (dtr.Read())
            {
                if (dtr[0].ToString().Equals(TextBox1.Text) && dtr[1].ToString().Equals(TextBox2.Text))
                {
                    flag = 1;
                    break;
                }
            }
            if (flag == 1)
            {
                HttpCookie uname = new HttpCookie("username");
                HttpCookie upass = new HttpCookie("userpass");
                HttpCookie rights = new HttpCookie("rights");
                uname.Value = TextBox1.Text;
                upass.Value = TextBox2.Text;
                rights.Value = dtr[2].ToString();
                Response.Cookies.Add(uname);
                Response.Cookies.Add(upass);
                Response.Cookies.Add(rights);
                Response.Redirect("Home.aspx");
            }
        }
        con.Close();
    }
    protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {

    }
    protected void TextBox2_TextChanged(object sender, EventArgs e)
    {

    }
}

Here's the error in Red:

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)
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: 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 Error:

Line 25: SqlConnection con = new SqlConnection("Data Source=(localhost);Initial Catalog=chenezboutique;Integrated Security=true;"); // access database
Line 26: SqlCommand com = new SqlCommand("select * from login", con); // access login table from the database
Line 27: con.Open();
Line 28: if (con.State == ConnectionState.Open)
Line 29: {

Try changing this:

SqlConnection con = new SqlConnection("Data Source=(localhost);Initial Catalog=chenezboutique;Integrated Security=true;");

to this:

SqlConnection con = new SqlConnection("Data Source=localhost;Initial Catalog=chenezboutique;Integrated Security=true;");

I did try, but it's still not working.. Help!

The problem is the connection string.

Try removing parameters. Maybe the Initial Catalogue, and make sure that the Integrated Security is correct as well.

I think so, it's really on the connection string.. I am really trying to correct it, but until now I can't figure it out. I tried to change the connection string but the error is still the same.

As charlybones said, there is 100% a problem in the connection string structure.

Remove Integrated Security=True from your connection string and (optional) add Persist Security Info=True;

From MSDN:
Integrated Security - When false, User ID and Password are specified in the connection. When true, the current Windows account credentials are used for authentication.

@Mitja Bonca: I also tried your suggestion but it's still not working,still the same error. Gosh.!. I appreciate your heLp, you and Charlybones.. Thank you.. I still need more help...

Where did you find this connection string anyway?

Try this conn.string:

"Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI;"

And go here to check for the right connection string, if the one I pasted will not work.

--------------

One more thing to mention:
Go to SQL Server Management Studio,
Right click on the database,
select properties,
select Security,
Click ON> SQL Server and Windows Authentication mode ON.

Then an explicit username connection will work.

I got the connection string in this reference
http://sqlstrings.com/SQL-Server-connection-strings.htm

chenezboutique is the database i created at SQL Server Management Studion, it contains 4 tables dbo.login, dbo.cart, dbo.userdetails,and dbo.dresses.

I tried the connection string you currently suggested, but it's still the same error..

Also I did go to SQL server Management Studio, but I can't seem to find the Security option when I clicked properties..

You need a connection string appropriate for Microsoft SQL 2008 R2 database.
Go here to find your connection string.
Btw, is your DB Express or not?

I'll try to find out on the link you shared.

I did clicked on the link you shared.. I used this one:

Standard Security
Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;

replacing the connection string to this:
("Data Source=_ROSEANN_\sqlexpress;Initial Catalog=chenezboutique;User Id=;Password=;")

but when the page is build, there was an error: Unrecognized escape sequence
("Data Source=_ROSEANN_\sqlexpress;Initial Catalog=chenezboutique;User Id=;Password=;")

error in red..

my DB is express..

Now it makes more sence why its not working. You didnt says your DB is Express edition.
If so, you must include "SQLExpress" keyword in the conn.string.
Check these two out:

Connecting to a Local SQL Server Express Instance:

"Server=.\SQLExpress; AttachDbFilename=yourMDFFile.mdf; Database=yourDB; Trusted_Connection=Yes;"

Connecting to a Local SQL Server Express Instance (Database File in Data Directory):

"Server=.\SQLExpress; AttachDbFilename=|DataDirectory|yourMDFFile.mdf; Database=yourDB;
Trusted_Connection=Yes;"

I tried again the two strings , but still the error says:

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 Error:

Line 18: SqlConnection con = new SqlConnection("Server=_ROSEANN_/SQLEXPRESS;AttachDbFilename=ASPNETDB.mdf;Database=chenezboutique;Trusted_Connection=Yes;");
Line 19: SqlCommand com = new SqlCommand("select * from login", con);
Line 20: con.Open();
Line 21: if (con.State == ConnectionState.Open)
Line 22: {

error in BOLD

I hope you will not feel weary of helping me out.

Hmm, here is something I just found. Read and try it out.

Gosh! I feLt like giving up, it's not working even though I followed step-by-step the link you currently shared... What will I do now?..(sigh)

YOu have to ge the correct connection string.

One last questions:
1. Is this Sql Server 2008 Express edition?
2. Is Db located on your hdd (hard drive) at the moment?

I think there is a problem on the named pipes because the error keeps on saying like this:
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)

1. I am using Microsoft SQL Server 2008 R2 Express edition
2. My database is located on C: drive, the local drive

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.