Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have multiple session objects getting from the previous page and this data should binded to gridview in another page. previous page does not have any gridview. While button click event fired all the data is stored into session objects and then binded to the gridview. when the data is binded to the gridview using session objects then it is redirect to error page where i am doing wrong. why i require session object is that in my table only 5 columns but i require to show 11 columns to the user

What I have tried:

if (!IsPostBack)
{
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[11] { new DataColumn("UserName"), new DataColumn("Subject"), new DataColumn("Exam Date"), new DataColumn("Score"), new DataColumn("Pass Mark"), new DataColumn("Total Marks"), new DataColumn("Number of Answered Questions"), new DataColumn("Number of UnAnswered Questions"), new DataColumn("Correct Choice Entered"), new DataColumn("Wrong Choice Entered"), new DataColumn("Status") });
    ViewState["ExamResults"] = dt;

    // Checking whether Viewstate is null or not
    if (ViewState["ExamResults"] != null)
        BindGridView();
    else
    {
        Panel_myresultshow_warning.Visible = true;
        lblMyResultsShowWarning.Text = "Sorry! There is no result of yours in this application";
    }

    InsertIntoGridView();
}

protected void BindGridView()
{
    GvExamResults.DataSource = (DataTable)ViewState["ExamResults"];
    GvExamResults.DataBind();
}

protected void InsertIntoGridView()
{
    DataTable dt = (DataTable)ViewState["ExamResults"];
    dt.Rows.Add(SUserName, Session["Subject"].ToString(), Session["DateOfExam"].ToString(), Session["MarksObtained"].ToString(), Session["PassMark"].ToString(), Session["TotalMarks"].ToString(), Session["AnsweredCount"].ToString(), Session["UnAnsweredCount"].ToString(), Session["CorrectAnswerCount"].ToString(), Session["WrongAnswerCount"].ToString(), Session["Status"].ToString());
    ViewState["ExamResults"] = dt;

   // Checking whether Viewstate is null or not
   if (ViewState["ExamResults"] != null)
       BindGridView();
   else
   {
       Panel_myresultshow_warning.Visible = true;
       lblMyResultsShowWarning.Text = "Something went wrong. Please try after sometime later</br> Contact you developer for this problem";
   }
}
Posted

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