Solved

Datagrid Template Column - Radio Button Bind

Posted on 2004-04-29
2
4,868 Views
Last Modified: 2007-12-19
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?
0
Comment
Question by:numten
2 Comments
 

Expert Comment

by:shekhar_k
ID: 10956814
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...
0
 
LVL 28

Accepted Solution

by:
mmarinov earned 500 total points
ID: 10959449
Hi numten,

I'm glad you've solved the problem, but can i ask you something:
please post only one question per a problem or ask the page editors ( in this TA i'm this man ) to move your questions to an appropirate TA

Thanks for the understanding

B..M
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Can I play with MicroStrategy.NET without a license? 5 48
Wpf develop 5 35
Need to Modify a Script I found 5 77
Birthdays 3 21
Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
Normally the drop down box control found in the .Net framework tools is able to select just one data and value at a time, which is displayed on the text area.   But what if you want to have multiple values to be selected in the drop down box? As …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now