7

Why is System.Data.SqlClient.SqlConnection class throwing an exception that "The parameter is incorrect"?

I'm attempting to learn how to set up database connections using a local SQL database and an ASP.NET MVC project, using .NET framework 4.5.

Here are the steps I've taken that lead up to this:

  1. Created a new SQL Server database in my project App_Data folder, called TestSQLdb.mdf.

  2. Created a connection string in web.config:

    <add name="SQLTestConnection1" 
         connectionString="Data Source=(LocalDB);initial catalog=TestSQLdb;Integrated Security=true" />  
    
  3. Access the connection string through

    string cn_str = ConfigurationManager.ConnectionStrings["SQLTestConnection1"].ConnectionString;
    

    (See this SO thread for more info on this).

  4. Created a new connection:

    SqlConnection conn = new SqlConnection(cn_str);
    
  5. Attempt to open the connection:

    try
    {
         conn.Open();
    

    At this point, an exception is thrown.

I tried to set this up so it would be simple and easy to learn on. What am I missing?

2
  • Do you really call a method cn_str() in your new SqlConnection or is it just a typo? Commented Feb 23, 2015 at 20:50
  • @PilgerstorferFranz: the cn_str() method is correct in my code, but for example here it should just be cn_str. Thanks for pointing it out. I'll edit my question, since this was also pointed out by @NightOwl888 in his answer below. Commented Feb 23, 2015 at 21:06

2 Answers 2

9

When you are working with a localDB you'll have to specify a AttachDbFileName attribute in your connectionString. This attribute should point to your TestSQLdb.mdf file. Initial catalog is the name of your dataBase within your mdf file.

see MSDN for this example

<add name="ConnectionStringName"
providerName="System.Data.SqlClient"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFileName=|DataDirectory|\DatabaseFileName.mdf;InitialCatalog=DatabaseName;Integrated Security=True;MultipleActiveResultSets=True" />
Sign up to request clarification or add additional context in comments.

1 Comment

Yep, that got it. +1 for the helpful link.
0

You are not passing a variable to the new SqlConnection, you are instead trying to call a function called cn_str.

To get it to work, you need to remove the parentheses from the name.

SqlConnection conn = new SqlConnection(cn_str);

2 Comments

Umm, my fault I didn't point out that I'm actually using a function to get cn_str. Thanks for pointing that out, but it's not actually the problem here I think. I'll edit my question if needed.
If this had been the problem, the error message would have been different and the exception would have happened at a different time.

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.