-2

I am needing to pass username and password so that any user can run stored procedures on a database. In my web config I have my code set to this

<appSettings>
  <add key="Connection1" value="server=validserver;database=validdatabase;uID=validuserid;password=validpassword;" />
</appSettings>

And in my C# - I first set a variable equal to the connection like so:

public const string MainDBConn = "Connection1";

and any time I want to use this connection string I call it like this

RunSQLQuery(MainDBCon, "nameofstoredprocedure");

However I keep getting this error thrown, which to me says it is an invalid conenction string, but my server name, database, userid and password are all valid...

Format of the initialization string does not conform to specification starting at index 0

What do I alter so that I can use this connection string in my syntax to connect to my server instance?

EDIT Altering my connection string to be

ConfigurationManager.ConnectionStrings["Connection1"].ConnectionString;

Now produces an error of:

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)

EDIT 2
I modified my webconfig connection as suggested by @Khazratbek in his answer and now I get this error:

Connection Timeout Expired. The timeout period elapsed while attempting to consume the pre-login handshake acknowledgement.
This could be because the pre-login handshake failed or the server was unable to respond back in time.
The duration spent while attempting to connect to this server was - [Pre-Login] initialization=3672; handshake=0;

That is strange to me as I have my timeout set to 0, which if I understand properly should set this to be non timeout

CommandTimeout = 0;

2 Answers2

1

Connection string in web.config usually like this :

<connectionStrings>
<add 
name="connectionStringName" 
connectionString="Data Source=serverName;Initial 
Catalog=yourDatabase;Persist Security Info=True;User 
ID=userName;Password=password"
providerName="System.Data.SqlClient"
/>

and then read connection like:

System.Configuration.ConfigurationManager.
ConnectionStrings["connectionStringName"].ConnectionString;

Follow the following steps to connect to SQL server:

1- Click Server Explorer in visual studio and then click it, add connection will display.

2- Enter your Server name and password and the click ok.

3- Go to properties and copy connection string.

4- paste copied connection string here

connectionString="copied connection string"

enter image description hereenter image description here

Saif
  • 2,611
  • 3
  • 17
  • 37
1

First of all, in the web.config you have to add not appSettings, and the next one:

<connectionStrings>
  <add name="Connection1" connectionString="Server=validserver;uid=validuserid;pwd=validpassword;database=validdatabase"/>
</connectionStrings>

Then in your page:

using System.Web.Configuration;

And your connection string will be:

string ConnectionString = WebConfigurationManager.ConnectionStrings["Connection1"].ConnectionString;
Khazratbek
  • 1,656
  • 2
  • 10
  • 17