hi!!!
actually i'm doing a project for e-survey. the problem is that when i create different types of control, an error message is displayed. but if i create only radio buttons, everything fine. can u plz see my problem. thanks
ant
----------------------------------------
Imports System
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports System.Drawing
Imports System.Collections
Imports System.ComponentModel
Imports System.Web.SessionState
Imports System.IO
Public Class frmViewQuestionsAll
Inherits System.Web.UI.Page
Protected WithEvents tblViewAllQues As System.Web.UI.WebControls.Table
Protected WithEvents Label1 As System.Web.UI.WebControls.Label
Protected NumberOfQuestions = 0
Protected WrongArray As String() = New String(100) {}
Protected AnswerArray As String() = New String(100) {}
Protected SubmitButton As System.Web.UI.WebControls.Button
Protected WithEvents Label2 As System.Web.UI.WebControls.Label
Protected WithEvents Label3 As System.Web.UI.WebControls.Label
Protected WithEvents Label4 As System.Web.UI.WebControls.Label
Protected WithEvents Label5 As System.Web.UI.WebControls.Label
Protected WithEvents lblViewSurvey As System.Web.UI.WebControls.Label
Protected WithEvents lblViewSurveyID As System.Web.UI.WebControls.Label
Protected WithEvents lblSessionName As System.Web.UI.WebControls.Label
Protected WithEvents Label7 As System.Web.UI.WebControls.Label
Protected WithEvents Label8 As System.Web.UI.WebControls.Label
Protected WithEvents txtRespondantName As System.Web.UI.WebControls.TextBox
Protected WithEvents txtRespondantNo As System.Web.UI.WebControls.TextBox
Protected WithEvents lblRespondantID As System.Web.UI.WebControls.Label
Protected WithEvents cboSelectRespondant As System.Web.UI.WebControls.DropDownList
Protected strSurveyTitle As String = ""
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Dim MyConnection As SqlConnection
Dim strTitleSurvey As String
Function connect()
MyConnection = New SqlConnection("data source=localhost;initial catalog=ESurvey;user Id=survey;password=mysurvey;")
End Function
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not IsPostBack Then
LoadUserName()
DisplaySurvey()
ReadSurveyTitle()
ReadQuestionsIntoTable()
AddSubmitButton()
Else
'get all corresponding ids and save in response table
getDetailID()
End If
End Sub
Sub LoadUserName()
connect()
MyConnection.Open()
Dim strSQLQuery8 As String
Dim MyCommand8 As New SqlCommand()
Dim dr8 As SqlDataReader
'populate the datacombolist
strSQLQuery8 = "Select * From RESPONDANT order by RespondantID asc"
MyCommand8 = New SqlCommand(strSQLQuery8, MyConnection)
' Get a new datareader from our command
dr8 = MyCommand8.ExecuteReader()
cboSelectRespondant.DataSource = dr8
cboSelectRespondant.DataValueField = "RespondantID"
cboSelectRespondant.DataTextField = "RespondantName"
cboSelectRespondant.DataBind()
'Add a new listitem to the beginning of the listitem collection
cboSelectRespondant.Items.Insert(0, New ListItem("--Choose an item--"))
dr8.Close()
MyConnection.Close()
End Sub
Sub ReadSurveyTitle()
Dim strSQLQuery2 As String
Dim MyCommand2 As New SqlDataAdapter()
connect()
MyConnection.Open()
strSQLQuery2 = "Select * from SURVEY order by SurveyID desc"
MyCommand2 = New SqlDataAdapter(strSQLQuery2, MyConnection)
Dim ds As DataSet = New DataSet("SURVEY")
MyCommand2.MissingSchemaAction = MissingSchemaAction.AddWithKey
MyCommand2.Fill(ds, "SURVEY")
Dim SurveyTable As DataTable = ds.Tables("SURVEY")
strSurveyTitle = SurveyTable.Rows(0)("SurveyTitle").ToString
Dim tr As TableRow = New TableRow()
tblViewAllQues.Rows.Add(tr)
Dim aCell As TableCell = New TableCell()
aCell.Text = "<H1>" + strSurveyTitle + "</H1>"
tr.Cells.Add(aCell)
MyConnection.Close()
End Sub
Sub DisplaySurvey()
'display survey title and id in respective label
Dim strSQLQuery2 As String
Dim MyCommand2 As New SqlDataAdapter()
Dim ds2 As New DataSet()
strTitleSurvey = lblViewSurvey.Text
strTitleSurvey = Request.Params("STitle")
connect()
strSQLQuery2 = "Select * from SURVEY order by SurveyID desc"
MyCommand2 = New SqlDataAdapter(strSQLQuery2, MyConnection)
MyCommand2.Fill(ds2, "SURVEY")
MyConnection.Open()
Dim SurveyTitleName As DataRow
For Each SurveyTitleName In ds2.Tables(0).Rows
If SurveyTitleName("SurveyTitle") = strTitleSurvey Then
lblViewSurvey.Text = SurveyTitleName("SurveyTitle")
lblViewSurveyID.Text = SurveyTitleName("surveyID")
Exit Sub
End If
Next SurveyTitleName
MyConnection.Close()
End Sub
Private Sub ReadQuestionsIntoTable()
Dim strSQLQuery3 As String
Dim MyCommand3 As New SqlDataAdapter()
Dim strSQLQuery4 As String
Dim MyCommand4 As New SqlDataAdapter()
'Fill the questions and choices tables in memory
connect()
MyConnection.Open()
Dim strSurvey = CInt(lblViewSurveyID.Text)
strSQLQuery3 = _
"Select * from QUESTION " _
& "where SurveyID='" & strSurvey & "'"
' strSQLQuery3 = "select * from QUESTION order by QuestionID desc"
MyCommand3 = New SqlDataAdapter(strSQLQuery3, MyConnection)
strSQLQuery4 = "Select * from ANSWER a INNER JOIN QUESTION q ON " _
& "a.QuestionID = q.QuestionID " _
& "WHERE q.SurveyID= '" & strSurvey & "'"
' strSQLQuery4 = "select * from ANSWER order by AnswerID desc"
MyCommand4 = New SqlDataAdapter(strSQLQuery4, MyConnection)
Dim ds3 As DataSet = New DataSet("questionsds")
MyCommand3.Fill(ds3, "QUESTION")
MyCommand4.Fill(ds3, "ANSWER")
Dim QuestionsTable As DataTable = ds3.Tables("QUESTION")
Dim ChoicesTable As DataTable = ds3.Tables("ANSWER")
' create a data relation between the Questions and Answer Tables
' so we can cycle through the answers for each question
Dim QALink As DataRelation = New DataRelation("QuestionLink", QuestionsTable.Columns("QuestionID"), ChoicesTable.Columns("QuestionID"))
QuestionsTable.ChildRelations.Add(QALink)
NumberOfQuestions = 0
' go through every row in the questions table
' and place each question in the Table Web Control
Dim dr As DataRow
For Each dr In QuestionsTable.Rows
' create a row for the question and read it from the database
Dim tr As TableRow = New TableRow()
tblViewAllQues.Rows.Add(tr)
Dim aCell As TableCell = New TableCell()
' get the text for the question and stick it in the cell
aCell.Text = dr("QuestionDescription").ToString
tr.Cells.Add(aCell)
'''' AnswerArray(NumberOfQuestions) = dr("Answer").ToString
' create a row for the answers and read from the database
Dim count As Integer = 0
'go through the child rows of the question table
' established by the DataRelation QALink and
'fill the answers for the table
Dim choiceRow As DataRow
For Each choiceRow In dr.GetChildRows(QALink)
Dim tr2 As TableRow = New TableRow()
tblViewAllQues.Rows.Add(tr2)
'create a cell for the answers
Dim aCell3 As TableCell = New TableCell()
aCell3.Width = New Unit("1000px")
'align the answers on the left
aCell3.HorizontalAlign = HorizontalAlign.Left
tr2.Cells.Add(aCell3)
'find items
Select Case choiceRow("ItemID")
''' If choiceRow("ItemID") = 1 Then
' if item id is 1 that is radio button
Case 1
'create a radio button in the cell
Dim rb As RadioButton = New RadioButton()
'assign the radio button to Group + QuestionID
rb.GroupName = "Group" + choiceRow("QuestionID").ToString
'Assign the choice to the radio button
rb.Text = choiceRow("AnswerDescription").ToString
' Assign the radio button id corresponding to the choice and question #
rb.ID = CInt(choiceRow("AnswerID")) '"Radio" + NumberOfQuestions.ToString + Convert.ToChar(count + 65)
rb.Visible = True
'add the radio button to the cell
aCell3.Controls.Add(rb)
' if item id is 2 that is check box
Case 2
'create a check boxes in the cell
Dim ckb As CheckBox = New CheckBox()
'Assign the choice to the check boxes
ckb.Text = choiceRow("AnswerDescription").ToString
' Assign the check boxes id corresponding to the choice and question #
ckb.ID = choiceRow("AnswerID") '"Chk" + NumberOfQuestions.ToString + Convert.ToChar(count + 65)
ckb.Visible = True
'add the check boxes to the cell
aCell3.Controls.Add(ckb)
' if item id is 3 that is text box
Case 3
'create a textbox in the cell
Dim txt As TextBox = New TextBox()
'Assign the choice to the textbox
txt.Text = choiceRow("AnswerDescription").ToString
txt.Width = New Unit("400px")
txt.height = New Unit("25px")
' Assign the textbox button id corresponding to the choice and question #
txt.ID = choiceRow("AnswerID") '"txt" + NumberOfQuestions.ToString + Convert.ToChar(count + 65)
txt.Visible = True
'add the textbox to the cell
aCell3.Controls.Add(txt)
' if item id is 4 that is text area
Case 4
'create a textarea in the cell
Dim txt As TextBox = New TextBox()
'Assign the choice to the textbox
txt.Text = choiceRow("AnswerDescription").ToString
'make text box multiline
txt.TextMode = TextBoxMode.MultiLine
txt.Width = New Unit("345")
txt.height = New Unit("125")
' Assign the textarea button id corresponding to the choice and question #
txt.ID = "txt" + NumberOfQuestions.ToString + Convert.ToChar(count + 65)
txt.Visible = True
'add the textarea to the cell
aCell3.Controls.Add(txt)
' if item id is 5 that is message i.e label
Case 5
' if item id is 6 that is combobox
Case 6
'create a combobox in the cell
Dim cbo As DropDownList = New DropDownList()
''''assign the combobox to Group + QuestionID
'''cbo.group = "Group" + choiceRow("QuestionID").ToString
'Assign the choice to the combobox
cbo.DataTextField = choiceRow("AnswerDescription").ToString
cbo.Width = New Unit("100px")
' Assign the combobox id corresponding to the choice and question #
cbo.ID = "Cbo" + NumberOfQuestions.ToString + Convert.ToChar(count + 65)
cbo.Visible = True
'add the combobox to the cell
aCell3.Controls.Add(cbo)
End Select
System.Math.Min(System.Threading.Interlocked.Increment(count), count - 1)
Next
'add a table row between each question
'as a spacer
Dim spacer As TableRow = New TableRow()
spacer.Height = New Unit("30px")
Dim spacerCell As TableCell = New TableCell()
spacerCell.Height = New Unit(" 30px")
spacer.Cells.Add(spacerCell)
tblViewAllQues.Rows.Add(spacer)
System.Math.Min(System.Threading.Interlocked.Increment(NumberOfQuestions), NumberOfQuestions - 1)
Next
MyConnection.Close()
End Sub
Private Sub AddSubmitButton()
Dim SubmitButton As New Button()
SubmitButton.ID = "btnSubmit"
AddHandler SubmitButton.Click, AddressOf Button1_Click
Me.Controls.Add(SubmitButton)
SubmitButton.Width = New Unit("100px")
SubmitButton.Height = New Unit("25px")
SubmitButton.Visible = True
SubmitButton.Text = "Score"
SubmitButton.Style.Add("runat", "server")
Dim ButtonRow As TableRow = New TableRow()
Dim ButtonCell As TableCell = New TableCell()
ButtonRow.Cells.Add(ButtonCell)
ButtonCell.Controls.Add(SubmitButton)
tblViewAllQues.Rows.Add(ButtonRow)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
' now go through table one and get the score
'CalculateScore();
' Dim sButton As String = CType(sender, Button1_Click).ID
End Sub
Private Function getDetailID()
'initialize wrong answer array
WrongArray.Initialize()
saveRespondantDetails()
selectRespondant()
' Load up survey and question table to get Number of Questions
Dim strSQLQuery As String
Dim MyCommand As New SqlCommand()
connect()
strSQLQuery = "Select count(QuestionID) from QUESTION where SurveyID='" & lblViewSurveyID.Text & "'"
MyCommand = New SqlCommand(strSQLQuery, MyConnection)
MyConnection.Open()
NumberOfQuestions = MyCommand.ExecuteScalar()
Label3.Text = CStr(NumberOfQuestions)
MyConnection.Close()
saveResponse()
End Function
Sub saveRespondantDetails()
Dim strSQLQuery9 As String
Dim MyCommand9 As New SqlCommand()
connect()
strSQLQuery9 = "INSERT INTO RESPONDANT(RespondantName,RespondantTel,Log_As) values(@ResName,@ResTel,@SessionName)"
MyCommand9 = New SqlCommand(strSQLQuery9, MyConnection)
MyCommand9.Parameters.Add(New SqlParameter("@ResName", SqlDbType.Text, 20))
MyCommand9.Parameters.Add(New SqlParameter("@ResTel", SqlDbType.Text, 15))
MyCommand9.Parameters.Add(New SqlParameter("@SessionName", SqlDbType.Text, 20))
MyCommand9.Parameters("@ResName").Value = CStr(txtRespondantName.Text)
MyCommand9.Parameters("@ResTel").Value = CStr(txtRespondantNo.Text)
MyCommand9.Parameters("@SessionName").Value = CStr(lblSessionName.Text)
MyConnection.Open()
MyCommand9.ExecuteNonQuery()
MyConnection.Close()
End Sub
Sub selectRespondant()
Dim strSQLQuery10 As String
Dim MyCommand10 As New SqlDataAdapter()
Dim ds10 As New DataSet()
strTitleSurvey = lblViewSurvey.Text
strTitleSurvey = Request.Params("STitle")
connect()
strSQLQuery10 = "Select * from RESPONDANT order by RespondantID desc"
MyCommand10 = New SqlDataAdapter(strSQLQuery10, MyConnection)
MyCommand10.Fill(ds10, "RESPONDANT")
MyConnection.Open()
Dim resName As DataRow
For Each resName In ds10.Tables(0).Rows
If resName("RespondantName") = txtRespondantName.Text Then
lblRespondantID.Text = resName("RespondantID")
Exit Sub
End If
Next resName
MyConnection.Close()
End Sub
Sub saveResponse()
' cycle through all the keys in the returned Request
Dim i As Integer
Dim r As HttpRequest
r = Me.Request
Label5.Text = r.Form.Keys.Count
For i = 0 To r.Form.Keys.Count - 1
''''''''''''''''''''
Label2.Text = "sssss"
'''''''''''''''''''''''''
Dim nextKey As String = r.Form.Keys(i)
If (nextKey.Substring(0, 5) = "Group") Then '''''here if i remove this Label4 will display "SCORE" i don't know why.
'get the radiobutton ID
Dim radioAnswer As Integer = r.Form.Get(nextKey)
Label4.Text = radioAnswer
Dim strSQLQuery3 As String
Dim MyCommand3 As New SqlCommand()
connect()
strSQLQuery3 = "INSERT INTO SURVEY_RESPONSE(ResponseAnswerID,RespondantID) values(@RAnsId,@ResID)"
MyCommand3 = New SqlCommand(strSQLQuery3, MyConnection)
MyCommand3.Parameters.Add(New SqlParameter("@RAnsId", SqlDbType.BigInt, 8))
MyCommand3.Parameters.Add(New SqlParameter("@ResID", SqlDbType.BigInt, 8))
MyCommand3.Parameters("@RAnsId").Value = CInt(radioAnswer)
MyCommand3.Parameters("@ResID").Value = CInt(lblRespondantID.Text)
MyConnection.Open()
MyCommand3.ExecuteNonQuery()
MyConnection.Close()
End If
Next i
Response.Redirect("frmmain.aspx")
End Sub
Private Function CalcQuestionsAnsweredCount(ByVal r As HttpRequest)
''''''''''''Dim count = 0
''''''''''''Dim i
''''''''''''For i = 0 To i < r.Form.Keys.Count
'''''''''''' ''''''''''''''''''''
'''''''''''' Label2.Text = "ngerwt"
'''''''''''' '''''''''''''''''''''''''
'''''''''''' Dim nextKey As String = r.Form.Keys(i)
'''''''''''' If (nextKey.Substring(0, 5) = "Group") Then
'''''''''''' count = count + 1
'''''''''''' Else
'''''''''''' End If
''''''''''''Next
''''''''''''Return count
End Function
End Class