I'm trying to insert data from a textbox into a SQL table. I created a SQL db on my local machine and the instance name is "Brandon-PC\brandon". The username is "Test" and the password is "test". The db is named "STATUS". I have added the user to the server as a sysadmin as well adding it in the STATUS db. For the "server = " argument I've tried Brandon-PC, Brandon-PC\brandon, and brandon I keep getting this error:
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) I've posted the code below, and any help is appreciated.
Using "server = brandon" is definitely wrong, because it's only the name of instance. You need to use "server=Brandon-PC\brandon", or "localhost\brandon". If this doesn't help - try to connect to the database, for example, with VS.NET (Tools - Connect to
Database) and see if it works. If it doesn't work, see if
remote connections were enabled.
Smirnov, I'm able to connect to the STATUS db using the using Tools>>Connect
The owner of the db is Brandon, but there is no password for that account. I created that Test account because of this. I know the name and password are correct
B471code, that looks like a connection string within SQL, will that work for a C# webform?
The error didn't say about password - "Verify that the instance name
is correct and that SQL
Serveris configured to allow remote connections". Your sql connection string is ok, but not well-formed (take a look at
connectionstrings.com for right syntax).
I don't know why it wouldn't work, and I guess I should've tried it. I just did and i got the same error. As I tweak the code, the error seems to change from, "Format initialization string does not conform to specification starting at index XX" where XX
is 18,19, or 21.
Is there somewhere I can check these specifications?
RageQwit
Member
15 Points
11 Posts
Trouble with SQL connection string
Jan 25, 2012 08:49 PM|LINK
protected void Button1_Click(object sender, EventArgs e) { SqlConnection connection = new SqlConnection("server = brandon; uid = Test; pwd = test; database = STATUS;"); connection.Open(); string clientname = TextBox1.Text; string sqlquery = ("INSERT INTO [STATUS] (Client_Name) VALUES ('" + TextBox1.Text + "')"); SqlCommand command = new SqlCommand(sqlquery, connection); command.Parameters.AddWithValue("Client_Name", clientname); command.ExecuteNonQuery(); }smirnov
All-Star
24614 Points
4192 Posts
Re: Trouble with SQL connection string
Jan 25, 2012 09:04 PM|LINK
Using "server = brandon" is definitely wrong, because it's only the name of instance. You need to use "server=Brandon-PC\brandon", or "localhost\brandon". If this doesn't help - try to connect to the database, for example, with VS.NET (Tools - Connect to Database) and see if it works. If it doesn't work, see if remote connections were enabled.
b471code3
Star
13877 Points
2598 Posts
Re: Trouble with SQL connection string
Jan 25, 2012 09:06 PM|LINK
Try something like this:
RageQwit
Member
15 Points
11 Posts
Re: Trouble with SQL connection string
Jan 25, 2012 09:20 PM|LINK
Smirnov, I'm able to connect to the STATUS db using the using Tools>>Connect
The owner of the db is Brandon, but there is no password for that account. I created that Test account because of this. I know the name and password are correct
B471code, that looks like a connection string within SQL, will that work for a C# webform?
smirnov
All-Star
24614 Points
4192 Posts
Re: Trouble with SQL connection string
Jan 25, 2012 09:31 PM|LINK
The error didn't say about password - "Verify that the instance name is correct and that SQL Server is configured to allow remote connections". Your sql connection string is ok, but not well-formed (take a look at connectionstrings.com for right syntax).
b471code3
Star
13877 Points
2598 Posts
Re: Trouble with SQL connection string
Jan 25, 2012 09:34 PM|LINK
RageQwit
Member
15 Points
11 Posts
Re: Trouble with SQL connection string
Jan 25, 2012 09:42 PM|LINK
It is configured to allow remote connections. I've checked in SSMS and via the method you provided in VS.
Now my code generates the error: Format of the initialization string does not conform to specifiaction starting at index 19.
When you say my connection string is not well formed, do you mean it should be like : datasource=;initial_catalog=; etc?
RageQwit
Member
15 Points
11 Posts
Re: Trouble with SQL connection string
Jan 25, 2012 09:53 PM|LINK
I don't know why it wouldn't work, and I guess I should've tried it. I just did and i got the same error. As I tweak the code, the error seems to change from, "Format initialization string does not conform to specification starting at index XX" where XX is 18,19, or 21.
Is there somewhere I can check these specifications?
smirnov
All-Star
24614 Points
4192 Posts
Re: Trouble with SQL connection string
Jan 26, 2012 11:40 AM|LINK
What is your connection string that caused "Format initialization string does not conform to specification"?
Again, you can look at connectionstrings.com for right syntax.