System.NullReferenceException: Object reference not set to an instance of an object.

J

John Smith

Why do I get above error message while executing this code:

Public Sub DropDown_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs)
Dim list As DropDownList = CType(sender, DropDownList)
Dim cell = CType(list.Parent, TableCell)
Dim item = CType(cell.Parent, DataGridItem)
Dim index = item.ItemIndex

Dim ds As DataSet
ds = DataGrid1.DataSource

Dim row As DataRow
row = ds.Tables(0).Select("ID = '" + item.Cells(0).Text + "'")(0)
'<--- ERROR
End Sub
 
J

John Smith

Thank you.

"ds" was null. It seems I need to add DataSource into Session object
somehow. Beginner mistake. It seems DataGrid1.DataSource is not persistance?
 
J

John Smith

"ds" was null. It seems I need to add DataSource into Session object
somehow. Beginner mistake. It seems DataGrid1.DataSource is not
persistance?

I've declared ds as global Shared variable and it works fine so far.
 
E

Eliyahu Goldin

Set a break point on the line

row = ds.Tables(0).Select("ID = '" + item.Cells(0).Text + "'")(0)

and see what is null there.

Could be:
no tables in ds;
no Cells(o) in item;
no item;
select doesn't return anything
....

Debug it.

Eliyahu
 
J

John Smith

Just note, that global Shared variables are shared between user sessions.
It
means that if there will be multiple users using the app in the same time,
they will affect each other.

So, should I declare it as "static" or as simple global variable (without
"shared")?
 
E

Eliyahu Goldin

Just note, that global Shared variables are shared between user sessions. It
means that if there will be multiple users using the app in the same time,
they will affect each other.

Eliyahu
 
B

Bob Barrows [MVP]

John said:
So, should I declare it as "static" or as simple global variable
(without "shared")?

You should probably store it in Session rather than declaring it "Global
Shared"
 
B

Bob Barrows [MVP]

John said:
There is no other way but session object?
If you need it to be session-specific, the session object makes it easy.
Sure, you can create a global hashtable keyed by some property of the
users of your application, but
1. this hashtable could get rather large, and
2. if you are not careful, bugs are hard to avoid.

Just out of curiosity, what is your objection to the session object?
 
E

Eliyahu Goldin

If you can't re-populate the datasource on every postback, you can store it
in a session variable.

Eliyahu
 
B

Bob Barrows [MVP]

John said:
I am beginner and I have never used it before. I'll try it now.
Ah!
I typically use a pattern like this in my pages:

Private _CachedDS as dataset
Private Propery CachedDS() as dataset
Get
If _CachedDS Is Nothing Then
If Not Session("CachedDS") Is Nothing Then
_CachedDS = CType(Session("CachedDS"), dataset)
End If
End If
Return _CachedDS
End Get
Set(ByVal Value As dataset)
Session("CachedDS") = Value
_CachedDS = Value
End Set
End Property

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
If Not IsPostBack Then
BindDataGrid()
End If

End Sub

....
Sub BindDataGrid
Dim ds as dataset
if CachedDS Is Nothing then
FillDataSet()
End If
'bind the grid
Dim ds as dataset=CachedDS
With DataGrid1
.DataSource=ds
.DataMember = ...
.DataBind
End With

End Sub

Sub FillDataSet()
Try
CachedDS = DataLayer.RetrieveData(parms)
Catch ex
'handle error
End Try
End Sub

Public Sub DropDown_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs)
Dim list As DropDownList = CType(sender, DropDownList)
Dim cell = CType(list.Parent, TableCell)
Dim item = CType(cell.Parent, DataGridItem)
Dim index = item.ItemIndex

Dim ds As DataSet
if not CachedDS is Nothing then
ds = CachedDS
Else
'throw an error? call FillDataset? up to you.
End If

Dim row As DataRow
row = ds.Tables(0).Select("ID = '" + item.Cells(0).Text +
"'")(0)
etc.

HTH,
Bob Barrows
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top