tags:

views:

55

answers:

4

Hey, I am trying to create a login page that checks the username and password with the database on the server. The server is located in a different country.

This is the code I have so far:

    #region Building the connection string

            string Server = "XX.XXX.XX.XX, XXXX";
            string Username = "_Username_";
            string Password = "_Password_";
            string Database = "_Database_";

            string ConnectionString = "Data Source=" + Server + ";";
            ConnectionString += "User ID=" + Username + ";";
            ConnectionString += "Password=" + Password + ";";
            ConnectionString += "Initial Catalog=" + Database;

            #endregion

            SqlConnection SQLConnection = new SqlConnection();

            try
            {
            SQLConnection.ConnectionString = ConnectionString;
            SQLConnection.Open();
 
            // You can get the server version 
            // SQLConnection.ServerVersion
            }

            catch (Exception Ex)
            {
            // Try to close the connection
            if (SQLConnection != null)
                SQLConnection.Dispose();
 
            // Create a (useful) error message
            string ErrorMessage = "A error occurred while trying to connect to the server.";
            ErrorMessage += Environment.NewLine;
            ErrorMessage += Environment.NewLine;
            ErrorMessage += Ex.Message;
 
            // Show error message (this = the parent Form object)
            MessageBox.Show(this, ErrorMessage, "Connection error", MessageBoxButtons.OK, MessageBoxIcon.Error);
 
            // Stop here
            return;
}

I am getting the error message:

Non-negative number required. Parameter name: count

I have accepted wildcards on my server and I have no idea what that error means? Any help would be appreciated,

Thanks.

A: 

try to use a real connection string at first - that is tested against the DB with SSMS, then create your own. once the first one works, the 2nd one should work (make sure they are identical)

never concatenated input from client into an SQL statement (I know this is a connection string... but it's a bad idea to use input without checking it for illegal chars / injection, so further down the code - make sure you don't do this).

Dani
A: 

why do you have a messagebox.show in your data access code?

Its really bad practise dude.

umm valid connection strings can be found here

http://www.connectionstrings.com/

also use a connectionstring builder http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnectionstringbuilder.aspx

im guessing that your connection string is invalid and so it is failing on counting its params ... but am really guessing there.

Your connection string will not be changing so id put it in the app.config ro web.config

also if you adda datasource then it will construct the connection string and add the item into your web.config.

John Nicholas
I am trying to add a Data Source, but when I have filled out all the information in Add Connection, when I click Test Connection it comes up with the same error?
Crazyd22
if you have the correct ip and port along with the correct user name and password ... then the problem is in the database config i think. are you sure its a sql server database? My point being that i no longer think its a programming problem.
John Nicholas
A: 

best option user SqlConnectionStringBuilder as suggested by SLaks

or

put connection string in app.config file

or

try terminating connection string with semicolon (;) --this is just a wild guess

  string ConnectionString = "Data Source=" + Server + ";";
  ConnectionString += "User ID=" + Username + ";";
  ConnectionString += "Password=" + Password + ";";
  ConnectionString += "Initial Catalog=" + Database + ";";

PS:- Do pay attention to what John Nicholas is saying about MessageBox in data access code. If you are a beginner then try to develop good coding practices early on. It will help you tremendously as you graduate to write more complicated applications.

TheVillageIdiot
I tried SqlConnectionStringBuilder but still doesnt work, gives the same message
Crazyd22
+1  A: 

You need to put quotes around the server name in the connection string.

However, you should use a SqlConnectionStringBuilder instead, like this:

SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();

builder.DataSource = Server;
builder.UserID = Username;
builder.Password = Password;
builder.InitialCatalog = Database;


SqlConnection SQLConnection = new SqlConnection(builder.ToString());
try {
    SQLConnection.Open();
    ...
} finally {
    SQLConnection.Close();        
}

Also, you should close the connection in a finally block.

SLaks
Quallity, I will try this now, thanks! :D
Crazyd22
I tried this but still get the same message, it gets thrown at 'SQLConnection.Open();'
Crazyd22
Please show us the full stack trace of the exception. Can you connect to the server using Visual Studio's UI or SSMS?
SLaks
When I try to add a Data Source, I get the same error message when I try to test the connection, if that's what you mean? Whats SSMS?
Crazyd22
For the DataSource I am using "my server ip, server port", is the correct?
Crazyd22
SSMS is SQL Server Management Studio. Your DataSource is wrong.
SLaks
usually you don't need to specify port. try without giving port and even if you have to specify port it is in this formsat IPADDRESS:PORT, note **:** dont use simple comma (,)
TheVillageIdiot
I have changed the DataSource to the correct one, and it is not saying '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: SQL Network Interfaces, error: 25 - Connection string is not valid)' ? I'm pritty sure that the server IP and port is correct as I asked the manager of the company :P
Crazyd22
There is probably a firewall
SLaks
Hmm, okay I will talk to the guy
Crazyd22