Thursday, March 29, 2012

Object Reference Blah Blah & Code Behind

I am getting this error:
Object reference not set to an instance of an object

On this line of my code:

dgNewsItems.DataSource = myDataSet.Tables("News")

I can't see what the problem would be...

Here's my full code:
News.aspx

<%@dotnet.itags.org. Page Language="VB" Debug="true" Inherits="clsNews" src="http://pics.10026.com/?src=..\code\news.vb" %
<html>
<head>
<title>Admin - News</title>
</head>
<body>
<asp:datagrid ID="dgNewsItems" runat="server" />
</body>
</html>

news.vb

Imports System
Imports System.Configuration
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data
Imports System.Data.OleDb

Public Class clsNews : Inherits Page
'Declare public variables for .aspx file to inherit
Dim dgNewsItems as DataGrid

Sub Page_Load(sender as Object, e as EventArgs)
If Not Page.IsPostBack Then
FillDataGrid()
End If
End Sub 'Page_Load

Private Sub FillDataGrid()
Dim strConn as String = ConfigurationSettings.AppSettings("ConnectionString")
Dim strCmd as String = "SELECT * FROM News"
Dim myDataSet as New DataSet()

Dim myConn as New OleDbConnection(strConn)
Dim myCmd as New OleDbDataAdapter(strCmd, myConn)

myConn.Open()
myCmd.Fill(myDataSet, "News")

dgNewsItems.DataSource = myDataSet.Tables("News")
dgNewsItems.DataBind()
myConn.Close()
End Sub 'FillDataGrid
End Class

Cheers
Charles VTry giving
myCmd.Fill(myDataSet, "News1")
dgNewsItems.DataSource = myDataSet.Tables("News1")

or
dgNewsItems.DataSource = myDataSet.Tables(0)

or
dgNewsItems.DataSource = myDataSet

does that make difference??

Have you changed the statement as
<%@. Page Language="VB" Debug="true" Inherits="clsNews" src="http://pics.10026.com/?src=..\code\news.vb" %
hmm

None of those made any difference...

What do you mean by 'Have you changed the statement as'?

Charles V
I found the error

This line:
Dim myCmd as New OleDbDataAdapter(strCmd, myConn)

Is supposed to be:
Dim myCmd as OleDbDataAdapter = New OleDbDataAdapter(strCmd, myConn)

So simple........

0 comments:

Post a Comment