多用户同时登录

时间:2014-12-06 05:53:59

标签: sql asp.net sql-server-2008 c#-4.0

我创建了一个在线考试系统。它一次只能运行一个用户。但是,当多个用户登录时会出现问题。

每次测试都有20个问题。如果只有一个用户正在进行测试,那么它可以正常工作。用户可以完成所有20个问题。现在另一个用户同时登录。该用户没有收到所有20个问题。说User1已完成12个问题。 User2只会得到8个问题。

假设有User1,User2,User3同时登录。 User1做了8个问题,User2做了6个问题,User3也做了8个问题。他们都会在没有完成20个问题的情况下到达结果页面。这意味着,如果有20个用户,他们只会得到1个问题,而不是20个。有人可以帮忙吗?

  try
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["hasexaminationConnectionString"].ConnectionString);
            SqlCommand cmd = new SqlCommand();
            SqlDataReader dr;
            cmd.Connection = con;
            cmd.CommandText = "select * from tblregister where Name=@Name and EMail=@EMail";

            cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = TxtStName.Text;
            cmd.Parameters.Add("@EMail", SqlDbType.VarChar).Value = TxtStudentID.Text;
            //cmd.Parameters.Add("@Flag", SqlDbType.Int).Value = Convert.ToInt32(HiddenField1.Value);

            con.Open();
            dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                FormsAuthentication.RedirectFromLoginPage(TxtStName.Text, false);
                Session["Name"] = TxtStName.Text;
                Session["EMail"] = TxtStudentID.Text;



                Response.Redirect("TestHome.aspx");

            }
            else
            {
                Label1.Visible = true;

                Label1.Text = "UserName or Password is Required/Incorrect.";
            }
        }
        catch
        { }
    }

下一页:

protected void Page_Load(object sender, EventArgs e)
{
    mob = HttpContext.Current.Session["Name"].ToString();
    number = HttpContext.Current.Session["EMail"].ToString();
    //Response.Cache.SetCacheability(HttpCacheability.NoCache);

    if (Session["mob"] == null & Session["number"] == null)
        //Response.Redirect("Home.aspx");

        if (!IsPostBack)
        {
            DataSet TestList = getTestList("GetTestList");
            DataList1.DataSource = TestList;
            DataList1.DataBind();
        }
}

public DataSet getTestList(string procedurename)
{
    using (DataSet QuestionSet = new DataSet())
    {
        using (DataTable QTable = new DataTable())
        {
            QTable.Columns.Add("TESTNAME");
            QTable.Columns.Add("TESTNUMBER");
            DataTable dt;



            cmd.Connection = con;

            cmd.CommandText = "select * from testnumber";
            using (Da = new SqlDataAdapter(cmd))
            {
                con.Open();
                dt = new DataTable();
                Da.Fill(dt);
                if (dt.Rows.Count > 0)
                {
                    DataRow dr;
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        dr = QTable.NewRow();

                        dr[0] = dt.Rows[i]["testname"].ToString();
                        dr[1] = dt.Rows[i]["testnumber"].ToString();

                        QTable.Rows.Add(dr);
                    }
                }
            }
            QuestionSet.Tables.Add(QTable);

            return QuestionSet;
        }
    }
}


protected void LinkButton_Click(object sender, CommandEventArgs e)
{
    string name = e.CommandArgument.ToString();
    Response.Redirect("TakeTest1.aspx?testno=" + e.CommandArgument.ToString());
}

下:

protected void Page_Load(object sender, EventArgs e)
{
    //UserName = Session["UserName"].ToString();
    //Password = Session["Password"].ToString();

    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["hasexaminationConnectionString"].ConnectionString);
    SqlCommand cmd = new SqlCommand();
    SqlDataReader dr;
    cmd.Connection = con;

    cmd.CommandText = "select username,password,flag from tblcollegeuser where username='" + UserName + "'and password='" + Password + "'";



    con.Open();
    dr = cmd.ExecuteReader();
    if (dr.Read())
    {

        HiddenField1.Value = dr["Flag"].ToString();

        if (HiddenField1.Value.ToString() == "0")
        {
            //Response.Write("<script>alert('Your Session Timer has Expired! We are Sorry!')</script>");
            Response.Redirect("Result.aspx");
        }
    }

    if (HttpContext.Current.Session["Name"] == null && HttpContext.Current.Session["EMail"] == null)
    {
        Response.Write("<script>alert('Your Session Timer has Expired! We are Sorry!')</script>");
        Response.Redirect("default.aspx");
    }

    if (!IsPostBack)
    {
        this.timerStartValue = long.Parse(ConfigurationManager.AppSettings["Delay"].ToString());
        this.TimerInterval = 500;
        tno = Request.QueryString["testno"].ToString();
        //string query = "select * from questions where tnumber='" + tno + "'";
        Questions = GetDataSet(tno);
        totalQs = GetCount(tno);
        LoadQuestion();


        DataSet questions = new DataSet("Questions");
        questions.Tables.Add();

    }
}


protected override void OnPreRender(EventArgs e)
{
    base.OnPreRender(e);
    string strDisAbleBackButton;
    strDisAbleBackButton = "";
    ClientScript.RegisterClientScriptBlock(this.Page.GetType(), "clientScript", strDisAbleBackButton);
}

public DataSet GetDataSet(string query)
{




    using (DataSet QuestionSet = new DataSet())
    {
        using (DataTable QTable = new DataTable())
        {
            QTable.Columns.Add("ROW_NUMBER");
            QTable.Columns.Add("QuestionNo");
            QTable.Columns.Add("Tname");
            QTable.Columns.Add("Tnumber");
            QTable.Columns.Add("question");
            QTable.Columns.Add("ans1");
            QTable.Columns.Add("ans2");
            QTable.Columns.Add("ans3");
            QTable.Columns.Add("ans4");



            DataTable dt;


            using (cmd.Connection = con)
            {
                //cmd.CommandText = " SELECT  * FROM test ORDER BY CHECKSUM(NEWID()) where tnumber='" + query + "'";
                cmd.CommandText = " SELECT * FROM  test WHERE tnumber='" + query + "' ORDER BY CHECKSUM(NEWID()) ";
                //cmd.Parameters.AddWithValue("@cse", query);
                Da = new SqlDataAdapter(cmd);
                con.Open();
                dt = new DataTable();
                Da.Fill(dt);
                if (dt.Rows.Count > 0)
                {

                    DataRow dr;
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        dr = QTable.NewRow();
                        dr[0] = dt.Rows[i]["id"].ToString();
                        dr[1] = "Qno" + dt.Rows[i]["Qno"].ToString();
                        dr[2] = dt.Rows[i]["tname"].ToString();
                        dr[3] = dt.Rows[i]["tnumber"].ToString();
                        dr[4] = dt.Rows[i]["quation"].ToString();
                        dr[5] = dt.Rows[i]["ans1"].ToString();
                        dr[6] = dt.Rows[i]["ans2"].ToString();
                        dr[7] = dt.Rows[i]["ans3"].ToString();
                        dr[8] = dt.Rows[i]["ans4"].ToString();

                        QTable.Rows.Add(dr);
                    }
                }
            }
            QuestionSet.Tables.Add(QTable);

            return QuestionSet;
        }
    }
}


public Int32 GetCount(string tno)
{
    return 10;
}



void Page_PreRender(object sender, EventArgs e)
{
    StringBuilder bldr = new StringBuilder();
    bldr.AppendFormat("var Timer = new myTimer({0},{1},'{2}','timerData');", this.timerStartValue, this.TimerInterval, this.lblTimerCount.ClientID);
    bldr.Append("Timer.go()");
    ClientScript.RegisterStartupScript(this.GetType(), "TimerScript", bldr.ToString(), true);
    ClientScript.RegisterHiddenField("timerData", timerStartValue.ToString());
}

void Page_PreInit(object sender, EventArgs e)
{
    string timerVal = Request.Form["timerData"];
    if (timerVal != null || timerVal == "")
    {
        timerVal = timerVal.Replace(",", String.Empty);
        timerStartValue = long.Parse(timerVal);
    }
}

private Int32 TimerInterval
{
    get
    {
        object o = ViewState["timerInterval"];
        if (o != null) { return Int32.Parse(o.ToString()); }
        return 50;
    }
    set { ViewState["timerInterval"] = value; }

}

void RedirectToResults()
{

    Response.Redirect("Results.aspx");
}

protected void LoadQuestion()
{
    if (Questions.Tables[0].Rows.Count > 0)
    {
        //Load Question;
        DataRow DR = Questions.Tables[0].Rows[0];

        //Question.Text = DR[0].ToString() + " of " + 20;


        //Question.Text = (i + 1).ToString() + " of " + 20;

        sno = DR[1].ToString();
        TestName.Text = DR[2].ToString();
        TestNo.Text = DR[3].ToString();

        Questionlbl.Text = DR[4].ToString();

        rbtnAns.Items.Clear();
        rbtnAns.Items.Add(DR[5].ToString());
        rbtnAns.Items.Add(DR[6].ToString());
        rbtnAns.Items.Add(DR[7].ToString());
        rbtnAns.Items.Add(DR[8].ToString());

        Questions.Tables[0].Rows.Remove(DR);



        if (Questionlbl.Text.Equals(totalQs.ToString()))
        {
            IsLastQs = true;
        }

    }
    else
    {
        //End Of File;
        //Response.Write("<script>alert('Thanks For Your Presence! You Can Leave Now.')</script>");
        //Session.Abandon();
        Session["raj"] = Questions;
        RedirectToResults();
    }
}




protected void Button1_Click(object sender, EventArgs e)
{

    try
    {
        //Write your code here to save the question

        //Displays the Next Question
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["hasexaminationConnectionString"].ConnectionString);
        SqlCommand cmd = new SqlCommand("insert into testdisplay Values ('" + sno + "','" + Session["Name"] + "','" + Session["EMail"] + "','" + Questionlbl.Text + "','" + rbtnAns.SelectedItem.Text + "')", con);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        LoadQuestion();
    }
    catch (Exception ex)
    {
        Response.Write("<script>alert(''" + ex.Message + "'')</script>");
    }



}
protected void Button2_Click(object sender, EventArgs e)
{
    //When Skip Button is pressed it loads the next question
    LoadQuestion();
}

和结果页面:

protected void Page_Load(object sender, EventArgs e)
{


    l = Session["Name"].ToString();
    m = Session["EMail"].ToString();


    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["hasexaminationConnectionString"].ConnectionString);
    SqlCommand cmd = new SqlCommand("select  tnumber,quation,ans1,ans2,ans3,ans4,ans  from test left join testdisplay on (test.ans= testdisplay.answ and test.quation= testdisplay.quations)  where UserName='" + Session["Name"] + "' and Password='" + Session["EMail"] + "'", con);
    con.Open();
    SqlDataAdapter adp = new SqlDataAdapter(cmd);

    adp.Fill(dt);
    con.Close();
    {
        int to = 20;
        GridView1.DataSource = dt;
        GridView1.DataBind();
        int marks = GridView1.Rows.Count;
        Label1.Text = Convert.ToInt32(GridView1.Rows.Count).ToString();
        decimal total = Convert.ToDecimal((double)marks / (double)20) * 100;
        lbltotal.Text = total.ToString();
    }

}
public void bind()
{
    // Write your code to get the summary of the result and display it


}
protected void Button1_Click(object sender, EventArgs e)
{
    //Response.Redirect("Home.aspx");
    string uniqueCode = string.Empty;

    //SqlDataReader dr;
    try
    {
        DataSet ds = new DataSet();
        using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["hasexaminationConnectionString"].ConnectionString))
        {

            con.Open();

            SqlCommand cmd = new SqlCommand("SELECT Name,EMail FROM tblregister Where EMail= '" + txtEmail.Text.Trim() + "'", con);

            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds);




                ////}


                if (Page.IsValid)
                {
                    //GridView1.RenderControl(hw);
                    const string SERVER = "relay-hosting.secureserver.net";
                    MailMessage oMail = new MailMessage();
                    oMail.From = new MailAddress("contact@dssgroups.com");
                    oMail.To.Add(new MailAddress(txtEmail.Text.Trim()));
                    oMail.Subject = "Your Test Details";
                    oMail.IsBodyHtml = true; // enumeration
                    oMail.Priority = MailPriority.High; // enumeration
                    oMail.Body = "Hi, <br/><b>Please check your Test Details:</b><br/><br/>Your Marks Percentage: " + lbltotal.Text+" % "+"<br/>For any query contact "+" http://dssgroups.com";
                    SmtpClient sC = new SmtpClient(SERVER);
                    sC.EnableSsl = false;
                    ContentType contentType = new ContentType();
                    contentType.MediaType = MediaTypeNames.Application.Octet;
                    contentType.Name = "xml.xml";
                    sC.Send(oMail);
                    oMail = null; // free up resources
                    lblMessage.ForeColor = System.Drawing.Color.DarkKhaki;
                    lblMessage.Text = "EMail Sent";
                    ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('EMail Sent');", true);
                }
                else
                {
                    lblMessage.Text = "The Email you entered not exists.";
                }
            }


            //}




        }

    catch (Exception ex)
    {
        Console.WriteLine("{0} Exception caught.", ex);
    }
}


protected void Button2_Click(object sender, EventArgs e)
{

    Session.Clear();
    Session.Abandon();
    Response.Redirect("default.aspx");

}

0 个答案:

没有答案