Link to home
Start Free TrialLog in
Avatar of maverick1611
maverick1611

asked on

Object reference not set to an instance of an object .. datagrid error .. !!

Hi,
I'm trying to conditionally format the cells in a datagrid on a webpage.. the datagrid gets values from an OLAP datasource .. every thing works fine and i can see different cells in different colors based on their values.. I have been able to do the conditional formating using the following code:

Private Sub OnNWDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemCreated

        If (e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem) Then

            Dim rv As DataRowView
            rv = CType(e.Item.DataItem, DataRowView)

            Dim NcmCount1 As New Object

            If IsDBNull(rv.Row.ItemArray(1)) Then
                NcmCount1 = 0
            Else
                NcmCount1 = CType(rv.Row.ItemArray(1), Integer)
            End If

            If (NcmCount1 <= 5) Then
                e.Item.Cells(2).BackColor = Color.Green
            ElseIf (NcmCount1 > 5 And NcmCount1 < 50) Then
                e.Item.Cells(2).BackColor = Color.Yellow
            ElseIf (NcmCount1 > 50) Then
                e.Item.Cells(2).BackColor = Color.Red
            End If

        End If
    End Sub

The problem comes when i click on a button on the same webpage.This button has nothing to do with this conditional formating .. it simply redirects the user to a different page .. till now (ie before adding this conditional formating code) everything was working properly .. how ever after adding this code when ever i press this button i get the following error :

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 179:            Dim NcmCount4 As New Object
Line 180:
Line 181:            If IsDBNull(rv.Row.ItemArray(1)) Then
Line 182:                NcmCount1 = 0
Line 183:            Else
 
i have no idea why this is happening .. please help.
thanks mav ..
Avatar of praneetha
praneetha

Dim NcmCount4 As New Object // here u have declared NcmCount4
Line 180:
Line 181:            If IsDBNull(rv.Row.ItemArray(1)) Then
Line 182:                NcmCount1 = 0 // and u r using NcmCount1
Line 183:            Else

is it just type?

and did u bind your datagrid in

if Not Page.IsPostBacl
bind here
Avatar of YZlat
Declare NcmCount1 as an integer

Private Sub OnNWDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemCreated

        If (e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem) Then

            Dim rv As DataRowView
            rv = CType(e.Item.DataItem, DataRowView)

            Dim NcmCount1 As Integer
            If IsDBNull(rv.Row.ItemArray(1)) Then
                NcmCount1 = 0
            Else
                NcmCount1 = CType(rv.Row.ItemArray(1), Integer)
            End If

            If (NcmCount1 <= 5) Then
                e.Item.Cells(2).BackColor = Color.Green
            ElseIf (NcmCount1 > 5 And NcmCount1 < 50) Then
                e.Item.Cells(2).BackColor = Color.Yellow
            ElseIf (NcmCount1 > 50) Then
                e.Item.Cells(2).BackColor = Color.Red
            End If

        End If
    End Sub

ASKER CERTIFIED SOLUTION
Avatar of Snarf0001
Snarf0001
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of maverick1611

ASKER

bingo snarf0001,
ur a genius .. that solution worked like a charm.
thanks a lot ..
muaaah
mav.
Thanks, glad I could help.