当我单击asp.net中的上一个按钮时如何插入非重复值 [英] How to insert non repeated values when I click previous button in asp.net

查看:58
本文介绍了当我单击asp.net中的上一个按钮时如何插入非重复值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在asp.net c#中创建了在线考试应用程序(基于Web).在我的应用程序中

I have created online exam application (web based) in asp.net c#. In my application

  • 第一个表单包含一个下拉列表,用于测试与一个开始按钮.

  • The first form contains a drop down list for tests & a start button.

当我从下拉列表中选择一个特定的测试,然后单击开始"按钮时,它将转到下一页.

When i select a particular test from the dropdownlist and then clicking the start button ,it goes to the next page.

此页面包含一个标签问题,单选按钮列表答案,下一个&上一个按钮.

this page contains one label for question, radiobuttonlist for answers, next & previous button.

在第一种形式:开始按钮的click事件中,我创建了非重复的随机值(用于问题ID),即存储在数组中.当它重定向到另一个页面时,该数组中的第一个问题将显示并带有答案&单击下一个按钮后,将出现下一个问题,在这里我已将用户在数据库中插入了选定的值(单选按钮列表中选择的答案)以计算得分.问题是,当单击上一个按钮然后选择另一个答案时,它重新插入记录,因为我想更新该值,而不是重复的值.我为此编写了以下代码,但是它不起作用.在这里

In the first form :the click event of the start button, i have created non repeated random values (for question ids) i.e stored in array. when it redirects to another page then 1st question from that array will display with answers & after clicking next button next question will appear, here i have inserted selected values(answers selected in radiobuttonlist) by user in database to calculate score.The Problem is when clicking the previous button and then selecting another answer then it reinserts the record again as i want to update that value, not the repeated value. I have written the following code for this but it doesn't work. Here

if (result != DBNull.Value)

此情况无法正常运行,因为我想检查该值是否存在.

this condition doesn't work correctly as i want to check that value exists or not.

请帮助我吗?

 if (Convert.ToInt32(Session["Counter"]) <= 7)
        {
            //a is the array of random values where Counter is my counter start value=1
            SqlCommand cmd = new SqlCommand("SELECT selected from Sel_Ans WHERE Quid=" + a[Convert.ToInt32(Session["Counter"]) - 1] + "", sqlconn);
            sqlconn.Open();
            var result = cmd.ExecuteScalar();
            if (result != DBNull.Value)
            {
                cmd = new SqlCommand("Update Sel_Ans Set Selected=@Selected WHERE Quid=" + a[Convert.ToInt32(Session["Counter"]) - 1] + "", sqlconn);
                SqlParameter Selected = cmd.Parameters.Add("@Selected", SqlDbType.Int, 50);

                Selected.Value = rb;
                sqlconn.Close();
                sqlconn.Open();
                cmd.ExecuteNonQuery();
                sqlconn.Close();

            }
            else
            {
                int t = Convert.ToInt32(Session["Counter"]);

                cmd = new SqlCommand("INSERT INTO Sel_Ans VALUES ('1', @Quid, @Selected, '1')", sqlconn);
                SqlParameter Quid = cmd.Parameters.Add("@Quid", SqlDbType.Int, 50);
                Quid.Value = a[t - 2];

                SqlParameter Selected = cmd.Parameters.Add("@Selected", SqlDbType.Int, 50);
                Selected.Value = rb;
                sqlconn.Close();
                sqlconn.Open();
                cmd.ExecuteNonQuery();
                sqlconn.Close();
            }
            sqlconn.Close();
        }

推荐答案

结果应指示是否插入新记录(重复键),否则请更新记录.

the result should indicate whether it inserts new record or not( duplicate key),if not Update the record instead.

 SqlCommand cmd = new SqlCommand("SELECT Count(*)from Sel_Ans WHERE Quid=" + a[Convert.ToInt32(Session["Counter"]) - 1] + "", sqlconn);

            sqlconn.Open();
            int result = cmd.ExecuteScalar();
            if (result != 0)

这篇关于当我单击asp.net中的上一个按钮时如何插入非重复值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆