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_itemdatabo und(ByVal sender As System.Object, ByVal e As DataGridItemEventArgs) Handles dgviewiereports.ItemDataBo und
****STORED PROCEDURE CALL TO GET DATAGRID POPULATED **********************
Dim cmdG_GET_IMPCG_EVNT As OracleCommand = DBHelper.CreateSPCommand(" ***.GET_DA TAGRID_FIL LED")
Dim pimpcg_evnt_nm_in As New OracleParameter("impcg_evn t_nm_in", OracleDbType.Varchar2, CAP.decode(ddlviewIEReport s), ParameterDirection.Input)
Dim pCUR_impcg_evnt_info As New OracleParameter("CUR_impcg _evnt_info ", OracleDbType.RefCursor, DBNull.Value, ParameterDirection.Output)
cmdG_GET_IMPCG_EVNT.Parame ters.Add(p impcg_evnt _nm_in)
cmdG_GET_IMPCG_EVNT.Parame ters.Add(p CUR_impcg_ evnt_info)
Dim dsFillDatagrid As New DataSet
DBHelper.FillDataSet(dsFil lDatagrid, 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).R ows.Count 'count number rows in DG
If dsFillDatagrid.Tables(0).R ows.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_ques t As OracleCommand = DBHelper.CreateSPCommand(" XXX.get_RA DIO_BUTTON _POPULATED ")
Dim pimpcg_evnt_nm As New OracleParameter("impcg_evn t_nm_in", OracleDbType.Varchar2, ddlviewIEReports.SelectedV alue, 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_por tfl_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_ques t.Paramete rs.Add(pim pcg_evnt_n m)
cmdg_impcg_evnt_appln_ques t.Paramete rs.Add(pap pln_id)
cmdg_impcg_evnt_appln_ques t.Paramete rs.Add(pap pln_portfl _quest_id)
cmdg_impcg_evnt_appln_ques t.Paramete rs.Add(pre sults)
DBHelper.RunSP(cmdg_impcg_ evnt_appln _quest)
Dim dsRBbind As New DataSet
DBHelper.FillDataSet(dsRBb ind, cmdg_impcg_evnt_appln_ques t)
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.Co unt > 0 Then
For i3 = 0 To dsRBbind.Tables(0).Rows.Co unt - 1
dr = dsRBbind.Tables(0).Rows(i3 )
Dim ansval As String = dr.Item("ans_val").ToStrin g
If i2 = 103 Then
Dim rbactionneeded As RadioButton = CType(e.Item.FindControl(" rbactionne eded"), 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_appl n_quest)
Next
End If
Next
End Sub
'************************* ********** ******
Do you see anything wrong with the section I said had a problem?
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_itemdatabo
****STORED PROCEDURE CALL TO GET DATAGRID POPULATED **********************
Dim cmdG_GET_IMPCG_EVNT As OracleCommand = DBHelper.CreateSPCommand("
Dim pimpcg_evnt_nm_in As New OracleParameter("impcg_evn
Dim pCUR_impcg_evnt_info As New OracleParameter("CUR_impcg
cmdG_GET_IMPCG_EVNT.Parame
cmdG_GET_IMPCG_EVNT.Parame
Dim dsFillDatagrid As New DataSet
DBHelper.FillDataSet(dsFil
dgviewiereports.DataSource
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).R
If dsFillDatagrid.Tables(0).R
'************* 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_ques
Dim pimpcg_evnt_nm As New OracleParameter("impcg_evn
Dim pappln_id As New OracleParameter("appln_id_
Dim pappln_portfl_quest_id As New OracleParameter("appln_por
Dim presults As New OracleParameter("CUR_impcg
cmdg_impcg_evnt_appln_ques
cmdg_impcg_evnt_appln_ques
cmdg_impcg_evnt_appln_ques
cmdg_impcg_evnt_appln_ques
DBHelper.RunSP(cmdg_impcg_
Dim dsRBbind As New DataSet
DBHelper.FillDataSet(dsRBb
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.Co
For i3 = 0 To dsRBbind.Tables(0).Rows.Co
dr = dsRBbind.Tables(0).Rows(i3
Dim ansval As String = dr.Item("ans_val").ToStrin
If i2 = 103 Then
Dim rbactionneeded As RadioButton = CType(e.Item.FindControl("
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(
Next
End If
Next
End Sub
'*************************
Do you see anything wrong with the section I said had a problem?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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_itemdatab
hope it resolves your issue...