2013-04-08 46 views
0

数据表运行不正常,甚至导致我的动态表格布局不显示。我完全不知道哪里出了问题。顺便说一句,我是一个初学者。数据表运行不正常

Public Class ListItem 
Dim dt As System.Data.DataTable 


Private Sub ListItem_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load 
    Dim conn As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source =|DataDirectory|\SMS.accdb" 
    Dim sqlstr As String = "Select * from ItemList" 
    Dim dtad As New OleDb.OleDbDataAdapter(sqlstr, conn) 
    dtad.Fill(dt) 
    dtad.Dispose() 
End Sub 

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click 
    Dim ListTable As New TableLayoutPanel() 
    ListTable.AutoSize = True 
    ListTable.AutoSizeMode = Windows.Forms.AutoSizeMode.GrowAndShrink 
    ListTable.Location = New Point(20, 20) 
    ListTable.BackColor = Color.White 
    ListTable.ColumnCount = CInt(dt.Columns.Count) 
    ListTable.RowCount = CInt(dt.Rows.Count) 
    ListTable.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single 


    For colindex = 0 To dt.Columns.Count - 1 
     For rowindex = 0 To 0 
      Dim newlabel As New Label() 
      newlabel.Location = New Point(10, 10) 
      newlabel.Name = "label" & colindex 
      newlabel.Font = New Drawing.Font("Microsoft Sans Serif", 16, FontStyle.Underline) 
      newlabel.Text = dt.Columns(colindex).ColumnName 
      newlabel.AutoSize = True 
      ListTable.Controls.Add(newlabel, colindex, rowindex) 
     Next 
    Next 

    Controls.Add(ListTable) 
End Sub 
End Class 

它给我这个错误:

An unhandled exception of type 'System.NullReferenceException' occurred in StockManagementSystem.exe 

Additional information: Object reference not set to an instance of an object. 

回答

1

在类中第一行。从这一变化是:

Dim dt As System.Data.DataTable 

这样:

Dim dt As New System.Data.DataTable 
+0

哦,THX了很多〜我傻〜 – K3v 2013-04-08 14:54:02