?
Solved

Datagrid Template Column - Radio Button Bind

Posted on 2004-04-29
2
Medium Priority
?
4,903 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 2000 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

771 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