Tuesday, 2 September 2014

Quiz question that calculate Correct answers

<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1" DataKeyNames="Quiz_id"
        OnSelectedIndexChanged="ListView1_SelectedIndexChanged" >
        <ItemTemplate>
          <span style=" ">
                <asp:Label ID="Quiz_idLabel" Visible="false" runat="server" Text='<%# Eval("Quiz_id") %>' />
                <br />
         
                       
            </span>
          <table width="800px"  align="center">
          <tr>
                    <td width="20%">
                        <span style="font-family: 'Times New Roman', Times, serif; font-size: 18px; color: #0000FF; ">
                    </td>
                    <td width="80%">
                        <span style="font-family: 'Times New Roman', Times, serif; font-size: 16px; color: #800080; font-weight: bold">
                              <asp:Label ID="Quiz_catLabel" Visible="false"  runat="server" Text='<%# Eval("Quiz_cat") %>' />
                        </span>
                    </td>
                </tr>
         
         
                <tr>
                    <td>
                        <span style="font-family: 'Times New Roman', Times, serif; font-size: 18px; color: #000000; font-weight: bold"> Question:
                    </td>
                    <td>
                        <span style="font-family: 'Times New Roman', Times, serif; font-size: 16px; color: #800080; font-weight: bold">
                            <asp:Label ID="Quiz_nameLabel" runat="server" Text='<%# Eval("Quiz_name") %>' />
                        </span>
                    </td>
                </tr>
                <tr>
                    <td>
                        <span style="">  </span>
                    </td>
                    <td>
                       
                            <asp:RadioButton ID="Quiz_opt1Label"  GroupName="sanj" runat="server"
                                Text='<%# Eval("Quiz_opt1") %>' />
                       
                    </td>
                </tr>
                 <tr>
                    <td >
                     
                    </td>
                    <td>
                     <asp:RadioButton ID="Quiz_opt2Label"  GroupName="sanj" runat="server"
                                Text='<%# Eval("Quiz_opt2") %>' />
                    </td>
                   
                </tr>
                <tr>
                    <td >
                     
                    </td>
                    <td>
                     <asp:RadioButton ID="Quiz_opt3Label"  GroupName="sanj" runat="server"
                                Text='<%# Eval("Quiz_opt3") %>' />
                    </td>
                   
                </tr>
                 <tr>
                    <td >
                     
                    </td>
                    <td>
                     <asp:RadioButton ID="Quiz_opt4Label"  GroupName="sanj" runat="server"
                                Text='<%# Eval("Quiz_opt4") %>' />
                         
                    </td>
                   
                </tr>
                <tr>
                    <td >
                     
                    </td>
                    <td>
                 
                     <asp:Label ID="Quiz_ansLabel" Visible="false" runat="server" Text='<%# Eval("Quiz_ans") %>' />
                      </td>
                </tr>
            </table>
         
        </ItemTemplate>
        <LayoutTemplate>
            <div style="left: 100px; right: 100px" id="itemPlaceholderContainer" runat="server">
                <span runat="server" id="itemPlaceholder" />
            </div>
            <div  style="font-family: Garamond; font-size: medium; font-weight: bold; color: #FF0000">
                <asp:DataPager ID="DataPager1" runat="server" PageSize="21">
                        <Fields>
                            <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="False"
                                ShowLastPageButton="False" ShowPreviousPageButton="False" ShowNextPageButton="False" />
                                <asp:TemplatePagerField >
       <PagerTemplate>
         
           <asp:Label runat="server" ID="lbCurrentPage"
             Text="<%# Container.TotalRowCount>0 ? (Container.StartRowIndex / Container.PageSize) + 1 : 0 %>" />
           of
           <asp:Label runat="server" ID="lbTotalPages"
             Text="<%# Math.Ceiling ((double)Container.TotalRowCount / Container.PageSize) %>" />
           (Total:
           <asp:Label runat="server" ID="lbTotalItems"
             Text="<%# Container.TotalRowCount%>" />
           Question)                    
       </PagerTemplate>
   </asp:TemplatePagerField>
                        </Fields>
                    </asp:DataPager>
            </div>
        </LayoutTemplate>
    </asp:ListView>
 
    <asp:Button ID="Button1" runat="server"
            Text="Submit" Width="93px"
        Height="35px" onclick="Button1_Click" />
       
     <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString=""
        SelectCommand="SELECT DISTINCT [Quiz_id], [Quiz_cat], [Quiz_name], [Quiz_opt1], [Quiz_opt2], [Quiz_opt3], [Quiz_opt4], [Quiz_ans] FROM [emed_qiz] WHERE ([Quiz_cat] = @Quiz_cat) ORDER BY [Quiz_id]">
        <SelectParameters>
            <asp:QueryStringParameter DefaultValue="" Name="Quiz_cat" QueryStringField="catvalue"
                Type="String" />
        </SelectParameters>
    </asp:SqlDataSource>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class Test : System.Web.UI.Page
{
    string userinput, qanswer, catvalue;

    Int32 Score, finalscore;
       string registration;
    int registration1;
    string regname;
    Int32 counter;
    protected void Page_Load(object sender, EventArgs e)
    {

        if (Session["username"] != null)
        {
            regname = Session["UserName"].ToString();
            registration = Session["ID1"].ToString();
            catvalue = Request.QueryString.Get("value").ToString();
             Button2.Visible = false;
         
            if (Page.IsPostBack == true)
            {
             
                Button2.Visible = true;
             
             

            }
        }
        else
        {
           

        }


    }
    protected void ListView1_SelectedIndexChanged(object sender, EventArgs e)
    {


    }
    public void option()
    {


        foreach (
)
        {

            RadioButton option1 = (RadioButton)item.FindControl("Quiz_opt1Label");
            RadioButton option2 = (
)item.FindControl("Quiz_opt2Label");
            RadioButton option3 = (RadioButton)item.FindControl("Quiz_opt3Label");
            RadioButton option4 = (RadioButton)item.FindControl("Quiz_opt4Label");
            Label answer = (Label)item.FindControl("Quiz_ansLabel");
            Label question = (Label)item.FindControl("Quiz_nameLabel");
            Label catageory = (Label)item.FindControl("Quiz_catLabel");
            Label quizid = (Label)item.FindControl("Quiz_idLabel");
            if (option1.Checked == true || option2.Checked == true || option3.Checked == true || option4.Checked == true)
            {

                if (option1.Checked == true)
                {
                    userinput = option1.Text.ToString();
                    qanswer = answer.Text.ToString();
                    Score = System.String.Compare(userinput.Trim(), qanswer.Trim());
                 
                }
                if (option2.Checked == true)
                {
                    userinput = option2.Text.ToString();
                    qanswer = answer.Text.ToString();
                    Score = System.String.Compare(userinput.Trim(), qanswer.Trim());
                 
                }
                if (option3.Checked == true)
                {
                    userinput = option3.Text.ToString();
                    qanswer = answer.Text.ToString();
                    Score = System.String.Compare(userinput.Trim(), qanswer.Trim());
                 
                }
                if (option4.Checked == true)
                {
                    userinput = option4.Text.ToString();
                    qanswer = answer.Text.ToString();
                    Score = System.String.Compare(userinput.Trim(), qanswer.Trim());
                 
                }
                con.Open();
                SqlCommand cmd = new SqlCommand("insert into tbl values('" + registration + "','" + regname + "','" + quizid.Text + "','" + question.Text + "','" + answer.Text + "','" + userinput + "','" + catageory.Text + "','" + Score + "')", con);
                cmd.ExecuteNonQuery();

                con.Close();
            }

           
        }

       con.Close();




    }



    protected void Button1_Click(object sender, EventArgs e)
    {
        con.Close();
        option();
         Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Script", "alert('You have Successfully completed the Test. ');", true);
       
     

     
     
    }


    protected void Button2_Click(object sender, EventArgs e)
    {
        con.Open();
           string sql = "select * from tbl where Reg_id ='" + registration + "'";
        SqlCommand cmd1 = new SqlCommand(sql, con);
        SqlDataReader dr = cmd1.ExecuteReader();
        while (dr.Read() == true)
        {

            Label1.Text = dr["Score"].ToString();
            if (Label1.Text.Trim() == "0")
            {
                 finalscore = finalscore + 1;

            }



        }
     
        con.Close();

        con.Open();
 
        SqlCommand cmd = new SqlCommand("insert into tbl values('" + registration.Trim() + "','" + regname.Trim() + "','" + finalscore + "')", con);
        int i = cmd.ExecuteNonQuery();
        con.Close();
        delresult();
        Session.Abandon();
        Session.Clear();
          }
    protected void delresult()
    {

        con.Open();
        SqlCommand cmd2 = new SqlCommand("delete From tbl where Reg_id = '" + registration + "'", con);
        cmd2.ExecuteNonQuery();

        con.Close();
    }
}

No comments:

Post a Comment