Click to See Complete Forum and Search --> : Passing values to a RadioButtonList Doesn't Work


beacon-dartmouth
May 13th, 2004, 02:28 PM
I have a radio button list (or it could be a pair of individual radio buttons) that receive a value passed by a session variable from a previous page via the statement:

radioButton.selectedItem.value = context.items("variable")

Then, after user input, the radio button value is passed to an UPDATE statement with the @radButton variable:

cmdUpdate.parameters.add( "@radButton", radioButton.selectedItem.text )

The HTML code is:

<asp:RadioButtonList id="radioButton" runat="server">
<asp:ListItem Value="Yes" Text="Yes" />
<asp:ListItem Value="No" Text="No" Selected="false" />
</asp:RadioButtonList>

When I make Selected="false" in the asp:ListItem Value="No" ... it throws the following error: Object reference not set to an instance of an object.

Any ideas?

Sonu Kapoor
May 14th, 2004, 07:39 AM
Are you sure that you need the "@" sign in the function ? Maybe you can show me where you create cmdUpdate.

Sonu

beacon-dartmouth
May 14th, 2004, 08:40 AM
Well, I couldn't tell you for sure that I do need the @ with the variable, but so far it's been working with all the other ones. Here's a more complete version of this code:

Sub bClickS( s As Object, e As EventArgs )
Dim conMeta As SqlConnection
Dim strUpdate As String
Dim cmdUpdate As SqlCommand
Dim intUpdateCount As Integer
Dim radFieldDetail As String
If radYes.checked
radFieldDetail = "Yes"
else
radFieldDetail = "No"
end if

conMeta = New SqlConnection( "Server='DOHSDBS96';trusted_connection=true;Database='METADATAv2'" )
strUpdate = "Update Application Set Name=@name, Acronym=@acronym, Program_Area_Name=@progAreaName, Usage_Start_Date=@useStartDate, Description=@desc, Field_Detail=@fieldDetail Where Application_ID=@appID"
cmdUpdate = New SqlCommand( strUpdate, conMeta )
cmdUpdate.Parameters.Add( "@appID", lblID.Text )
cmdUpdate.Parameters.Add( "@name", txtName.Text )
cmdUpdate.Parameters.Add( "@acronym", txtAcronym.Text )
cmdUpdate.Parameters.Add( "@progAreaName", txtProgAreaName.Text )
cmdUpdate.Parameters.Add( "@useStartDate", txtUseStartDate.Text )
cmdUpdate.Parameters.Add( "@desc", txtDesc.Text )
cmdUpdate.Parameters.Add( "@fieldDetail", radFieldDetail )

conMeta.Open()
intUpdateCount = cmdUpdate.ExecuteNonQuery()
conMeta.Close()
lblResults.Text = intUpdateCount & " record(s) updated!"
End Sub


Sub Page_Load(s As Object, e As EventArgs)

If Not Page.IsPostBack Then
'label1.text = "The information you are about to modify will be applied to: " & Context.Items("Name") & " - ID: " & Context.Items("Application_ID")
lblID.Text = Context.Items("Application_ID")
txtName.Text = Context.Items("Name")
txtAcronym.text = Context.Items("Acronym")
txtProgAreaName.text = Context.Items("Program_Area_Name")
txtUseStartDate.text = Context.Items("Usage_Start_Date")
txtDesc.text = Context.Items("Description")
'response.write("<BR>Field_Detail = "& Context.Items("Field_Detail"))
'response.end

If Context.Items("Field_Detail") = "Yes"
radYes.Checked = true
Else
radNo.Checked = true
End If

txtDivID.text = Context.Items("DivisionName")
txtBurID.text = Context.Items("BureauName")

End If


End Sub


Since the time I posted my question on this forum I've been experimenting, and I managed to rid the error I originally got, by using some conditional statements to handle the radio button assignment. But the original problem of not taking the value passed from a previous page remains the same.

Sonu Kapoor
May 14th, 2004, 08:47 AM
If I understood you correct then the only problem now is that the radiobuttonlist value is not passed. Is that correct ?

Why are using the statement: I am not sure whether this will work but I would use it the other way. I would set the "Yes" listitem to true and would get rid of the false in the "No" ListItem.


<asp:ListItem Value="No" Text="No" Selected="false" />


Instead set the other value to true.


<asp:ListItem Value="Yes" Text="Yes" Selected="true"/>



Sonu

beacon-dartmouth
May 17th, 2004, 12:39 PM
My objective here was to successfully assign a session variable value to a radioButtonList.

Selected="false" prevents the item from being selected and it throws an exception. I went ahead and changed it to "true" as you suggested. It didn't complain BUT it didn't display the original value assigned to the radio button EITHER, which is my goal here.


radFieldDetail.selectedItem.value = context.items("Field_Detail")


So, I took that property out and now the compiler returns:

"Object reference not set to an instance of an object."

So, the question is what's the right syntax that would allow the radiobutton to receive a value from the above assignment as it is setup.

This should be a simple thing to do, and it shouldn't require voodoo manipulation, which is what I had to do in this case by using a couple of conditional statements and regular radio buttons instead of a radiobuttonList:

'at the button click sub:
Dim radFieldDetail As String
If radYes.checked
radFieldDetail = "Yes"
Else
radFieldDetail = "No"
End If

'at the page_load sub:
If Context.Items("Field_Detail") = "Yes"
radYes.Checked = true
Else
radNo.Checked = true
End If

It's frustrating having to rely on conditionals for such a trivial task but I couldn't find a more suitable alternative, yet.

Thanks for trying to help me here, though.