Object reference not set to an instance of an object

G

Guest

Hi,

I am creating in an ASP Page Load event an empty DataTable in a DataSet as follows:


Imports System
Imports System.Web.UI.WebControls
Imports System.Data
Imports System.Data.SqlClient
Imports System.Collections
Imports System.Text


Namespace WebInterface

Public Class DataEntry

Protected objGridDataSet As New DataSet()

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

If Not Page.IsPostBack Then
Dim objConnect As New SqlConnection(ConfigurationSettings.AppSettings(WebInterface.Global.CfgKeyConnString))
Dim objDataAdapter As New SqlDataAdapter("SELECT * FROM tblEntries", objConnect)
objConnect.Open()
objDataAdapter.FillSchema(objGridDataSet, SchemaType.Mapped, "tblEntries")
objConnect.Close()

'then adding an extra columt to the table ...
Dim objColumn As DataColumn
objColumn = objGridDataSet.Tables("tblExpenseEntries").Columns.Add("Name", System.Type.GetType ("System.String"))

End If


End Sub


Then in a Button_Click routine I want to add a data rows to the DataTable:


Private Sub AddEntry_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddEntry.Click

Dim objDataTable As DataTable
Dim objDataRow As DataRow
objDataTable = objGridDataSet.Tables("tblEntries")
objDataRow = objDataTable.NewRow()
objDataRow("EntryID") = 2
............................................
objDataRow("Description") = "Test Value"
objDataTable.Rows.Add(objDataRow)

End Sub

End Class

End Namespace


Page loads fine but when I click AddEntry button to add a row of data to my DataTable in DataSest, I get following eror:


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:


Line 413: objDataTable = objGridDataSet.Tables("tblEntries")
Line 414: objDataRow = objDataTable.NewRow()


Source File: c:\inetpub\wwwroot\Tracker\Entry.aspx.vb Line: 414

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an object.]
ExpenseTracker.WebInterface.ExpenseEntry.AddEntry_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\Tracker\Entry.aspx.vb:415
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +57
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain() +1263


When I place datarow addition code in Page Load rutine, it works fine (it adds the datarow to the DatTable)...


Any Idea why is breaking in AddEntry rutine ???

Many thanks for help....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,310
Messages
2,571,603
Members
48,419
Latest member
EstelaCout

Latest Threads

Top