如何在ASP.Net中使用DataGrid显示mysql数据库中的数据?

时间:2013-03-29 22:46:13

标签: asp.net

我的背景是PHP / MySQL,但我目前正在尝试学习如何在MySQL中使用ASP.Net。我创建了一个允许用户添加任务的小应用程序。我试图在按下按钮时检索完整的任务列表。有一次,我得到了一个结果,第一行的大约一半是显示而没有其他的(taskid& createdate)。那不再出现了,我不知道为什么。

有人会有任何建议或信息可以帮助我解决这个问题吗?

您可以在www.galtechsolutions.net/tasklist

查看该应用程序

我的标记是:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="viewtasks.aspx.vb" Inherits="MyTasks.ViewTasks" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<link href="style.css" rel="Stylesheet" media="screen" />
<title>TaskList - View Tasks</title>
</head>
<body>
<a href="default.aspx">Home</a> | <a href="addtask.aspx">Add Task</a> | <a href="viewtasks.aspx">View Tasks</a>
<br /><br />
<form id="form1" runat="server">
<div>
    <asp:Button ID="gettasks" runat="server" Text="Show Tasks" class="taskbutton" />
    <br /><br />
    <asp:DataGrid ID="DataGridView1" runat="server" PageSize="5" AllowPaging="True" 
        DataKeyField="taskid" AllowSorting="True" 
        EmptyDataText="There are no data records to display" AutoGenerateColumns="false">
    <Columns>
    <asp:BoundColumn HeaderText="taskid" DataField="taskid"></asp:BoundColumn>
    <asp:BoundColumn HeaderText="createdate" DataField="createdate"></asp:BoundColumn>
    <asp:BoundColumn HeaderText="taskname" DataField="taskname="></asp:BoundColumn>
    <asp:BoundColumn HeaderText="taskdesc" DataField="taskdesc"></asp:BoundColumn>
    <asp:BoundColumn HeaderText="duedate" DataField="duedate"></asp:BoundColumn>
    </Columns>
    </asp:DataGrid>      
    <asp:Label ID="gettaskslabel" runat="server"></asp:Label>
    <br />
    <br />
</div>
</form>
</body>
</html>

我的代码背后是:

Protected Sub gettasks_Click(sender As Object, e As EventArgs) Handles gettasks.Click

    Dim conn As MySqlConnection
    Dim selcmd As MySqlCommand
    Dim SQL As String

    conn = New MySqlConnection("Data Source=phaspdev1.db.10654019.hostedresource.com;Database=phaspdev1;User Id=;Password=;")
    selcmd = New MySqlCommand

    Try
        conn.Open()
        SQL = "SELECT * FROM mytasks"
        selcmd.Connection = conn
        selcmd.CommandText = SQL

        dtadapter.SelectCommand = selcmd
        dtadapter.Fill(dt)


        DataGridView1.DataSource = dt
        DataGridView1.DataBind()
        DataGridView1.DataSource = dt.Columns.Add("taskid", GetType(Integer))
        DataGridView1.DataSource = dt.Columns.Add("createdate", GetType(Date))
        DataGridView1.DataSource = dt.Columns.Add("taskname", GetType(String))
        DataGridView1.DataSource = dt.Columns.Add("taskdesc", GetType(String))
        DataGridView1.DataSource = dt.Columns.Add("duedate", GetType(Date))

        For Each dr As DataRow In dt.Rows
            dt.Rows.Add(dr)

        Next dr
        dt.AcceptChanges()

    Catch ex As Exception
        Console.WriteLine("Cannot connect to database: " & ex.Message)
    Finally
        conn.Close()
        conn.Dispose()

    End Try

End Sub

提前致谢,

彼得

1 个答案:

答案 0 :(得分:0)

尝试将autoeventwireup设置为true。听起来你没有在按钮上设置事件处理程序。

http://msdn.microsoft.com/en-us/library/system.web.configuration.pagessection.autoeventwireup.aspx