0

Let me add a quick explanation to this post:

I am using visual studio and if I create a connection to a database it works. I can query the database through the database designer (The one where you see the tables and can create new queries) and the data processes correctly.

HOWEVER even using this route I am getting the same sql exception. To me this says something in visual studio is not set up correctly however I could be wrong.

I am using the following code to connect to a database on my server (with using System.Data.SqlClient; at the top):

SqlConnection thisConnection = new SqlConnection();
thisConnection.ConnectionString = 
                              "Data Source=192.168.0.0,1433;" +
                              "Initial Catalog=test-db;" +
                              "User Id=UserName;" +
                              "Password=Password;";
thisConnection.Open();

And I am getting the following error:

at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, TdsParserState state) 
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, TdsParserState state) 
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() 
at System.Data.SqlClient.TdsParser.Connect(String host, SqlInternalConnection connHandler, Int32 timeout) 
at System.Data.SqlClient.SqlInternalConnection.OpenAndLogin() 
at System.Data.SqlClient.SqlInternalConnection..ctor(SqlConnection connection, Hashtable connectionOptions) 
at System.Data.SqlClient.SqlConnection.Open() 
at InventoryControl.Login.validUserName() 
at InventoryControl.Login.LoginButton_Click(Object sender, EventArgs e) 
at System.Windows.Forms.Control.OnClick(EventArgs e) 
at System.Windows.Forms.Button.OnClick(EventArgs e) 
at System.Windows.Forms.ButtonBase.WnProc(WM wm, Int32 wParam, Int32 lParam) 
at System.Windows.Forms.Control._InternalWnProc(WM wm, Int32 wParam, Int32 lParam) 
at Microsoft.AGL.Forms.EVL.EnterMainLoop(IntPtr hwnMain) 
at System.Windows.Forms.Application.Run(Form fm) 
at InventoryControl.Program.Main()

If you have any idea how to resolve this it would be greatly appreciated!

Exception is below:

System.Data.SqlClient.SqlError: SQL Server does not exist or access denied

However I am positive I have permission to this server as I can access it and query it in visual studio.

2
  • 8
    you missed the most important line of the exception message Commented May 8, 2013 at 17:16
  • That's the whole message, except the popup display is sqlException Commented May 8, 2013 at 17:45

2 Answers 2

2

I'm guessing it can't find the server. Given the default configuration on most routers/modems, I doubt that 192.168.0.0 is the IP you're looking for. If it's on your local machine, use localhost or 127.0.0.1 or your computer name (e.g. hans-pc). If it's on another machine, you need to make sure you have the right IP and that it is configured for remote access in both the firewall and the SQL Server Configuration Manager (enable the TCP/IP and/or named pipes protocols).

Also, you can use a few aliases for the properties in that string to shorten it up:

server=192.168.0.0;database=test-db;user id=UserName;password=Password
                                                                      ↑
                                      Make sure no trailing semicolon ┘

I dropped the port as 1433 is the default port for SQL server.

To get more information about the exception, try this code:

try
{
   ...
   thisConnection.Open();
   ...
}
catch (SqlException ex)
{
    for (int i = 0; i < ex.Errors.Count; i++)
    {
        Console.WriteLine(ex.Errors[i].ToString());
        // or output them wherever you need to see them
    }
}
Sign up to request clarification or add additional context in comments.

11 Comments

I used 192.168.0.0 as just an example ip (should have used something different) the actual ip I am 100% positive is correct because we have a java program using the same ip and accessing fine. I just enabled TCP/IP and named pipes as well. And it still is not working (Also I know I have access to it because in visual studio 2008 I am able to query it if I create a dataset however as soon as I try to access the code via c# I run into errors)
In the exception, the very first line should be the exception message. You provided only the stack trace. Can you double check that you cannot see an actual exception message or please post it if you do?
Exception has been relisted in the question section
@HansHansen: are you using Windows authentication or SQL authentication when you are connecting in VS?
If I create a databse connection in visual studio I use SQL authentication however I want to do it manually (AKA not with visual studios help) so the way my thought process worked is that the instantiation of userid and password lend it towards SQL authentication
|
0

Alright so here is the answer everyone!!!

The emulator was not set up correctly...THIS HAS NOTHING TO DO WITH CODE!!! You need to set up the emulator to connect to the internet, if using a mobile emulator see this link:

http://www.xdevsoftware.com/blog/post/Enable-Network-Connection-Windows-Mobile-6-Emulator.aspx

Comments

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.