p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Beginning ASP.NET 4 : in C# and VB (http://p2p.wrox.com/forumdisplay.php?f=560)
-   -   Multiple Choice Online Exam (http://p2p.wrox.com/showthread.php?t=85864)

jpjamie November 30th, 2011 12:48 PM

Multiple Choice Online Exam
 
I'm working through the book and so far I'm up to Chapter 13.
Thought I would attempt some original code and having difficulties inserting data into a table via an online exam....

Below is the *.aspx code from a Form with the first question only of a multiple choice online exam which has 16 multiple choice radio button (A,B, C, D) questions along with a comment. All of the 15 remaining questions are code structured the same.

At the very bottom of this page is a submit button, which first validates all questions have been answered. If any questions are not answered the user gets a prompt to answer the missed question(s). If validation passes, the user is sent to another page where all the questions with his/her selected answers are displayed along with any comments.

What is missing is all the CRUD (Create, Read, Update, Insert), especially Insert, since my primary objective is to just insert all the selected answers. I have a database table and it's available in Visual Studio 2010's Server Explorer, but so far I'm at a loss for inserting data into the database table. While it's relatively easy to click an drag GirdView and/or ListView to validate database interactivity, how is this done without them?

I need help with the code (examples?) for inserting all the radio button selections into my database table.
Any help would be greatly appreciated.

Here is the code for just the first multiple choice question...

Code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Week1Exam.aspx.cs" Inherits="WeekExams_WeekI_Week1Exam" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Online Exam</title>

  <link href="../../Style/Style2.css" rel="stylesheet" type="text/css" />


    <style type="text/css">
      .style2
      {
        width: 1021px;
   
      </style>


</head>
<body>

 <form id="form1" runat="server">
 
 
 <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
  AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="Id"
  DataSourceID="SqlDataSource1">
  <Columns>
    <asp:CommandField ShowSelectButton="True" />
    <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False"
      ReadOnly="True" SortExpression="Id" />
    <asp:BoundField DataField="ResidentName" HeaderText="ResidentName"
      SortExpression="ResidentName" />
    <asp:BoundField DataField="Q1" HeaderText="Q1" SortExpression="Q1" />
    <asp:BoundField DataField="Q2" HeaderText="Q2" SortExpression="Q2" />
    <asp:BoundField DataField="Q3" HeaderText="Q3" SortExpression="Q3" />
    <asp:BoundField DataField="Q4" HeaderText="Q4" SortExpression="Q4" />
    <asp:BoundField DataField="Q5" HeaderText="Q5" SortExpression="Q5" />
    <asp:BoundField DataField="Q6" HeaderText="Q6" SortExpression="Q6" />
    <asp:BoundField DataField="Q7" HeaderText="Q7" SortExpression="Q7" />
    <asp:BoundField DataField="Q8" HeaderText="Q8" SortExpression="Q8" />
    <asp:BoundField DataField="Q9" HeaderText="Q9" SortExpression="Q9" />
    <asp:BoundField DataField="Q10" HeaderText="Q10" SortExpression="Q10" />
    <asp:BoundField DataField="Q11" HeaderText="Q11" SortExpression="Q11" />
    <asp:BoundField DataField="Q12" HeaderText="Q12" SortExpression="Q12" />
    <asp:BoundField DataField="Q13" HeaderText="Q13" SortExpression="Q13" />
    <asp:BoundField DataField="Q14" HeaderText="Q14" SortExpression="Q14" />
    <asp:BoundField DataField="Q15" HeaderText="Q15" SortExpression="Q15" />
    <asp:BoundField DataField="Q16" HeaderText="Q16" SortExpression="Q16" />
    <asp:BoundField DataField="CreateDateTime" HeaderText="CreateDateTime"
      SortExpression="CreateDateTime" />
  </Columns>
 </asp:GridView>
 
 <asp:SqlDataSource ID="SqlDataSource1" runat="server"
  ConnectionString="<%$ ConnectionStrings:CCSExamsConnectionString1 %>"
  SelectCommand="SELECT [Id], [ResidentName], [Q1], [Q2], [Q3], [Q4], [Q5], [Q6], [Q7], [Q8], [Q9], [Q10], [Q11], [Q12], [Q13], [Q14], [Q15], [Q16], [CreateDateTime] FROM [Week1]"
  InsertCommand="INSERT INTO [Week1] ([ResidentName], [Q1], [Q2], [Q3], [Q4], [Q5], [Q6], [Q7], [Q8], [Q9], [Q10], [Q11], [Q12], [Q13], [Q14], [Q15], [Q16], [CreateDateTime]) VALUES (@ResidentName, @Q1, @Q2, @Q3, @Q4, @Q5, @Q6, @Q7, @Q8, @Q9, @Q10, @Q11, @Q12, @Q13, @Q14, @Q15, @Q16, @CreateDateTime)"
  DeleteCommand="DELETE FROM [Week1] WHERE [Id] = @Id"
  UpdateCommand="UPDATE [Week1] SET [ResidentName] = @ResidentName, [Q1] = @Q1, [Q2] = @Q2, [Q3] = @Q3, [Q4] = @Q4, [Q5] = @Q5, [Q6] = @Q6, [Q7] = @Q7, [Q8] = @Q8, [Q9] = @Q9, [Q10] = @Q10, [Q11] = @Q11, [Q12] = @Q12, [Q13] = @Q13, [Q14] = @Q14, [Q15] = @Q15, [Q16] = @Q16, [CreateDateTime] = @CreateDateTime WHERE [Id] = @Id">
  <DeleteParameters>
    <asp:Parameter Name="Id" Type="Int32" />
  </DeleteParameters>
  <InsertParameters>
    <asp:Parameter Name="ResidentName" Type="String" />
    <asp:Parameter Name="Q1" Type="String" />
    <asp:Parameter Name="Q2" Type="String" />
    <asp:Parameter Name="Q3" Type="String" />
    <asp:Parameter Name="Q4" Type="String" />
    <asp:Parameter Name="Q5" Type="String" />
    <asp:Parameter Name="Q6" Type="String" />
    <asp:Parameter Name="Q7" Type="String" />
    <asp:Parameter Name="Q8" Type="String" />
    <asp:Parameter Name="Q9" Type="String" />
    <asp:Parameter Name="Q10" Type="String" />
    <asp:Parameter Name="Q11" Type="String" />
    <asp:Parameter Name="Q12" Type="String" />
    <asp:Parameter Name="Q13" Type="String" />
    <asp:Parameter Name="Q14" Type="String" />
    <asp:Parameter Name="Q15" Type="String" />
    <asp:Parameter Name="Q16" Type="String" />
    <asp:Parameter Name="CreateDateTime" Type="DateTime" />
  </InsertParameters>
  <UpdateParameters>
    <asp:Parameter Name="ResidentName" Type="String" />
    <asp:Parameter Name="Q1" Type="String" />
    <asp:Parameter Name="Q2" Type="String" />
    <asp:Parameter Name="Q3" Type="String" />
    <asp:Parameter Name="Q4" Type="String" />
    <asp:Parameter Name="Q5" Type="String" />
    <asp:Parameter Name="Q6" Type="String" />
    <asp:Parameter Name="Q7" Type="String" />
    <asp:Parameter Name="Q8" Type="String" />
    <asp:Parameter Name="Q9" Type="String" />
    <asp:Parameter Name="Q10" Type="String" />
    <asp:Parameter Name="Q11" Type="String" />
    <asp:Parameter Name="Q12" Type="String" />
    <asp:Parameter Name="Q13" Type="String" />
    <asp:Parameter Name="Q14" Type="String" />
    <asp:Parameter Name="Q15" Type="String" />
    <asp:Parameter Name="Q16" Type="String" />
    <asp:Parameter Name="CreateDateTime" Type="DateTime" />
    <asp:Parameter Name="Id" Type="Int32" />
  </UpdateParameters>
</asp:SqlDataSource>


 <div align="center">
<table width="650" cellspacing="0" cellpadding="0" border="0" height="107">
<tr>
<td align="left" valign="top" >
<font face="Arial" size="2">   

       
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Week I Exam</title>



 
<p><font face="Arial" size="+2" color="#00458a"><strong>Week I</font>
<br><font face="Arial" size="2" color="#00458a">
<br>Online Exam         
 </strong>
 </font>
<br>
 <br>
 
 
 <table  width="650" cellpadding="5">


<tr>
<td valign="top" align="left" bgcolor="#00458a" colspan="2">
<font face="arial" color="white" size="2" >
<p><strong><font face="Arial" size="3">Question 1</font></strong>
<p>Question one context is located here.
<p><strong>Based on these findings, what would you do next?</strong>
</td>
</tr>
<tr>
<td bgcolor="#d7ebff" align="left" class="style2">
<font face="arial" size="2">
A. Question A Answer
</td>
<td rowspan="4" bgcolor="#00458a" align="left">
<font face="arial" size="2" color="#ffffff">
<asp:RadioButtonList ID="Q1" runat="server" Font-Bold="True"
    Font-Names="Arial" Font-Size="10pt"
    ForeColor="White" ViewStateMode="Enabled">
    <asp:ListItem Value="A: A Answert">A</asp:ListItem>
    <asp:ListItem Value="B: B Answer.">B</asp:ListItem>
    <asp:ListItem Value="C: C Answer.">C</asp:ListItem>
    <asp:ListItem Value="D: D Answer">D</asp:ListItem>
    </asp:RadioButtonList>   
          </td>
</tr>
<tr>
<td bgcolor="#77bbff"align="left" class="style2">
<font face="arial" size="2">
B. Question B Answer.
</td>
</tr>
<tr>
<td bgcolor="#d7ebff" align="left" class="style2">
<font face="arial" size="2">
C. Question C Answer
</td>
</tr>
<tr>
<td bgcolor="#77bbff"align="left" class="style2">
<font face="arial" size="2">
D. Question D Answer.
</font>
</td>
</tr>
<tr>
<td bgcolor="#00458a" valign="top" align="left" colspan="2">
<font face="Arial" color="white" size="2"><strong>
Comments:</strong>
<br>
<asp:TextBox ID="Q1Comments" runat="server" Height="49px" Width="657px"
    ontextchanged="Q1Comments_TextChanged" TextMode="MultiLine"></asp:TextBox>
</font>
</td>
</tr>


Imar December 1st, 2011 10:16 AM

Hi there,

Try posting this in a more general ASP.NET cateogry (http://p2p.wrox.com/asp-net-asp-12/) as you'll attract more viewers...

Cheers,

Imar

jpjamie December 1st, 2011 11:00 AM

Quote:

Originally Posted by Imar (Post 278901)
Hi there,

Try posting this in a more general ASP.NET cateogry (http://p2p.wrox.com/asp-net-asp-12/) as you'll attract more viewers...

Cheers,

Imar

Thanks! I just clicked on the link you provided and "damolekunfemi" is working on the same kind of quiz!

jpjamie December 4th, 2011 08:42 PM

Quote:

Originally Posted by jpjamie (Post 278871)
I'm working through the book and so far I'm up to Chapter 13.
Thought I would attempt some original code and having difficulties inserting data into a table via an online exam....

Below is the *.aspx code from a Form with the first question only of a multiple choice online exam which has 16 multiple choice radio button (A,B, C, D) questions along with a comment. All of the 15 remaining questions are code structured the same.

At the very bottom of this page is a submit button, which first validates all questions have been answered. If any questions are not answered the user gets a prompt to answer the missed question(s). If validation passes, the user is sent to another page where all the questions with his/her selected answers are displayed along with any comments.

What is missing is all the CRUD (Create, Read, Update, Insert), especially Insert, since my primary objective is to just insert all the selected answers. I have a database table and it's available in Visual Studio 2010's Server Explorer, but so far I'm at a loss for inserting data into the database table. While it's relatively easy to click an drag GirdView and/or ListView to validate database interactivity, how is this done without them?

I need help with the code (examples?) for inserting all the radio button selections into my database table.
Any help would be greatly appreciated.

Here is the code for just the first multiple choice question...

Code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Week1Exam.aspx.cs" Inherits="WeekExams_WeekI_Week1Exam" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Online Exam</title>

  <link href="../../Style/Style2.css" rel="stylesheet" type="text/css" />


    <style type="text/css">
      .style2
      {
        width: 1021px;
   
      </style>


</head>
<body>

 <form id="form1" runat="server">
 
 
 <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
  AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="Id"
  DataSourceID="SqlDataSource1">
  <Columns>
    <asp:CommandField ShowSelectButton="True" />
    <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False"
      ReadOnly="True" SortExpression="Id" />
    <asp:BoundField DataField="ResidentName" HeaderText="ResidentName"
      SortExpression="ResidentName" />
    <asp:BoundField DataField="Q1" HeaderText="Q1" SortExpression="Q1" />
    <asp:BoundField DataField="Q2" HeaderText="Q2" SortExpression="Q2" />
    <asp:BoundField DataField="Q3" HeaderText="Q3" SortExpression="Q3" />
    <asp:BoundField DataField="Q4" HeaderText="Q4" SortExpression="Q4" />
    <asp:BoundField DataField="Q5" HeaderText="Q5" SortExpression="Q5" />
    <asp:BoundField DataField="Q6" HeaderText="Q6" SortExpression="Q6" />
    <asp:BoundField DataField="Q7" HeaderText="Q7" SortExpression="Q7" />
    <asp:BoundField DataField="Q8" HeaderText="Q8" SortExpression="Q8" />
    <asp:BoundField DataField="Q9" HeaderText="Q9" SortExpression="Q9" />
    <asp:BoundField DataField="Q10" HeaderText="Q10" SortExpression="Q10" />
    <asp:BoundField DataField="Q11" HeaderText="Q11" SortExpression="Q11" />
    <asp:BoundField DataField="Q12" HeaderText="Q12" SortExpression="Q12" />
    <asp:BoundField DataField="Q13" HeaderText="Q13" SortExpression="Q13" />
    <asp:BoundField DataField="Q14" HeaderText="Q14" SortExpression="Q14" />
    <asp:BoundField DataField="Q15" HeaderText="Q15" SortExpression="Q15" />
    <asp:BoundField DataField="Q16" HeaderText="Q16" SortExpression="Q16" />
    <asp:BoundField DataField="CreateDateTime" HeaderText="CreateDateTime"
      SortExpression="CreateDateTime" />
  </Columns>
 </asp:GridView>
 
 <asp:SqlDataSource ID="SqlDataSource1" runat="server"
  ConnectionString="<%$ ConnectionStrings:CCSExamsConnectionString1 %>"
  SelectCommand="SELECT [Id], [ResidentName], [Q1], [Q2], [Q3], [Q4], [Q5], [Q6], [Q7], [Q8], [Q9], [Q10], [Q11], [Q12], [Q13], [Q14], [Q15], [Q16], [CreateDateTime] FROM [Week1]"
  InsertCommand="INSERT INTO [Week1] ([ResidentName], [Q1], [Q2], [Q3], [Q4], [Q5], [Q6], [Q7], [Q8], [Q9], [Q10], [Q11], [Q12], [Q13], [Q14], [Q15], [Q16], [CreateDateTime]) VALUES (@ResidentName, @Q1, @Q2, @Q3, @Q4, @Q5, @Q6, @Q7, @Q8, @Q9, @Q10, @Q11, @Q12, @Q13, @Q14, @Q15, @Q16, @CreateDateTime)"
  DeleteCommand="DELETE FROM [Week1] WHERE [Id] = @Id"
  UpdateCommand="UPDATE [Week1] SET [ResidentName] = @ResidentName, [Q1] = @Q1, [Q2] = @Q2, [Q3] = @Q3, [Q4] = @Q4, [Q5] = @Q5, [Q6] = @Q6, [Q7] = @Q7, [Q8] = @Q8, [Q9] = @Q9, [Q10] = @Q10, [Q11] = @Q11, [Q12] = @Q12, [Q13] = @Q13, [Q14] = @Q14, [Q15] = @Q15, [Q16] = @Q16, [CreateDateTime] = @CreateDateTime WHERE [Id] = @Id">
  <DeleteParameters>
    <asp:Parameter Name="Id" Type="Int32" />
  </DeleteParameters>
  <InsertParameters>
    <asp:Parameter Name="ResidentName" Type="String" />
    <asp:Parameter Name="Q1" Type="String" />
    <asp:Parameter Name="Q2" Type="String" />
    <asp:Parameter Name="Q3" Type="String" />
    <asp:Parameter Name="Q4" Type="String" />
    <asp:Parameter Name="Q5" Type="String" />
    <asp:Parameter Name="Q6" Type="String" />
    <asp:Parameter Name="Q7" Type="String" />
    <asp:Parameter Name="Q8" Type="String" />
    <asp:Parameter Name="Q9" Type="String" />
    <asp:Parameter Name="Q10" Type="String" />
    <asp:Parameter Name="Q11" Type="String" />
    <asp:Parameter Name="Q12" Type="String" />
    <asp:Parameter Name="Q13" Type="String" />
    <asp:Parameter Name="Q14" Type="String" />
    <asp:Parameter Name="Q15" Type="String" />
    <asp:Parameter Name="Q16" Type="String" />
    <asp:Parameter Name="CreateDateTime" Type="DateTime" />
  </InsertParameters>
  <UpdateParameters>
    <asp:Parameter Name="ResidentName" Type="String" />
    <asp:Parameter Name="Q1" Type="String" />
    <asp:Parameter Name="Q2" Type="String" />
    <asp:Parameter Name="Q3" Type="String" />
    <asp:Parameter Name="Q4" Type="String" />
    <asp:Parameter Name="Q5" Type="String" />
    <asp:Parameter Name="Q6" Type="String" />
    <asp:Parameter Name="Q7" Type="String" />
    <asp:Parameter Name="Q8" Type="String" />
    <asp:Parameter Name="Q9" Type="String" />
    <asp:Parameter Name="Q10" Type="String" />
    <asp:Parameter Name="Q11" Type="String" />
    <asp:Parameter Name="Q12" Type="String" />
    <asp:Parameter Name="Q13" Type="String" />
    <asp:Parameter Name="Q14" Type="String" />
    <asp:Parameter Name="Q15" Type="String" />
    <asp:Parameter Name="Q16" Type="String" />
    <asp:Parameter Name="CreateDateTime" Type="DateTime" />
    <asp:Parameter Name="Id" Type="Int32" />
  </UpdateParameters>
</asp:SqlDataSource>


 <div align="center">
<table width="650" cellspacing="0" cellpadding="0" border="0" height="107">
<tr>
<td align="left" valign="top" >
<font face="Arial" size="2">   

       
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Week I Exam</title>



 
<p><font face="Arial" size="+2" color="#00458a"><strong>Week I</font>
<br><font face="Arial" size="2" color="#00458a">
<br>Online Exam         
 </strong>
 </font>
<br>
 <br>
 
 
 <table  width="650" cellpadding="5">


<tr>
<td valign="top" align="left" bgcolor="#00458a" colspan="2">
<font face="arial" color="white" size="2" >
<p><strong><font face="Arial" size="3">Question 1</font></strong>
<p>Question one context is located here.
<p><strong>Based on these findings, what would you do next?</strong>
</td>
</tr>
<tr>
<td bgcolor="#d7ebff" align="left" class="style2">
<font face="arial" size="2">
A. Question A Answer
</td>
<td rowspan="4" bgcolor="#00458a" align="left">
<font face="arial" size="2" color="#ffffff">
<asp:RadioButtonList ID="Q1" runat="server" Font-Bold="True"
    Font-Names="Arial" Font-Size="10pt"
    ForeColor="White" ViewStateMode="Enabled">
    <asp:ListItem Value="A: A Answert">A</asp:ListItem>
    <asp:ListItem Value="B: B Answer.">B</asp:ListItem>
    <asp:ListItem Value="C: C Answer.">C</asp:ListItem>
    <asp:ListItem Value="D: D Answer">D</asp:ListItem>
    </asp:RadioButtonList>   
          </td>
</tr>
<tr>
<td bgcolor="#77bbff"align="left" class="style2">
<font face="arial" size="2">
B. Question B Answer.
</td>
</tr>
<tr>
<td bgcolor="#d7ebff" align="left" class="style2">
<font face="arial" size="2">
C. Question C Answer
</td>
</tr>
<tr>
<td bgcolor="#77bbff"align="left" class="style2">
<font face="arial" size="2">
D. Question D Answer.
</font>
</td>
</tr>
<tr>
<td bgcolor="#00458a" valign="top" align="left" colspan="2">
<font face="Arial" color="white" size="2"><strong>
Comments:</strong>
<br>
<asp:TextBox ID="Q1Comments" runat="server" Height="49px" Width="657px"
    ontextchanged="Q1Comments_TextChanged" TextMode="MultiLine"></asp:TextBox>
</font>
</td>
</tr>


UPDATE:
After a lot of searching I now fully realize I need to write C# code in the code behind file for this quiz form. Visual Studio's GridView and DetailsView WILL connect to your database table for basic CRUD, but the interface (to use an loose analogy) is like baking the same "cake" over and over with different flavors. The graphical user interface choice is basically the same. C# code (in the code behind file) is needed to break the "Gridlock" of GridView.

Imar December 5th, 2011 05:44 AM

You may want to take another look at Chapter 15, page 554 and further on "hand-coding data access code". It shows similar concepts using the Entity Framework, freeing you from some of the complex code needed to do it all yourself.....

Cheers,

Imar


All times are GMT -4. The time now is 12:26 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.