Matthew Sutton
Matthew Sutton

Reputation: 31

Unable to open database connection from configuration value

Firstly I am aware that this has been asked many times, but I could not find anything related to my problem specifically. basically I have text inputs I need to insert into a local DB, I've checked my connection string many times, and pretty certain it isn't the problem.

<add name="ElectricsOnline" 
     connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\electricsonline.mdf;Integrated Security=True" 
     providerName="System.Data.SqlClient"/>

and here is the method for inserting

protected void btnSubmit_Click(object sender, EventArgs e)
{
        using (var connection = new SqlConnection("ElectricsOnline"))
        {
            connection.Open();

            var sqlStatement = "INSERT INTO Orders (FirstName, LastName, Phone, Address, Suburb, State, Postcode, Ctype, CardNo, ExpDate, Email) VALUES(@FirstName, @LastName, @Phone, @Address, @Suburb, @State, @Postcode, @Ctype, @CardNo, @ExpDate, @Email)";

            using (var cmd = new SqlCommand(sqlStatement, connection))
            {
                cmd.Parameters.AddWithValue("@FirstName", txtFirstname.Text);
                cmd.Parameters.AddWithValue("@LastName", txtLastname.Text);
                cmd.Parameters.AddWithValue("@Phone", txtPhone.Text);
                cmd.Parameters.AddWithValue("@Address", txtAddress.Text);
                cmd.Parameters.AddWithValue("@Suburb", txtSuburb.Text);
                cmd.Parameters.AddWithValue("@State", txtState.Text);
                cmd.Parameters.AddWithValue("@Postcode", txtPostcode.Text);
                cmd.Parameters.AddWithValue("@Ctype", ddlCtype.SelectedValue.ToString());
                cmd.Parameters.AddWithValue("@CardNo", txtCardno.Text);
                cmd.Parameters.AddWithValue("@ExpDate", txtExpDate.Text);
                cmd.Parameters.AddWithValue("@Email", txtEmail.Text);

                cmd.ExecuteNonQuery();
            }
        }

        Response.Redirect("CheckoutReview.aspx");
    }

Source error shows this

Format of the initialization string does not conform to specification starting at index 0.

Line 22: {
Line 23:
Line 24: using (var connection = new SqlConnection("ElectricsOnline"))
Line 25: {
Line 26: connection.Open();

Any help would be greatly appreciated!

Upvotes: 2

Views: 514

Answers (2)

nvoigt
nvoigt

Reputation: 77285

new SqlConnection("ElectricsOnline")

I don't know where you got the idea that you could pass in the name of a configuration value. You need to pass in the connection string. Read it from your configuration and pass it to the constructor.

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726479

You cannot use the name of your connection from configuration file directly, because new SqlConnection(...) needs a connection string itself, not its name from the config file.

You need to retrieve connection string from config before using it to create connections. Change your code as follows:

var connStr = System
    .Configuration
    .ConfigurationManager
    .ConnectionStrings["ElectricsOnline"]
    .ConnectionString;
using (var connection = new SqlConnection(connStr)) {
    ...
}

Upvotes: 3

Related Questions