I have the following code where I am extracting data from a set of controls for an xml class. I iterate through my controls and pull my control id and selected value for radiobuttonlist or checkboxlist. I am successfully iterating through my controls and can display the result set (surveylabel1). When I add the code to add detail items to my responsedataset i get an error "Object reference not set to an instance of an object."
I am using the integer i in my iteration and to define the detail, is the variable i not available inside of my Case statement, or do I have another problem with my code?
Thank you.
Private Function ExtractResults() As SResponse.Schema7Ihsurveyresponse Dim currentUser As New AccBusiness.User( _
CType(Context.User, SitePrincipal))
Dim sb As StringBuilder = New StringBuilder
sb.Append("Answers: ")
Dim msgtext As String
Dim ctrl, ctl2 As Control
Dim rb As RadioButtonList
Dim lb As TextBox
Dim ck As CheckBoxList
Dim i As Integer
Dim id As Integer
Dim item As ListItem
Dim newResponse As New SResponse.Schema7Ihsurveyresponse
newResponse.surveyid = 1
newResponse.Userid = currentUser.UserId
For i = 0 To (Controls().Count - 1)
Dim newresponsedetail(i) As SResponse.Schema7IhsurveyresponseIhsresponsedetail
ctrl = Controls(i)
id = -1
Select Case ctrl.GetType.Name
Case "RadioButtonList"
rb = CType(ctrl, RadioButtonList)
sb.Append(rb.ID & ": " & rb.SelectedValue)
newresponsedetail(i).question = rb.ID.ToString
newresponsedetail(i).result = CInt(rb.SelectedValue)
Case "CheckBoxList"
ck = CType(ctrl, CheckBoxList)
For Each item In ck.Items
If item.Selected Then
sb.Append(ck.ID & ": ")
sb.Append(item.Value & ", ")
'newresponsedetail(i).question = ck.ID.ToString
'newresponsedetail(i).result = CInt(item.Value)
End If
Next
End Select
sb.Append(" - ")
Next
Surveylabel1.Text = sb.ToString
End Function
Hi,
i couldn't find "responsedataset" in ur code ?
I am sorry I should have been clearer in my question. I am trying to iterate through my controls adding the Control.id and control.selectedvalue to the detail of my sresponse.schema7idsurveyresponse (please forgive the long names - I plan to clean this up later).
I have alltered my code for ExtractResults() from my previous post and add the code for my sresponse class.
I commented out my execute scalar and set my return for my Addresponse (newresponse) function to be the XMLResoponse string for troubleshooting. Although I am not getting any errors, I am also not getting any XMLResponse string. Why would that be? What am I missing?
Private Function ExtractResults() As SResponse.Schema7Ihsurveyresponse Dim currentUser As New AccBusiness.User( _
CType(Context.User, SitePrincipal))
Dim sb As StringBuilder = New StringBuilder
sb.Append("Answers: ")
Dim msgtext As String
Dim ctrl, ctl2 As Control
Dim rb As RadioButtonList
Dim lb As TextBox
Dim ck As CheckBoxList
Dim i As Integer
Dim id As Integer = (Controls().Count - 1)
Dim item As ListItem
Dim newResponse As New SResponse.Schema7Ihsurveyresponse
newResponse.surveyid = 1
'newResponse.Userid = currentUser.UserId
newResponse.Userid = 1
Dim details(id) As SResponse.Schema7IhsurveyresponseIhsresponsedetail
For i = 0 To (Controls().Count - 1)
details(i) = New SResponse.Schema7IhsurveyresponseIhsresponsedetail
ctrl = Controls(i)
id = -1
Select Case ctrl.GetType.Name
Case "RadioButtonList"
rb = CType(ctrl, RadioButtonList)
sb.Append(rb.ID & ": " & rb.SelectedValue)
details(i).question = rb.ID.ToString
'details(i).result = CInt(rb.SelectedValue)
details(i).result = 1
Case "CheckBoxList"
ck = CType(ctrl, CheckBoxList)
For Each item In ck.Items
If item.Selected Then
sb.Append(ck.ID & ": ")
sb.Append(item.Value & ", ")
details(i).question = ck.ID.ToString
'details(i).result = CInt(item.Value)
details(i).result = 2
End If
Next
End Select
sb.Append(" - ")
Next
newResponse.ihsresponsedetail = details
Dim responseID As String = SResponse.addresponse(newResponse)
Surveylabel1.Text = "Response: " & responseID & sb.ToString
End Function
Private Sub btnSurveySubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSurveySubmit.Click
ExtractResults()
End Sub
End Class
my sresponse class:
<System.Xml.Serialization.XmlRootAttribute([Namespace]:="", IsNullable:=False)> _
Public Class Schema7 '<remarks/>
<System.Xml.Serialization.XmlElementAttribute("ihsurveyresponse", Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)> _
Public Items() As Schema7Ihsurveyresponse
End Class
'<remarks/>
Public Class Schema7Ihsurveyresponse
'<remarks/>
<System.Xml.Serialization.XmlElementAttribute("ihsresponsedetail", Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)> _
Public ihsresponsedetail() As Schema7IhsurveyresponseIhsresponsedetail
'<remarks/>
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public surveyid As Integer
'<remarks/>
<System.Xml.Serialization.XmlIgnoreAttribute()> _
Public surveyidSpecified As Boolean
'<remarks/>
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public Userid As Integer
'<remarks/>
<System.Xml.Serialization.XmlIgnoreAttribute()> _
Public UseridSpecified As Boolean
End Class
'<remarks/>
Public Class Schema7IhsurveyresponseIhsresponsedetail
'<remarks/>
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public id As Integer
'<remarks/>
<System.Xml.Serialization.XmlIgnoreAttribute()> _
Public idSpecified As Boolean
'<remarks/>
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public question As String
'<remarks/>
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public response As Integer
'<remarks/>
<System.Xml.Serialization.XmlIgnoreAttribute()> _
Public responseSpecified As Boolean
'<remarks/>
<System.Xml.Serialization.XmlAttributeAttribute()> _
Public result As Integer
'<remarks/>
<System.Xml.Serialization.XmlIgnoreAttribute()> _
Public resultSpecified As Boolean
End Class
Public Shared Function addresponse(ByVal newResponse As Schema7Ihsurveyresponse) As String
Dim serializer As XmlSerializer
serializer = New XmlSerializer(GetType(Schema7Ihsurveyresponse))
Dim ms As MemoryStream
'Try
ms = New MemoryStream
serializer.Serialize(ms, newResponse)
'Catch ex As Exception
' ms.Close()
'Return (-1)
' End Try
ms.Close()
Dim xmlresponse As String = System.Text.Encoding.UTF7.GetString(ms.ToArray)
Dim cmd As New SqlClient.SqlCommand
cmd.CommandText = "insertResponseDetails"
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add(New SqlClient.SqlParameter("@.xml", xmlresponse))
Dim cn As New SqlClient.SqlConnection
cn.ConnectionString = Configuration.ModuleConfig.GetSettings.ConnectionString
cmd.Connection = cn
cn.Open()
Dim newResponseid As Integer
'Try
'newResponseid = CInt(cmd.ExecuteScalar)
' Catch ex As Exception
'Return -1
'End Try
Return xmlresponse
End Function
End Class