0

Hi I have been going through threads with the same error but cannot find any solution with visual studio 2013. When I test the connection with the database only it works and I can enter the records manually. But when I try using the code below it gives me the above mentioned error. I created the database with SQL. The original Connection string is

Data Source=(LocalDB)\v11.0;AttachDbFilename="C:\Users\fatimam\documents\visual studio 2013\Projects\BookingSystem\BookingSystem\App_Data\BookingsDb.mdf";Integrated Security=True

I suspect that my connection string in my code is not correctly formatted. I tried (LocalDB)\v11.0 and gives me the same error as well. when I run the program and it gets to the myDBconnection.Open(); then it goes to the Catch block. Could you please help me resolve this connection problem? Thank you

        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Web;
        using System.Web.UI;
        using System.Web.UI.WebControls;
        using System.Data.Sql;
        using System.Data.OleDb;
        using System.Data.SqlClient;
        using System.Configuration;

        namespace BookingSystem
        {
        public partial class _Default : Page
        {
        protected void Page_Load(object sender, EventArgs e)
        {

        int id = 65;
        string title = "Mr";
        string name = "Name";
        string surname = "Surname";
        string company = "Company";
        string address = "address";
        string city = "Cape town";
        string postal = "7764";
        string contact = "0829875";
        string email = "email";

        try
        {
            string myDBCon = "Data Source=." + "\\" + "SQLEXPRESS;AttachDbFilename='C:" + "\\" + "Users" + "\\" + "fatimam" + "\\" + "Documents" + "\\" + "Visual Studio 2013" + "\\" + "Projects" + "\\" + "BookingSystem" + "\\" + "BookingSystem" + "\\" + "App_Data" + "\\" + "BookingsDb.mdf';Integrated Security=True;User Instance=True";
            //Creating connection to the Database
            System.Data.SqlClient.SqlConnection myDBconnection = new System.Data.SqlClient.SqlConnection();
            //Adding the connection string to the connection
            myDBconnection.ConnectionString = myDBCon;
            //Opening Connection
            myDBconnection.Open();
            //Creating a string to hold the sql query
            string insertSqlQuery = "INSERT INTO Customer (customer_id, title, name,surname,company,address,city,postal_code,contact,email) VALUES ('" + id + "','" + title + "','" + name + "','" + surname + "','" + company + "','" + address + "','" + city + "','" + postal + "',,'" + contact + "','" + email + "')";
            //Create a command in C# to perform the sql Query
            SqlCommand myInsertCommand = new SqlCommand(insertSqlQuery, myDBconnection);
            //Creating the process for performing the command
            SqlDataReader performCommand = myInsertCommand.ExecuteReader();
            //Executing the command or running the command
            performCommand.Read();
            //Closing the execution of the command
            performCommand.Close();
            //Closing the connection
            myDBconnection.Close();

        }

        catch (SqlException exp)
        {
            // Log what you need from here.
            throw new InvalidOperationException("Data could not be read", exp);
        }

    }

    protected void Button1_Click(object sender, EventArgs e)
    {

    }
}

}

leppie
  • 115,091
  • 17
  • 196
  • 297
noor
  • 21
  • 1
  • 7
  • Are you using SQL Server Express 2012? If you're using SQL Server 2012 Enterprise Edition, the default connection string may not be correct. Which SQL Server version are you using? – Auguste Jun 09 '16 at 20:23
  • Why are you using a SQLExpress syntax for a LocalDB instance? – Conrad Frix Jun 09 '16 at 20:26
  • I created the database by right clicking on App_Data folder->Add new item->then SQL Server Database. I a not sure how to check the Sql Server database on visual studio. SQLExpress I copied from an example we did in class. That is what the lecturer used when we did the example – noor Jun 09 '16 at 20:32
  • 1
    @KenWhite - Care to post a link to go along with your helpful comment? – grambo25 Jun 09 '16 at 20:41
  • @KenWhite I have been trying for almost 12 hours.. – noor Jun 09 '16 at 20:55
  • 1
    @grambo25: Certainly. http://stackoverflow.com/search?q=[sql-server]+a+network+related+or+instance+specific+error – Ken White Jun 09 '16 at 21:05
  • Double Click on the MDF file under App_data. This will open up the server explorer. Right-Click on the mdf in the Server explorer window and select properties. You'll see a "Connection String" Property. What does it say? – Conrad Frix Jun 09 '16 at 21:19
  • Data Source=(LocalDB)\v11.0;AttachDbFilename="C:\Users\fatimam\documents\visual studio 2013\Projects\BookingSystem\BookingSystem\App_Data\BookingsDb.mdf";Integrated Security=True – noor Jun 09 '16 at 21:26
  • 1
    Ok then do this `string myDBCon = @"Data Source=(LocalDB)\v11.0;AttachDbFilename="C:\Users\fatimam\documents\visual studio 2013\Projects\BookingSystem\BookingSystem\App_Data\BookingsDb.mdf";Integrated Security=True"` The `@` symbol stops you from having to escape the backslahes – Conrad Frix Jun 09 '16 at 21:29

2 Answers2

1

In your connection string string myDBCon just use single \ and use Server keyword instead instead of Data Source. and at the end of your connection string you can use Trusted_Connection=True; instead of both Integrated Security=True; and User Instance=True". And an other important thing is YOURCOMPUTERNAME\SQLEXPRESS.

Raza Ali
  • 46
  • 8
  • I tried string myDBCon = "Server=" + "\\" + "FATIMA"+"\\"+"SQLEXPRESS;AttachDbFilename='C:" + "\\" + "Users" + "\\" + "fatimam" + "\\" + "Documents" + "\\" + "Visual Studio 2013" + "\\" + "Projects" + "\\" + "BookingSystem" + "\\" + "BookingSystem" + "\\" + "App_Data" + "\\" + "BookingsDb.mdf';Trusted_Connection=True;"; and I tried string myDBCon = Server=Fatima\SQLEXPRESS;AttachDbFilename="C:\Users\fatimam\documents\visual studio 2013\Projects\BookingSystem\BookingSystem\App_Data\BookingsDb.mdf";Trusted_Connection=True; Both gives red errors. – noor Jun 09 '16 at 20:54
  • from the first one I tried removing also one \ and everything red underlined. – noor Jun 09 '16 at 20:54
  • finally got it right.. string myDBCon = @"Server=(localdb)\v11.0;AttachDbFilename=C:\Users\fatimam\Documents\Visual Studio 2013\Projects\BookingSystem\BookingSystem\App_Data\BookingsDb.mdf;Integrated Security=True"; – noor Jun 10 '16 at 22:41
1

Per the answer provided by user 'eu-ge-ne' here:

c# 2008 SQL Server Express Connection String

The recommended solution would be to remove "Integrated Security=True" from your connection string, and optionally add "Persist Security Info=True;".

According to MSDN:

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

Community
  • 1
  • 1
Spazmoose
  • 305
  • 2
  • 9