0

Hy,

I am using SQL Server 2012 under MS Visual Studio 2012.

Below is my connection string from app.config

<add key="Platform" value="Data Source=Bigfoot2;Initial Catalog=Platform;Integrated Security=True"/>

and my connection class

static SqlConnection con;
        public static SqlConnection GetConnection()
        {
            con = new SqlConnection(ConfigurationManager.AppSettings["Platform"].ToString());
            return con;
        }

 internal void AddCustomer (Buyer customer, User user, PlatformType type )
        {
            SqlConnection conn = DALConnection.GetConnection();

            try
            {
                SqlCommand cmd = new SqlCommand("InsertCustomer", conn);
                cmd.CommandType = CommandType.StoredProcedure;

                 ...

                con.Open();
                cmd.ExecuteNonQuery();

My database is stored under my project folder.

The error I get when I try to use my method is:

Could not open a connection to SQL Server 2012

Sincerely,

Avi Turner
  • 10,234
  • 7
  • 48
  • 75
SocketM
  • 564
  • 1
  • 19
  • 34
  • It seems that the problem is not in your code. I would suggest testing the connection using an external tool first. I recommend a [UDL file](http://msdn.microsoft.com/en-us/library/e38h511e(v=vs.71).aspx) – Avi Turner May 11 '14 at 07:19

4 Answers4

0

Your connection object is wrong, you are passing conn in Command while for Connection open you are using con object not conn:

con.Open();

use :

conn.Open();

you code will be like:

try
            {
                SqlCommand cmd = new SqlCommand("InsertCustomer", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                conn.Open();
                cmd.ExecuteNonQuery();
             }
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
  • 1
    In the OP's code, `con == conn` (two different variables, but they'll get the same connection object), so it shouldn't matter. –  May 11 '14 at 07:15
  • but see down, OP is passing ``conn`` in Command while OP is using ``con.Open()`` instead of ``conn.Open()`` – Ehsan Sajjad May 11 '14 at 07:17
0

Although your code has some issues, It seems that it should work.
Try testing your connection string using an independent tool for connectivity.
For testing the connection string take a look at this SO thread.

As @hvd has mentioned in one of the comments, your code is confusing as you have con which is equivalent to conn. this makes your code hard to follow. I recommend you refactor it so it will be easier for you (and others) to follow.

Good luck!

Community
  • 1
  • 1
Avi Turner
  • 10,234
  • 7
  • 48
  • 75
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – Jaap May 11 '14 at 08:03
  • @jaap I agree with your comment, but don't think it applies here. This is not a `critique or request clarification`. The error message suggests that the issue is in the connection string. I have advised how to check this issue, It seems to me that once the connection issue is solved, his problem will be solved. Without having his network structure, it is impossible to give a more concrete solution. – Avi Turner May 11 '14 at 08:10
  • i think your answer is more a comment/suggestion than an answer as it only gives a direction for the solution and not a worked out solution – Jaap May 11 '14 at 09:07
0

Try this:

<add key="Platform" value="Data Source=.\Bigfoot2;Initial Catalog=Platform;Integrated Security=True"/>
Meysam Tolouee
  • 569
  • 3
  • 17
0

If your SQL Server is on a different machine than your Visual Studio or app, are the required ports open in the Windows Firewall ?

Try these steps: http://blog.sujay.sarma.in/2013/12/19/SCRIPT-Open-ports-in-Windows-Firewall-for-SQL-Server-connectivity