none
error: 40 - Could not open a connection to SQL Server RRS feed

  • Question

  • hi all
            when i am trying to run the program i am getting one error"An error has occurred while establishing a connection to the server.  When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) ". i have checked  configuration manager & sql server server area configuration .every thing is eanble as usual. please help me to solve the problem . this is the code .
     


    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;
    using System.Data.Odbc;

    public partial class Webform1 : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.GridView dgEmp;
        protected datav ucdataNavigator;
        public static readonly int page_size = 25;

        private void Page_Init(object sender, System.EventArgs e)
        {
            ucdataNavigator.CurrentPage=1;
        }


        private void Page_Load(object sender, System.EventArgs e)
        {
            // Put user code to initialize the page here
            if (!Page.IsPostBack)
            {
                BindGrid();
            }
        }

        private void BindGrid()
            {
                int totalCount;

                SqlConnection con = new SqlConnection("server= nkshukla-mobl0\\sqlexpress;database=master;Trusted_Connection=yes;Integrated Security=false");
                 SqlDataAdapter cmd = new SqlDataAdapter("select  policy_id,agent,agent_city,ModelName,product,region,NetPremium,Gross_premium from test where policy_id < 1000  order by policy_id"; con);
                 cmd.CommandType = CommandType.TableDirect;
              
                SqlParameter param = cmd.Parameters.Add("@CurrentPage", SqlDbType.Int);
                param.Direction = ParameterDirection.Input;
                param.Value = ucDataNavigator.CurrentPage;
              
                param = cmd.Parameters.Add("@PageSize", SqlDbType.Int);
                param.Direction = ParameterDirection.Input;
                param.Value = PAGE_SIZE;


               param = cmd.Parameters.Add("@TotalRecords", SqlDbType.Int);
                param .Direction = ParameterDirection.Output;
            
               SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                totalCount = (int)cmd.Parameters["@TotalRecords"].Value;
                if ((totalCount % PAGE_SIZE) == 0)
                    ucDataNavigator.TotalPages = totalCount / PAGE_SIZE;
                else
                    ucDataNavigator.TotalPages = (totalCount / PAGE_SIZE) + 1;

                if ((dgEmp.CurrentPageIndex <= ucDataNavigator.TotalPages) && (ucDataNavigator.TotalPages > 1))
                {
                    ucDataNavigator.NextButtonEnabled = true;
                    ucDataNavigator.NextButtonImageUrl = "Images/NavNextPage.gif";
                    ucDataNavigator.LastButtonEnabled = true;
                    ucDataNavigator.LastButtonImageUrl = "Images/NavLastPage.gif";
                }

                da.Dispose();
                cmd.Dispose();
                con.Close();
                con.Dispose();
            }
        protected void PageChanged(object sender, DataNavigatorEventArgs e)
        {
           
            ucDataNavigator.CurrentPage = e.CurrentPage;
            BindGrid();
            EnableDisableButtons(e.TotalPages);
        }

         protected void FirstPage(object sender, DataNavigatorEventArgs e)
            {
                // Decrement the current page index.
                if (e.CurrentPage > 1)
                {
                    ucdataNavigator.
                    // Get the data for the DataGrid.
                    BindGrid();

                    EnableDisableButtons(e.TotalPages);
                }

            }

         private void EnableDisableButtons(int iTotalPages)
            {
                // Should we disable the first link?
                ucDataNavigator.FirstButtonEnabled = (ucDataNavigator.CurrentPage == 1) ? false : true;
                ucDataNavigator.FirstButtonImageUrl = (ucDataNavigator.CurrentPage == 1) ? "Images/NavFirstPageDisabled.gif" : "Images/NavFirstPage.gif";

                // Should we disable the previous link?
                ucDataNavigator.PreviousButtonEnabled = (ucDataNavigator.CurrentPage == 1) ? false : true;
                ucDataNavigator.PreviousButtonImageUrl = (ucDataNavigator.CurrentPage == 1) ? "Images/NavPreviousPageDisabled.gif" : "Images/NavPreviousPage.gif";

                // Should we enable the next link?
                ucDataNavigator.NextButtonEnabled = (ucDataNavigator.CurrentPage < iTotalPages) ? true : false;
                ucDataNavigator.NextButtonImageUrl = (ucDataNavigator.CurrentPage < iTotalPages) ? "Images/NavNextPage.gif" : "Images/NavNextPageDisabled.gif";

                // Should we enable the last link?
                ucDataNavigator.LastButtonEnabled = (ucDataNavigator.CurrentPage < iTotalPages) ? true : false;
                ucDataNavigator.LastButtonImageUrl = (ucDataNavigator.CurrentPage < iTotalPages) ? "Images/NavLastPage.gif" : "Images/NavLastPageDisabled.gif";
            }
    }




    thanks in advance
                                                                                                                                                                                          regard
                                                                                                                                                                                        Neeraj k shukla
    Wednesday, April 22, 2009 4:36 AM

Answers

  • hi VMajure
                   actually i have created my table in master Database so i want to connect to masterdatabade.
                    Thanks for response
                                                                                                                                Neeraj shukla
     
    • Marked as answer by neeraj msrit Wednesday, December 23, 2009 9:35 AM
    Friday, April 24, 2009 10:49 AM

All replies

  • A bit hard to tell from this information.
    The reason you are getting Named Pipes is because the client tries on the different protocols in the order specified in the client protocols.
    Usually Named Pipes is the last protocol.

    One thing that is strange is that you have Trusted_Connection=yes AND Integrated Security=false ?
    I have not tested but happens when this is the case. But use one only.

    "SqlConnection.ConnectionString Property"
    http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring.aspx

    Some other resources:

    "Named Pipes Provider, error: 40 - Could not open a connection to SQL Server"
    http://blogs.msdn.com/sql_protocols/archive/2007/03/31/named-pipes-provider-error-40-could-not-open-a-connection-to-sql-server.aspx

    "Named Pipes Provider, error: 40 - Could not open a connection to SQL Server (Microsoft SQL Server, Error: XXX)"
    http://blogs.msdn.com/sql_protocols/archive/2007/05/16/named-pipes-provider-error-40-could-not-open-a-connection-to-sql-server-microsoft-sql-server-error-xxx.aspx

    //Michael


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Wednesday, April 22, 2009 7:41 AM
  • One addition. It is not related to the error you recieve, but your connection contains mutually exclusive properties - "Trusted_Connection" and "Integrated Security". Trusted connection is Integrated security. Both properties represent exact same functionality, but have different names for backward compatibility. Basically in one property you say it is integrated security and in another one you say no, it is not. You need to leave one to make it work properly.
    Val Mazur (MVP) http://www.xporttools.net
    Wednesday, April 22, 2009 10:15 AM
    Moderator
  • The error "An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections." is because Sql Server Browser is not enabled on the Server which hosts SqlExpress. You can find more information on how to enable it at the link http://support.microsoft.com/kb/914277.
    Wednesday, April 22, 2009 5:10 PM
    Moderator
  •   hi  all
                      i have done all these things as you advised but problem is not yet solved .same error  is coming .
                     thanks for response
                                                                                                                                                     Neeraj kumar shukla
                                 
    Friday, April 24, 2009 3:50 AM
  • Did you make SQL Express instance of the server visible to the network? Can you open SQL Server Management Studio from your computer and try to connect to the server from there? One more question - why are you connecting to the master database? You need to specify database name that contains your tables, not the master.
    Val Mazur (MVP) http://www.xporttools.net
    Friday, April 24, 2009 10:21 AM
    Moderator
  • hi VMajure
                   actually i have created my table in master Database so i want to connect to masterdatabade.
                    Thanks for response
                                                                                                                                Neeraj shukla
     
    • Marked as answer by neeraj msrit Wednesday, December 23, 2009 9:35 AM
    Friday, April 24, 2009 10:49 AM
  • Did you check if server is available on the network?
    Creating table in master database is dangerous, since you allow access to the core database of the server to anyone. What would be the reason to do it instead of creating table in your own database?
    Val Mazur (MVP) http://www.xporttools.net
    Monday, April 27, 2009 10:08 AM
    Moderator