2

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!

4
  • what is the error? Commented Mar 1, 2017 at 11:17
  • Can you post the actual exception message? Commented Mar 1, 2017 at 11:17
  • Try to explicitly (in code) assign the connection string to your connection Commented Mar 1, 2017 at 11:30
  • You should check out Can we stop using AddWithValue() already? and stop using .AddWithValue() - it can lead to unexpected and surprising results... Commented Mar 1, 2017 at 12:33

2 Answers 2

3

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)) {
    ...
}
Sign up to request clarification or add additional context in comments.

Comments

1
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.

2 Comments

"I don't know where you got the idea that you could pass in the name of a configuration value." - Entity Framework? Its DbContext has a constructor parameter named nameOrConnectionString.
@CodeCaster Considering this was wrongly tagged EntityFramework, maybe you are right.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.