我的背景是 PHP / MySQL,但我目前正在尝试学习如何将 ASP.Net 与 MySQL 一起使用。我创建了一个允许用户添加任务的小型应用程序。我试图在按下按钮时检索完整的任务列表。有一次,我得到的结果是第一行大约有一半显示,没有其他内容(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
提前致谢,
彼得