注册 登录
编程论坛 ASP.NET技术论坛

这几行代码哪错了?

lsz3344 发布于 2007-05-12 22:01, 601 次点击

Imports System.Data
Imports System.Data.SqlClient

Public Class read
Inherits System.Web.UI.Page
Dim conn As SqlConnection = New SqlConnection("database=new;uid=sa;pwd=;server=127.0.0.1")
Protected WithEvents Label2 As System.Web.UI.WebControls.Label
Protected WithEvents printScript As System.Web.UI.HtmlControls.HtmlGenericControl

Dim ds As New DataSet

#Region " Web 窗体设计器生成的代码 "

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

End Sub
Protected WithEvents TimeLabel As System.Web.UI.WebControls.Label
Protected WithEvents AuthorLabel As System.Web.UI.WebControls.Label
Protected WithEvents PreviousLink As System.Web.UI.WebControls.Literal
Protected WithEvents NextLink As System.Web.UI.WebControls.Literal
Protected WithEvents numPages As System.Web.UI.HtmlControls.HtmlGenericControl
Protected WithEvents Label1 As System.Web.UI.WebControls.Label

'注意: 以下占位符声明是 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 Session("pass") <> "ok" Then
Response.Redirect("login.aspx")
End If
Dim str1 As String = "select * from message1 where caption='" & Session("caption") & "'"
Dim da As New SqlDataAdapter(str1, conn)
Dim ds As New DataSet
da.Fill(ds, "usertable")
Label1.Text = RTrim(ds.Tables("message1").Rows(0).Item("caption"))
TimeLabel.Text = RTrim(ds.Tables("message1").Rows(0).Item("date1"))
AuthorLabel.Text = RTrim(ds.Tables("message1").Rows(0).Item("name"))
Label2.Text = RTrim(ds.Tables("message1").Rows(0).Item("meat"))

End Sub

End Class



“/WebApplication1”应用程序中的服务器错误。
--------------------------------------------------------------------------------

未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。

源错误:


行 43: Dim ds As New DataSet
行 44: da.Fill(ds, "usertable")
行 45: Label1.Text = RTrim(ds.Tables("message1").Rows(0).Item("caption"))
行 46: TimeLabel.Text = RTrim(ds.Tables("message1").Rows(0).Item("date1"))
行 47: AuthorLabel.Text = RTrim(ds.Tables("message1").Rows(0).Item("name"))

8 回复
#2
beniao2007-05-12 23:10
不懂VB``无能为力`
#3
从小到大2007-05-12 23:16
我也不懂 不过应该是dataSet的问题 ds.Tables("message1").
#4
moderndai2007-05-13 13:06
Label1.Text = RTrim(ds.Tables("usertable").Rows(0).Item("caption"))
TimeLabel.Text = RTrim(ds.Tables("usertable").Rows(0).Item("date1"))
AuthorLabel.Text = RTrim(ds.Tables("usertable").Rows(0).Item("name"))
Label2.Text = RTrim(ds.Tables("usertable").Rows(0).Item("meat"))
#5
guming2007-05-14 12:47
da.Fill(ds, "usertable")//问题应该在这吧,怎么会是usertable不是message1吗
#6
Kendy1234562007-05-14 17:27
2个问题

1 connection没open
2. 如同楼上 tables引用名字错 如果就这么一个表就用ds.tables(0)好了
#7
tel19822007-05-15 07:56
同意斑竹所说。
#8
xiezili2007-05-15 16:03

用SqlDataAdapter的话,connection就不用open。

上下二个表名不一致!

#9
bygg2007-05-15 16:46
是的,Connecting不用打开也行.
Label1.Text = RTrim(ds.Tables("message1").Rows(0).Item("caption").ToString())......试试这样.
1