简体   繁体   中英

ASP.NET page crashes with MySql connection

I am trying to write a dashboard with information about the website's hits and where people come from etc.

I wrote the following page:

<%@ Page Language="C#" Inherits="Daschboard.Board" MasterPageFile="~/site.master" %>        
<asp:Content id="main" runat="server" ContentPlaceHolderID="main">
<asp:UpdatePanel runat="server" id="updateableWidgets">
    <ContentTemplate>
        <asp:ContentPlaceHolder id="WidgetsTable">
        </asp:ContentPlaceHolder>
    </ContentTemplate>
</asp:UpdatePanel>
</asp:Content>

With the following code behind:

using Widgets;
using System;
using System.Text;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Configuration;
using MySql.Data.MySqlClient;

namespace Daschboard
{
public partial class Board : System.Web.UI.Page
{
    protected List<IWidget> widgets;
    private MySqlConnection db = new MySqlConnection (ConfigurationManager.AppSettings ["mySql"]);

    protected void Page_Load(object sender, EventArgs e)
    {
        widgets = new List<IWidget>();
        BuildWidgtTable();
        var WidgetThread = new System.Threading.Thread(new System.Threading.ThreadStart(this.InitWidgets));
        WidgetThread.Start();
    }

    private string BuildWidgtTable ()
    {
        StringBuilder sb = new StringBuilder ();
        db.Open ();
        var tableName = ConfigurationManager.AppSettings ["DatabaseSiteName"];
        var query = "SELECT * FROM " + tableName + ", Widget WHERE " + tableName + ".WidgetID = Widget.WidgetID";
        var command = new MySqlCommand (query, db);
        var result = command.ExecuteReader ();
        while (result.Read()) {
            widgets.Add(getWidget(result.GetString(4)));
        }
        db.Close ();

        sb.Append("<table>\n");

        sb.Append("</table>");

        result.Close();
        return sb.ToString();
    }

    private void InitWidgets ()
    {
        db.Open();
        foreach (var widget in widgets) {
            var siteId = ConfigurationManager.AppSettings["siteID"];
            var query = "SELECT * FROM Values, Widget WHERE       Widget.WidgetID = Values.WidgetID AND SiteID = " + siteId;
            var command = new MySqlCommand(query, db);
            var result = command.ExecuteReader();
            while(result.Read()){
                Console.WriteLine(result);
            }
            result.Close();
            widget.Init ("1");
        }
        db.Close();
    }

    private IWidget getWidget (string fileName)
    {
        IWidget widget = null;
        switch (fileName) {
        case "PageHits":
            widget = new PageHits();
            break;
        case "SiteHits":
            widget = new SiteHits();
            break;
        }
        return widget;
    }
}
}

When I run this something goes wrong; the debugger stops working without a good error. It does load the widgets from the database but it doesn't get the values from the database.

I am using Monodevelop on a Mac.

You are running the widget initialization method in a separate thread ( InitWidgets method). The page might have already finished rendering when this method executes. If you want to use asynchronous operations in ASP.NET I would recommend you reading about Asynchronous Pages on MSDN. The idea is to mark your WebForm as asynchronous by setting Async="true" in your Page directive ( <%@ Page Async="true" ... %> ) and then use the RegisterAsyncTask method on your web form.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM