nowandever29
MIS
Hi, I need help! I'm working on learning VB.net and ADO.net, but I'm getting an error when I compile and run the following code. I THINK it's on the Dim Row as Datarow line, because one time when I tried to compile the code, I got a compile error on that line. But every other time I compile and run, I get no compile error, just the runtime error below. But the book I'm using, and everything else I've read, says that the line is right. Please help!
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Dim DS As New DataSet
Dim Con As New SqlConnection("data source=SQLSERVER05;initial catalog=DB02;user id=*;pwd=*")
Dim Adap As New SqlDataAdapter("SELECT * FROM ArticleCategories", Con)
Con.Open()
Try
Adap.Fill(DS, "Categories")
Catch ex As Exception
Finally
Con.Close()
End Try
PH.Controls.Clear()
Dim TempStr As String = "Category ID: (1), Category Name: (2)"
Dim Row As DataRow
For Each Row In DS.Tables("Categories").Rows
Dim Lbl As New LiteralControl
Lbl.Text = String.Format(TempStr, Row("CategoryID"), Row("CategoryName"))
PH.Controls.Add(Lbl)
Next
If PH.Controls.Count = 0 Then PH.Visible = False Else PH.Visible = True
End Sub
The error I get is:
Server Error in '/' Application.
--------------------------------------------------------------------------------
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
MyWeb.addcategory.Page_Load(Object sender, EventArgs e)
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Page.ProcessRequestMain() +750
--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:1.1.4322.2032; ASP.NET Version:1.1.4322.2032
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Dim DS As New DataSet
Dim Con As New SqlConnection("data source=SQLSERVER05;initial catalog=DB02;user id=*;pwd=*")
Dim Adap As New SqlDataAdapter("SELECT * FROM ArticleCategories", Con)
Con.Open()
Try
Adap.Fill(DS, "Categories")
Catch ex As Exception
Finally
Con.Close()
End Try
PH.Controls.Clear()
Dim TempStr As String = "Category ID: (1), Category Name: (2)"
Dim Row As DataRow
For Each Row In DS.Tables("Categories").Rows
Dim Lbl As New LiteralControl
Lbl.Text = String.Format(TempStr, Row("CategoryID"), Row("CategoryName"))
PH.Controls.Add(Lbl)
Next
If PH.Controls.Count = 0 Then PH.Visible = False Else PH.Visible = True
End Sub
The error I get is:
Server Error in '/' Application.
--------------------------------------------------------------------------------
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
MyWeb.addcategory.Page_Load(Object sender, EventArgs e)
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Page.ProcessRequestMain() +750
--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:1.1.4322.2032; ASP.NET Version:1.1.4322.2032