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)
{
}
}
}