views:

56

answers:

6

I am a beginner in using Asp.NET with C# and it is my first time I am trying to establish a connection with an SQL server 2005 database:

Here is my simple code to connect to the sql server database,I am getting the text message set in the label. Is my problem in the connectionString ??? if so please show me examples how to write it and ow to get the server name and write it correctly ....or how to specify the database name (all path or just database name??)

protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection connection = new SqlConnection("server = Saher;Database=Database.mdf;integrated security = true");
            try{
                connection.Open();
            }
            catch{
                lblMessage.Text = "COULDN'T CONNECT to Stupid database";
            }finally{
                connection.Close();
            }



        }

Thanks,

+1  A: 

Change your code to this and post what lblMessage has in it:

protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection connection = new SqlConnection("server = Saher;Database=Database.mdf;integrated security = true");
            try{
                connection.Open();
            }
            catch(Exception ex)
            {
                lblMessage.Text = ex.Message;
            }finally{
                connection.Close();
            }

        }

Right now you are hiding the problem by putting "COULDN'T CONNECT to Stupid database" into your error label.

Abe Miessler
A: 

Check this Connection-Strings

Before all this, can you make it clear on which version of SQL server are you using and is the database file residing in the App_Data directory?

//Change the catch statement to get the error
//Original Code

catch{
  lblMessage.Text = "COULDN'T CONNECT to Stupid database";
}

//Change it into
catch(Exception ex){
  lblMessage.Text = ex.Message.ToString();
}
Chaitanya
yes the database resides in the App_Data directory and the server version is SQL server 2005.... should I put any of these two in my connection string??
Saher
Try this: "Server=.\SQLExpress;AttachDbFilename=|DataDirectory|Database.mdf; Database=dbname;Trusted_Connection=Yes;"Replace the dbname with the database name. I guess this should do.
Chaitanya
A: 

Take a look here.

Itay
+2  A: 

The Connection string for SQL Server using a trusted connection should be as follows:

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

myServerAddress can be the IP address of name of your server e.g. SQLSERVER01 or 192.168.1.5 myDataBase should be the actual database name not the MDF file e.g Northwind

Barry
A: 

this is the error I get fellows:

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

while my connection string now is :

"Server=./SQLExpress;AttachDbFilename=App_Data/Database.mdf; Database=Database;Trusted_Connection=Yes;"
Saher
A: 

Thanks everyone! I think I had more than one problem here but let me show the solution so that no one will spend many hours connecting to an SQL server database!!

protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection connection = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\ChurchApp\ChurchApplication\App_Data\Database.mdf;Integrated Security=True;User Instance=True");
        try{
            connection.Open();
            lblMessage.Text = "Connection Succeeded!";
        }
        catch(Exception ex){
            lblMessage.Text = ex.Message;
        }finally{
            connection.Close();
        }



    }

I had the @ sign missing before the connection string and I used to user the / instead to get over the error I get!!! (don't do that!!)

Get the connection string by right clicking on your database in the Server Explorer and Modify Connections and then ADVANCED .....copy everything from the advanced property you get or just the basic connection string found in the bottom of the page.

Saher