Link to home
Start Free TrialLog in
Avatar of numten
numten

asked on

Datagrid Template Column - Radio Button Bind

Hi.

I have a datagrid that has 4 radio buttons per row.  Each radio button is a seperate question (rb1=question1, rb2=quest2) etc... I am successfully able to submit the answers of each row into the database.  Now the problem is, I need to populate the datagrid and then using the datagrid results I also need to populate the answer of the radio buttons.  The results that populate the datagrid and radio buttons are two seperate stored procedures:

SP1 for datagrid is retrieve all applications:
So it returns:
Appln_id and Appln Name
1   App 1
2  App 2

SP2 uses the given rows Appln_ID and then populates the answers (using that appln_id)   into the RB controls:
1  App 1    RB1=Checked            RB2=Not Checked....
2  App 2   RB1= Not Checked      RB2= Not Checked .....

Right now, I first populate the datagrid with the appln_id and appln names.  Once the datagrid is filled with that information I go back and do a loop through each row, passing in the rows appln_id to help populate the rb answers (each rb is a different question id so I have to loop through each rb too).  I cannot seem to get the rb's to work.  I keep getting the error "object reference not set to an instance of an object".  I ran the sp and I am certain that the values I"m passing in return an answer (to eliminate all problems).

Here is the code.  Please help me debug this!  If you think I should be following another method, please let me know!  Thanks!


    Private Sub dgviewiereports_itemdatabound(ByVal sender As System.Object, ByVal e As DataGridItemEventArgs) Handles dgviewiereports.ItemDataBound

****STORED PROCEDURE CALL TO GET DATAGRID POPULATED **********************
        Dim cmdG_GET_IMPCG_EVNT As OracleCommand = DBHelper.CreateSPCommand("***.GET_DATAGRID_FILLED")
        Dim pimpcg_evnt_nm_in As New OracleParameter("impcg_evnt_nm_in", OracleDbType.Varchar2, CAP.decode(ddlviewIEReports), ParameterDirection.Input)
        Dim pCUR_impcg_evnt_info As New OracleParameter("CUR_impcg_evnt_info", OracleDbType.RefCursor, DBNull.Value, ParameterDirection.Output)
        cmdG_GET_IMPCG_EVNT.Parameters.Add(pimpcg_evnt_nm_in)
        cmdG_GET_IMPCG_EVNT.Parameters.Add(pCUR_impcg_evnt_info)

        Dim dsFillDatagrid As New DataSet
        DBHelper.FillDataSet(dsFillDatagrid, cmdG_GET_IMPCG_EVNT)
        dgviewiereports.DataSource = dsFillDatagrid
        dgviewiereports.databind()

'**************START OF LOOP TO FIND OUT THE NUMBER OF ROWS IN THE DATAGRID ******************
        Dim i As Integer
        For i = 0 To dsFillDatagrid.Tables(0).Rows.Count  'count number rows in DG
            If dsFillDatagrid.Tables(0).Rows.Count > 0 Then 'if there are results... continue
               
'************* START AT ROW 1, NOW LOOP THROUGH EACH QUESTION TO GET THE ANSWERS TO EACH RADIO BUTTON SINCE EACH ARE A DIFFERENT QUESTION ID IN THE DB. ***********
                Dim i2 As Integer
                For i2 = 103 To 106   'questionid for radio buttons. RB1=question id 103 in DB.

'**************STORED PROCEDURE CALL TO GET EACH RADIO BUTTON ANSWER ************
                    Dim cmdg_impcg_evnt_appln_quest As OracleCommand = DBHelper.CreateSPCommand("XXX.get_RADIO_BUTTON_POPULATED")
                    Dim pimpcg_evnt_nm As New OracleParameter("impcg_evnt_nm_in", OracleDbType.Varchar2, ddlviewIEReports.SelectedValue, ParameterDirection.Input)
                    Dim pappln_id As New OracleParameter("appln_id_in", OracleDbType.Int16, 1005, ParameterDirection.Input)
                    Dim pappln_portfl_quest_id As New OracleParameter("appln_portfl_quest_id_in", OracleDbType.Int16, i2, ParameterDirection.Input)
                    Dim presults As New OracleParameter("CUR_impcg_evnt_info", OracleDbType.RefCursor, DBNull.Value, ParameterDirection.Output)

                    cmdg_impcg_evnt_appln_quest.Parameters.Add(pimpcg_evnt_nm)
                    cmdg_impcg_evnt_appln_quest.Parameters.Add(pappln_id)
                    cmdg_impcg_evnt_appln_quest.Parameters.Add(pappln_portfl_quest_id)
                    cmdg_impcg_evnt_appln_quest.Parameters.Add(presults)
                    DBHelper.RunSP(cmdg_impcg_evnt_appln_quest)

                    Dim dsRBbind As New DataSet
                    DBHelper.FillDataSet(dsRBbind, cmdg_impcg_evnt_appln_quest)
                    Dim dr As DataRow
                    Dim i3 As Integer

'****iF THE GIVEN ROW, GIVEN RADIO BUTTON HAS AN ANSWER THAT BIND IT TO THE RADIO BUTTON -----  THIS IS WHERE THE PROBLEM OCCURS!!!!!!!!!! ***************************

                    If dsRBbind.Tables(0).Rows.Count > 0 Then
                        For i3 = 0 To dsRBbind.Tables(0).Rows.Count - 1
                            dr = dsRBbind.Tables(0).Rows(i3)
                            Dim ansval As String = dr.Item("ans_val").ToString

                            If i2 = 103 Then
                                Dim rbactionneeded As RadioButton = CType(e.Item.FindControl("rbactionneeded"), RadioButton)
                                If ansval = "Y" Then
                                    rbactionneeded.Checked = True  '******this is where it shoots the error, object reference not set to instance of object **********
                                ElseIf dr("ans_val") = "N" Then
                                    rbactionneeded.Checked = False
                                Else
                                    rbactionneeded.Checked = False
                                End If
                            ElseIf i2 = 104 Then

                            ElseIf i2 = 105 Then

                            ElseIf i2 = 106 Then

                            End If
                        Next
                    End If

                    DBHelper.DisposeSPCommand(cmdg_impcg_evnt_appln_quest)
                Next
            End If
        Next
    End Sub
'*****************************************

Do you see anything wrong with the section I said had a problem?
Avatar of shekhar_k
shekhar_k

Reason for the Error -
When ItemDataBound event is raised all the rows in the Datagrid including the header are populated. So for every row that the datasource of the grid has ItemDatabound is raised, say your datasource is a Datatable with 4 rows then ItemDataBound is called 5 times, once for the header and 4 times for each of the rows. The first time Datagrid is bound ItemDatabound event is raised for the header whose ItemIndex is -1. When you try to FindControl when ItemDataBound is raised for the header you get NullReferenceException as there is no RadioButton in the header.

Possible Solution -
Put a check in the "dgviewiereports_itemdatabound" subroutine wherein you check for the current ItemIndex for which the ItemDataBound event is raised. If the index is -1 then do not access any of the controls. Access "rbactionneeded" if the Item index is > -1. Induce this condition before you try to FindControls on the DataGridItem.

hope it resolves your issue...
ASKER CERTIFIED SOLUTION
Avatar of mmarinov
mmarinov

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