2013-03-29 60 views
0

我的背景是PHP/MySQL,但我目前正在嘗試學習如何使用ASP.Net與MySQL。我創建了一個允許用戶添加任務的小應用程序。我試圖在按下按鈕時檢索任務的完整列表。有一次,我得到了第一行的大約一半顯示的結果,沒有其他任何東西(taskid & createdate)。這不再顯示,我不知道爲什麼。如何在ASP.Net中使用DataGrid顯示來自mysql數據庫的數據?

有沒有人有任何建議或信息可以幫助我解決這個問題?

您可以在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 

在此先感謝,

彼得

+0

爲什麼使用舊的'DataGrid'控件而不是'GridView'?你還在ASP.NET 1.1上嗎? http://msdn.microsoft.com/en-gb/magazine/cc163933.aspx –

+0

我試圖用你的代碼重新創建問題。並得到了幾個錯誤。你可以在課後發佈完整的代碼嗎? –

+0

只是想對那些回答這個問題的人表示感謝。他們的建議,建議和幫助非常感謝。 – galtech

回答

相關問題