Click here to Skip to main content
15,918,267 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
There is some problem with the code.
We have a database name QuestionBank(SubCode,Question,Opt1,Opt2,Opt3,Opt4,Ans).
We're facing a problem with marks count and last record.

default.aspx
XML
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div style="height: 496px">

        <asp:TextBox ID="Questionnoinsert" runat="server"></asp:TextBox>
        <br />
        <br />
        <br />
        <br />
        <asp:TextBox ID="Questioninsert" runat="server" TextMode="MultiLine"
            Width="822px"></asp:TextBox>
        <br />
        <br />
        <br />
        <asp:RadioButton ID="Option1insert" runat="server" />
        <br />
        <br />
        <br />
        <asp:RadioButton ID="Option2insert" runat="server" />
        <br />
        <br />
        <br />
        <asp:RadioButton ID="Option3insert" runat="server" />
        <br />
        <br />
        <br />
        <asp:RadioButton ID="Option4insert" runat="server" />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Label ID="ansinsert" runat="server" Text="Selected answer"></asp:Label>
&nbsp;&nbsp;&nbsp;
        <asp:TextBox ID="ansinserttext" runat="server"></asp:TextBox>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Label ID="originalans" runat="server" Text="Answer"></asp:Label>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:TextBox ID="orignalansinsert" runat="server"></asp:TextBox>
        <br />
        <br />
        <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Button ID="Submit" runat="server" Text="Next" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Button ID="btnNext" runat="server" Text="Next" Visible=false/>

    </div>
    </form>
</body>
</html>


default.aspx.vb
VB
Imports System.Data.SqlClient
Imports System.Data
Imports System.Data.SqlClient.SqlConnection
Imports System.Data.DataSet
Imports System.Data.SqlClient.SqlDataReader


Partial Class _Default
    Inherits System.Web.UI.Page
    Dim adp As SqlDataAdapter
    Dim dat As DataSet
    Dim MyDataset As New DataSet()
    Dim objReader As SqlDataReader
    Dim maxrows As String
    Dim i As Integer
    Dim myConnection As SqlConnection = New SqlConnection("initial catalog=Students; integrated security=True")
    Dim MyAdapter As New SqlDataAdapter("SELECT * FROM QuestionBank where SubCode=007", myConnection)
    Dim SqlCommand1 As New SqlCommand("SELECT * FROM QuestionBank where SubCode=007", myConnection)

    Protected Sub Page_Load() Handles Me.Load
        myConnection.Open()
        displayrecord()
        If Not Page.IsPostBack Then
            Session("Record") = 0
        End If
        If Not Page.IsPostBack Then
            Session("Record1") = 0
        End If
    End Sub
    Private Sub displayrecord()
        SqlCommand1.CommandType = CommandType.StoredProcedure = CommandType.Text
        SqlCommand1.Connection = myConnection
        objReader = SqlCommand1.ExecuteReader()
        objReader.Read()
        Questioninsert.Text = objReader("Question")
        Option1insert.Text = objReader("Opt1")
        Option2insert.Text = objReader("Opt2")
        Option3insert.Text = objReader("Opt3")
        Option4insert.Text = objReader("Opt4")
        objReader.Close()
        'myConnection.Close()

    End Sub


    Protected Sub btnNext_Click() Handles btnNext.Click
        Option1insert.Enabled = True
        Option2insert.Enabled = True
        Option3insert.Enabled = True
        Option4insert.Enabled = True
        Option1insert.Checked = False
        Option2insert.Checked = False
        Option3insert.Checked = False
        Option4insert.Checked = False
        ansinserttext.Text = ""
        orignalansinsert.Text = ""
        i = i + 1
        'Dim myConnection As SqlConnection = New SqlConnection("initial catalog=Students; integrated security=True")
        'Dim MyAdapter As New SqlDataAdapter("SELECT * FROM QuestionBank where SubCode=007", myConnection)



        MyAdapter.Fill(MyDataset)

        Dim rec_count As Integer = Session("Record")

        Dim row_count As Integer = MyDataset.Tables(0).Rows.Count - 1

        Response.Write(row_count.ToString())
        If rec_count <> row_count Then

            Session("Record") = rec_count

            rec_count = rec_count


            Questioninsert.Text = MyDataset.Tables(0).Rows(rec_count).Item(1)
            Option1insert.Text = MyDataset.Tables(0).Rows(rec_count).Item(2)
            Option2insert.Text = MyDataset.Tables(0).Rows(rec_count).Item(3)
            Option3insert.Text = MyDataset.Tables(0).Rows(rec_count).Item(4)
            Option4insert.Text = MyDataset.Tables(0).Rows(rec_count).Item(5)


        Else
            MsgBox("Last record")

        End If



    End Sub

    Protected Sub Submit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Submit.Click
        If Option1insert.Checked Then
            ansinserttext.Text = Option1insert.Text
            Option2insert.Enabled = False
            Option3insert.Enabled = False
            Option4insert.Enabled = False

        ElseIf Option2insert.Checked Then
            ansinserttext.Text = Option2insert.Text
            Option1insert.Enabled = False
            Option3insert.Enabled = False
            Option4insert.Enabled = False
        ElseIf Option3insert.Checked Then
            ansinserttext.Text = Option3insert.Text
            Option2insert.Enabled = False
            Option1insert.Enabled = False
            Option4insert.Enabled = False
        ElseIf Option4insert.Checked Then
            ansinserttext.Text = Option4insert.Text
            Option2insert.Enabled = False
            Option3insert.Enabled = False
            Option1insert.Enabled = False
        Else
            MsgBox("Select the answer")
        End If

        'Dim myConnection As SqlConnection = New SqlConnection("initial catalog=Students; integrated security=True")
        'Dim MyAdapter As New SqlDataAdapter("SELECT * FROM QuestionBank where SubCode=007", myConnection)


        MyAdapter.Fill(MyDataset)

        Dim rec_count As Integer = Session("Record")

        Dim row_count As Integer = MyDataset.Tables(0).Rows.Count - 1

        Response.Write(row_count.ToString())
        If rec_count <> row_count Then

        End If

        Session("Record") = rec_count + 1

        rec_count = rec_count + 1
        maxrows = MyDataset.Tables(0).Rows.Count
        'MsgBox(maxrows)
        Dim a As String = increment()
        If a <> maxrows Then
            MsgBox(MyDataset.Tables(0).Rows(rec_count - 1).Item(6))
            'myConnection.Close()

            btnNext_Click()
            i = i + 1
            Dim f As Integer = f + i
        Else
            MsgBox("last record")
        End If



    End Sub
    Public Function increment() As String
        i = i + 1
        Return i
    End Function

End Class
Posted
Updated 22-Feb-11 2:15am
v2
Comments
Sunasara Imdadhusen 22-Feb-11 8:13am    
You should do yourself, no one can do your homework.

Tanwi wrote:
There is some problem with the code.

Tanwi wrote:
We're facing a problem with marks count and last record.

Are really poor descriptions of the problem you're experiencing.


Moreover, IMHO noone would read tons of code without a clearer description of the current program behaviour vs the expected one.
I strongly suggest you reading the Q&A FAQ, before posting again here.
:)
 
Share this answer
 
Comments
Vivek.Net 23-Feb-11 4:35am    
Hiyou wrote all the code, but can you explain what problem you are facing and what result you need from your code? After that only any one can help you. Solving problem without know the problem is a little bit difficult for normal human being like me ;)
Espen Harlinn 25-Feb-11 8:17am    
An obvious response :)
There are many problems with this code

1) why this assignment is like this?
SqlCommand1.CommandType = CommandType.StoredProcedure = CommandType.Text
I think it should be SqlCommand1.CommandType = CommandType.Text as you are using an sql for querying

2) A connection is opened in form_load, but not closed any where. I hope you are handling it somewhere.

3)Not sure you are using the correct connection string

4)Display record reads only one record, not looping through all the records. Is that way you want?. The Datareader is not closed at display record.

5) In the btnNext_Click() event handler you are getting Dim rec_count As Integer = Session("Record") which is assigned 0. Again you are assigning that to 0. Is something else you thought to do there?

Dim rec_count As Integer = Session("Record")

        Dim row_count As Integer = MyDataset.Tables(0).Rows.Count - 1

        Response.Write(row_count.ToString())
        If rec_count <> row_count Then

            Session("Record") = rec_count

            rec_count = rec_count


what you are trying to do here. Dim rec_count As Integer = Session("Record") and Session("Record") = rec_count mutually exclusive statements as rec_count is not changed anywhere.

6)why this function is required?

Public Function increment() As String
        i = i + 1
        Return i
    End Function


instead of simply i++.

7)Dim row_count As Integer = MyDataset.Tables(0).Rows.Count - 1 and maxrows = MyDataset.Tables(0).Rows.Count ... Same value in two different variables...

8)un used variables Dim f As Integer = f + i

still...a lot of redundancy and errors.

So why don't you Clean your code. Describe exact problem. ???!!!. Otherwise nobody will waste their time to clean up your code.
 
Share this answer
 
v2
Comments
thatraja 22-Feb-11 11:33am    
Hi dude, added code block & inline code for readability.
Espen Harlinn 25-Feb-11 8:16am    
Amazing effort, my 5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900