locked
there no connection to server RRS feed

  • Question

  • User2062956768 posted

    friends

    1) how can I display a message box or any alert to  the user that the connection to server DB disconnected know or server is the power of 

    using C# ASP

    2) the internet connection is disconnected

    I prefer shortcode 

    Thank you so much 

    Saturday, July 6, 2019 2:43 AM

All replies

  • User475983607 posted

    friends

    1) how can I display a message box or any alert to  the user that the connection to server DB disconnected know or server is the power of 

    using C# ASP

    2) the internet connection is disconnected

    I prefer shortcode 

    Thank you so much 

    Very simple, try to open a connection.  Return a message to the user if there is an error.

    Keep in mind that web applications do not not keep an open connection.  The connections are made in code - that your write - in response to an HTTP request.

    If you need a persistent connection then look into SignalR.

    https://dotnet.microsoft.com/apps/aspnet/real-time

    Saturday, July 6, 2019 12:13 PM
  • User1120430333 posted

    IMO,  you don't say that the DB is down, and you should be showing a user friendly error message about, 'There was a problem and contact support or try again later.',  after you log the exception to a log file like using Log4Net so that you can review what happened.

    If you use Global Exception Handling implemented at the Global.asax, you don't need a try/catch anywhere in any code, which makes them unhandled exceptions when thrown, that will be caught by the GEH logic. GEH logs the exception and does a redirect to a user friendly error page.

    https://stackify.com/csharp-catch-all-exceptions/

    https://stackify.com/log4net-guide-dotnet-logging/

    Saturday, July 6, 2019 12:24 PM
  • User-719153870 posted

    Hi mrzoz,

    According to your needs, I made a demo. In this demo, when the page is loaded, it needs to connect to the database to fill in the data of the Gridview in the page.

    After obtaining the data, it closes the connection with the database. At this time, it alerts the message that disconnects from the database.

    We can use Response.Write ("<script>alert ('The connection to DB is closed!')</script>") to display the alert box.

    Please refer to below codes:

    ASPX:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
                    <Columns>
                        <asp:BoundField DataField="UID" HeaderText="UserID" ReadOnly="true" />
                        <asp:BoundField DataField="UName" HeaderText="UserName" />
                        <asp:BoundField DataField="UAge" HeaderText="UserAge" />
                    </Columns>
                </asp:GridView>
            </div>
        </form>
    </body>
    </html>

    CS:

    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    GetData();
                }
            }
            public string connstring = "Data Source=.;Initial Catalog=DatabaseTestPool;Integrated Security=True";
    
            protected void GetData()
            {
                using (SqlConnection conn = new SqlConnection(connstring))
                {
                    conn.Open();
                    string sql = "select UID,UName,UAge from Users";
                    SqlDataAdapter ad = new SqlDataAdapter(sql, conn);
                    DataSet ds = new DataSet();
                    ad.Fill(ds, "Users");
                    GridView1.DataSource = ds;
                    GridView1.DataBind();
                    try
                    {
                        conn.Close();
                        Response.Write("<script>alert('The connection to DB is closed!')</script>");
                    }
                    catch { }
                }
            }

    Here is result of this demo:

    But in general, we do not prompt the connection failure directly in try but in catch when there is a problem connecting to the database.

    Please refer to below codes:

    CS:

    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    GetData();
                }
            }
            public string connstring = "Data Source=.;Initial Catalog=DatabaseTestPool;Integrated Security=True";
    
            protected void GetData()
            {
                using (SqlConnection conn = new SqlConnection(connstring))
                {
                    conn.Open();
    try { string sql = "select UID,UName,UAge from Users"; SqlDataAdapter ad = new SqlDataAdapter(sql, conn); DataSet ds = new DataSet(); ad.Fill(ds, "Users"); GridView1.DataSource = ds; GridView1.DataBind(); //conn.Close();//In the real case, we may not close the connection on our own initiative } catch {
    Response.Write("<script>alert('The connection to DB is closed!')</script>");
    } } }

    As for your second point, as mgebhard said, the web form's web page is not always connected to the server.

    When the client submits a request to the server, the server sends the corresponding web page or event to the client.

    Therefore in this situation, it is not necessary to display the if network is disconnected.

    Best Regard,

    Yang Shen

    Monday, July 8, 2019 6:05 AM