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.
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 rep开发者_如何转开发eated 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.
Please help me?
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.
EDIT:
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)
精彩评论