A network-related or instance-specific error occurred while establishing a connection

Discussion in 'Databases' started by stumac, Jul 4, 2012.

Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.
  1. It's late and I'm probably doing something stupid but.....................

    Can connect to my SQL Server 2008 database using Microsoft SQL Server Management Studio.
    Can also connect to my SQL Server 2008 database using my connections string set in my web.config file, when using the debug option in VS2008.
    Have now uploaded it to my code server, which is seperate from the DiscountASP.net server and get the message in the title.

    Have checked the web.config and the file is unsurprisingly the same (as I purely did an upload via FTP) which includes the connection string which works from VS2008.

    So what are my weary eyes overlooking?

    Thanks in advance.
     
  2. RayH

    RayH DiscountASP.NET Lackey DiscountASP.NET Staff

    Is your code referencing the connection string in the web.config properly?
     
  3. Apologies for delayed reply

    Hi Tasslehoff,
    Sorry for the delay in replying.

    So this is the story.
    I'm primarily VB.NET programmer but coding this in C#. After all it's basically syntax, not logic (loose statement).

    My web.config file is as follows (with sensitive info removed);
    <add name="my_cs" connectionString="Data Source=tcp:xxxx;Initial Catalog=xxxx;User ID=xxxx;Password=xxxx;" providerName="System.Data.SqlClient"/>
    Not useful with all the xxxx's I know but this is how it's formatted. And I can verify that the details contained within the xxxx's is correct as I can connect to the database using these details via SQL Server Management Studio.

    My code;
    It starts with checking a user name for new registration;
    actions.customer cust = new actions.customer();
    cust.UserName = Request.Form["new_username"];
    cust.Pword = Request.Form["new_pword"];

    cust.checkUsername();

    The customer class is as follows;
    public void checkUsername()
    {
    clayola_database cd = new clayola_database();

    SQLStmt = "xxxx";

    sqlparams.Clear();
    sqlvalues.Clear();

    sqlparams.Add("@username");
    sqlparams.Add("@pword");

    sqlvalues.Add(varUserName);
    sqlvalues.Add(varPword);

    if (cd.DBReader(SQLStmt, sqlparams, sqlvalues, ref DR) == true)
    {
    while (DR.Read())
    {
    varReturnBool = Convert.ToBoolean(DR[0]);
    }
    }
    else
    {
    varReturnBool = true;
    }
    }

    Finally the database class is;
    (this is the exact same code I use in my VB.NET projects, I've just 're-syntaxed' it);

    public bool DBReader(string StoredProcedure,ArrayList sqlparams,ArrayList sqlvalues,ref SqlDataReader ThisDR)
    {
    if (StoredProcedure != "")
    {
    SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["my_cs"].ConnectionString);
    SqlCommand comm = new SqlCommand();
    int numparams;

    comm.Connection = conn;
    comm.CommandText = StoredProcedure;
    comm.CommandType = CommandType.StoredProcedure;

    numparams = sqlparams.Count;

    switch (StoredProcedure)
    {
    case "xxxx":
    comm.Parameters.Add(sqlparams[0].ToString(),SqlDbType.VarChar);
    comm.Parameters[sqlparams[0].ToString()].Direction = ParameterDirection.Input;
    comm.Parameters[sqlparams[0].ToString()].Value = sqlvalues[0];
    comm.Parameters.Add(sqlparams[1].ToString(), SqlDbType.VarChar);
    comm.Parameters[sqlparams[1].ToString()].Direction = ParameterDirection.Input;
    comm.Parameters[sqlparams[1].ToString()].Value = sqlvalues[1];
    break;
    }

    try
    {
    conn.Open();
    ThisDR = comm.ExecuteReader();
    return true;
    }
    catch (Exception ex)
    {
    //Console.Write(ex.ToString());
    return false;
    }
    finally
    {
    //conn.Close();
    }
    }
    else
    {
    return false;
    }
    }

    Now it may be a c# newbie mistake.
    But debugging with Visual Studio, the code does step through to completion. But of course VS suppresses certain exceptions to allow debugging so no surprise. The crux of the problem is the conn.open().

    What am I doing wrong?
    Or what settings have I missed?

    Thanks for your time.
     
  4. RayH

    RayH DiscountASP.NET Lackey DiscountASP.NET Staff

    Actually, I'm not exactly sure what your code is doing. Are you trying to execute a stored procedure on your database? The way I usually do it is:

    comm.ExecuteNonQuery();

    But the error message you're getting "A network-related or instance-specific error occurred while establishing a connection" usually implies that it's connecting to an SQL Server instance that doesn't exist (e.g. Error 26 -> trying to connect to SQL Server Express instance) or it can't make a TCP/IP connection to the database server itself (e.g. Error 40 -> maybe the server name is misspelled, etc.)
     
  5. Connecting

    Yeah it's trying to run a stored procedure. A new customer comes along to register and has to enter a username. This stored procedure basically checks to see if that username exists.

    comm.ExecuteReader runs the stored procedure to build a SQLDataReader so I can look at the returns.
    http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executereader(v=vs.71).aspx
    ExecuteNonQuery is only for inserts,updates and deletes
    http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery.aspx

    Regardless of this, the connection string I use has the exact same information that I use to connect to the database (successfully) via SQL Server Management Studio. And I've double checked this a hundred times now just in case I was being stupid.

    This is a complete mystery to me and is very frustrating.:confused:
     
Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.

Share This Page