locked
Trouble with SQL connection string RRS feed

  • Question

  • User-697895819 posted

     

    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.
      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();
            }

     

    Wednesday, January 25, 2012 4:49 PM

All replies

  • User1508394307 posted

    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

    Wednesday, January 25, 2012 5:04 PM
  • User-1226263862 posted

    Try something like this:

    Data Source=ServerName;Initial Catalog=DataBaseName;Persist Security Info=True; User ID=YourUserId; Password=YourPass;

     

    Wednesday, January 25, 2012 5:06 PM
  • User-697895819 posted

    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?

    Wednesday, January 25, 2012 5:20 PM
  • User1508394307 posted

    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).

    Wednesday, January 25, 2012 5:31 PM
  • User-1226263862 posted
    Why wouldn't it work? Its a SQL database correct? Why not get the connection string from the web.config file?
    Wednesday, January 25, 2012 5:34 PM
  • User-697895819 posted

    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?

    Wednesday, January 25, 2012 5:42 PM
  • User-697895819 posted

    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?

    Wednesday, January 25, 2012 5:53 PM
  • User1508394307 posted

    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.

    Thursday, January 26, 2012 7:40 AM