I have a registration form and I want to feed the contents of it into an SQL database that I created in Visual Web Developer. I have the proper code to insert the contents into the database table, but there is a problem with my connection string. What's the problem with it? (I'm pretty sure the "server" property is causing the problem, so what is it supposed to be set to?) The SQL database is in the same VWD project as this code.

string connection = "server=local; database=AsamaiDB; Trusted_Connection=True";
SqlConnection conn = new SqlConnection(connection);

Recommended Answers

All 8 Replies

i believe your connection string written in wrong way. Try by below modified string.

string connection = "Data Source=your db server name;Initial Catalog=your db name;Persist Security Info=True;User ID=;Password="

I hope it will fix your problem..

I have a registration form and I want to feed the contents of it into an SQL database that I created in Visual Web Developer. I have the proper code to insert the contents into the database table, but there is a problem with my connection string. What's the problem with it? (I'm pretty sure the "server" property is causing the problem, so what is it supposed to be set to?) The SQL database is in the same VWD project as this code.

string connection = "server=local; database=AsamaiDB; Trusted_Connection=True";
SqlConnection conn = new SqlConnection(connection);

If you ever have questions on what a proper connection string is supposed to look like for a specified database I suggest using ConnectionStrings.Com. They cover just about every database system that you can use, it comes in real handy

What do I put in for user id and password? I never made one and don't know how.

If you have set any credentials to connect to the database then you need to set it else no need to write User Id and Password in connection string and You can remove it from the query string.

What do I put in for user id and password? I never made one and don't know how.

OK, but I'm still having a problem. The connection to the database is not working since there's something wrong with the connection string. I know what the database name is, but how do I find out the name of the database server? (I created this SQL database inside a Visual Web Developer project). I've already tried setting data source to localhost, but that still doesn't work.

Here's the error when I try to submit content to the database:

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: 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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

Source Error:

Line 20:         string connection = "Data Source=localhost;Initial Catalog=myDB;";
Line 21:         SqlConnection conn = new SqlConnection(connection);
Line 22:         conn.Open();
Line 23:         SqlCommand cmd = new SqlCommand(command, conn);
Line 24:         conn.Close();

The error is at line 22, in red.

This error

Exception Details: System.Data.SqlClient.SqlException: 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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

Is telling you that either there isn't a SQL Server located at localhost or it cannot connect with the login credentials you supplied. After looking at your connection string I'm voting for option 2 since there arent any credentials in this connection string

string connection = "Data Source=localhost;Initial Catalog=myDB;";

For how your connection string should look have a look at ConnectionStrings.com

As an additional note, if you never set any un/pw credentials when you set up your SQL server then odds are it's utilizing windows authentication (based on computer/user name).

When setting up a database for a web deployed project using windows authentication generally won't work out too well for you. It needs to be set up with user/pass authentication for the connection string provided to you above.

If your SQL server is, in fact, set up locally on your machine you can often access that server with COMPUTERNAME\SQLEXPRESS (using your computer's name found in the network settings of most windows OS's) if it's an express server and if it's actively running.

If, on the other hand, you set up a local DB file within your VWD and don't (somehow) have MS SQL Server or MS SQL Server Express actually set up on your system (unlikely as VWD installs express by default) then you would need to install one of those to utilize SQL database files.

As a final note, have you looked at the connectionstrings.com link that was provided at least twice above? It might help you sort the issue as well.

Hope this helps :)

I have a some problem in queries
how to join 4 tables

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.