恭喜拿到驾照的句子:未将对象引用设置到对象的实例

来源:百度文库 编辑:查人人中国名人网 时间:2024/04/30 03:06:46
各位老大,小弟在自学ASP.NET下面这段是在VISUAL STUDIO.NET里做的一个简易留言板的显示段但是出现了这个错误“未将对象引用设置到对象的实例”
源错误:

行 43: MyAdpt.Fill(MyDs, "1")
行 44:
行 45: DataGrid1.DataSource = MyDs.Tables("1").DefaultView
行 46: DataGrid1.DataBind()
行 47: MyCon.Close()

我编的代码如下:
Imports System.Data
Imports System.Data.OleDb
Public Class WebForm1
Inherits System.Web.UI.Page

Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
Protected WithEvents TextBox2 As System.Web.UI.WebControls.TextBox
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Protected WithEvents Button2 As System.Web.UI.WebControls.Button
Protected WithEvents Label1 As System.Web.UI.WebControls.Label
Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
#Region " Web 窗体设计器生成的代码 "

'该调用是 Web 窗体设计器所必需的。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub

'注意: 以下占位符声明是 Web 窗体设计器所必需的。
'不要删除或移动它。
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
'不要使用代码编辑器修改它。
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
Dim MyCon As New OleDbConnection
Dim MyAdpt As New OleDbDataAdapter
Dim MyDs As New DataSet

MyCon.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" _
& Server.MapPath("liuyan.mdb")

MyAdpt.SelectCommand = New OleDbCommand
MyAdpt.SelectCommand.CommandText = "select * from table1"
MyAdpt.SelectCommand.Connection = MyCon
MyAdpt.Fill(MyDs, "1")

DataGrid1.DataSource = MyDs.Tables("1").DefaultView
DataGrid1.DataBind()
MyCon.Close()

End If '在此处放置初始化页的用户代码
End Sub

End Class

应该是你没有打开连接吧
MyCon.Open() '这里补上这一句
行 43: MyAdpt.Fill(MyDs, "1")
行 44:
行 45: DataGrid1.DataSource = MyDs.Tables("1").DefaultView
行 46: DataGrid1.DataBind()
行 47: MyCon.Close()