Click here to Skip to main content
15,902,198 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi, I am having trouble connection my database to my windows application using a microsoft server 2008. When using a local connection string I am getting no errors and the program runs fine so I know its an issue with the connection string or something I am totally over looking.

The error I get when I change the connection string to an ip based one is this:


'The invocation of the constructor on type 'CalorieCounter.MainWindow' that matches the specified binding constraints threw an exception.' Line number '4' and line position '9'.

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 0 - No connection could be made because the target machine actively refused it.)


Now I know there is absolutly nothing wrong with that position as nothing changes in that position from being a local connection to a server connection. My 2 connection strings are:

SqlConnection conn = new SqlConnection("Data Source= 192.168.0.104,1433;Network Library=DBMSSOCN;Initial Catalog=food;");


SqlConnection conn = new SqlConnection("Data Source=.\\SQLExpress;Initial Catalog=Food;Integrated Security=SSPI;");


The second works perfectly connects no issues. The first crash's my program. Someone suggested changing the connection string within app.config I tried this and made no difference to the problem. I have also looked on connectionStrings.com but still can not get it to work!

Any help would be appreciated as it is starting to get very frustrating!
Posted
Updated 24-Jun-11 2:29am
v2
Comments
Dylan Morley 24-Jun-11 8:30am    
Added actual exception message from comment below

You could press Ctrl->Alt-E and make sure that 'break when an exception is thrown' is checked for 'common language runtime exceptions'

Or, put a try catch block around the line of code conn.Open()

e.g.

try
{
    SqlConnection conn = new SqlConnection("Data Source= 192.168.0.104,1433;Network Library=DBMSSOCN;Initial Catalog=food;");
    conn.Open()
}
catch (SqlException ex)
{
    Console.WriteLine(ex.Message);
}


Now you can debug and see exactly what the error message is.

Basically, "The invocation of the constructor on type..." exception is just saying it encountered an error within your form construct, it's not giving you the detail you need to solve the underlying problem.


Edit: Could there be a firewall blocking the remote connection?

http://technet.microsoft.com/en-us/library/cc646023.aspx[^]
 
Share this answer
 
v3
Comments
DanHodgson88 24-Jun-11 7:43am    
cheers for that, thats really helpful :)
jayantbramhankar 24-Jun-11 8:27am    
Good Solution
Have you tried making a connection in the Server Explorer? When you have correctly made that, the connection string is available in the Properties Pane.
 
Share this answer
 
Comments
DanHodgson88 24-Jun-11 7:40am    
When I go into server explorer where do I need to make the connection? At the minute the data is on my local machine, obviously why local connection string works, I have just looked when you mentioned and the connection string is the local one I have shown above. Sorry if Im sounding inexperienced, I am! C# and servers are new to me!
OriginalGriff 24-Jun-11 8:36am    
At the top of the Server Explorer pane are four buttons: use the "Connect to Database" one. It will allow you to browse for your server via the "server name" drop down, and the database via the "Select or enter a database name" drop down.
If you can't get there that way, you need to look at your SQL server directly, and work out why you can't see it.
SqlConnection conn = new SqlConnection("Data Source= 192.168.0.104,1433;Network Library=DBMSSOCN;Initial Catalog=food;");


Maybe the comma between the IP address and the port is the problem. I've never seen that syntax to indicate address and port ; usually, separator is rather ':'.

So, I would try one of these tricks :

- Do not indicate the port at all, as 1433 is the default port for SQL Server instances
OR
- Replace 192.168.0.104,1433 by 192.168.0.104:1433

... and see what happens with your connection.

Hope this helps.
 
Share this answer
 
Comments
DanHodgson88 24-Jun-11 8:15am    
cheers for the reply, tried it and unfortunatly get the same result! I have got it to throw me an error messgae now thanx to the guy above i've now got:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 0 - No connection could be made because the target machine actively refused it.)

any suggestions?
Dylan Morley 24-Jun-11 8:35am    
See my updated answer,
ta

phil.o 24-Jun-11 9:34am    
It sounds better : now we have a message saying that the connection was attempted but the computer refused it.

I would check that TCP protocol is activated for the instance of SQL Server. You can find it in 'SQL Server COnfiguration Tools'.

Hope this helps

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900