retain data set during post back

Last post 05-18-2008 3:22 AM by yeotumitsu@sify.com. 3 replies.

Sort Posts:

  • retain data set during post back

    05-17-2008, 1:08 AM
    • Loading...
    • vikasrami
    • Joined on 04-23-2008, 1:47 PM
    • Posts 2

     I am trying to create some thing like MCQ(multiple choice question). I am able to display my question on the first page load, but not able to maintain the data on post back, i want that on the next click i want another question display on my page.

     

    public partial class MCQ : System.Web.UI.Page
    {
      static private int i = 0;
       private SqlConnection MyConnection;
       private SqlCommand MyCommand;
       private SqlDataAdapter MyAdapter;
       private DataTable MyTable = new DataTable();
       private DataSet MyDataSet = new DataSet();
        protected void Page_Load(object sender, EventArgs e)
        {
            // Count max colums in questionaries table
            // Create a array of same size = MAX_TEST_SIZE
            // Fill array with random number MAX_ELEMENTS shuold be 
            //Random rd = new Random(1);
            //for(int i =1; i<10; i++)
            //System.Console.WriteLine(rd.Next());
            if (i == 0)
                Previous.Enabled = false;
            else
                Previous.Enabled = true;
    
            if (!Page.IsPostBack)
            {
                // Connection Properties
                MyConnection = new SqlConnection();
                MyConnection.ConnectionString = ConfigurationManager.ConnectionStrings["healingDBConnectionString"].ConnectionString;
                
                MyCommand = new SqlCommand();
                MyCommand.CommandText = "Select * from Questionaries";
                MyCommand.CommandType= CommandType.Text;
                MyCommand.Connection = MyConnection;
                
                MyAdapter = new SqlDataAdapter();
                MyAdapter.SelectCommand = MyCommand;
                MyAdapter.Fill(MyDataSet,"Questionaries");
    
                DisplayQuestion(i++);
                
                gvCustomers.DataSource = MyTable.DefaultView;
                gvCustomers.DataBind();
                //MyAdapter.Dispose();
                //MyCommand.Dispose();
                //MyConnection.Dispose();
    
              }
    
            
    
    
        }
    
        public void DisplayQuestion(int i)
        {
            DataRow pRow = MyDataSet.Tables["Questionaries"].Rows[i];
            Question.Text = pRow["Question"].ToString();
            QuestionNO.Text = pRow["SerialNo"].ToString();
            Option1.Text = pRow["Option1"].ToString();
            Option2.Text = pRow["Option2"].ToString();
            Option3.Text = pRow["Option3"].ToString();
            Option4.Text = pRow["Option4"].ToString();
    
        }
    
        protected void Previous_Click(object sender, EventArgs e)
        {
           
            DisplayQuestion(i);
            i--;
        }
        protected void Next_Click(object sender, EventArgs e)
        {
            
            DisplayQuestion(i);
            i++;
        }
    }
    
     
     
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="MCQ.aspx.cs" Inherits="MCQ" %>
    
    <!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>
        </div>
        <asp:Label ID="Welcome" runat="server" Text="Welcome"></asp:Label>
        <p>
            <asp:Label ID="QuestionNO" runat="server" Text="QuestionNO "></asp:Label>
            <asp:Label ID="Question" runat="server" Text="Question"></asp:Label>
        </p>
        <p>
            <asp:RadioButton ID="Option1" GroupName="Questionaries" runat="server" />
        </p>
        <p>
            <asp:RadioButton ID="Option2" GroupName="Questionaries" runat="server" />
        </p>
        <asp:RadioButton ID="Option3" GroupName="Questionaries" runat="server" />
        <p>
            <asp:RadioButton ID="Option4" GroupName="Questionaries" runat="server" />
        </p>
        <p>
            <asp:Button ID="Previous" runat="server" Text="Previous" Width="100" 
                onclick="Previous_Click" />
            <asp:Button ID="Next" runat="server" Text="Next" Width="100" 
                onclick="Next_Click" />
            <asp:GridView ID="gvCustomers" runat="server" />
        </p>
        </form>
    </body>
    </html>
    
      I wan that when i click on next , some other question should be display.
     
      
  • Re: retain data set during post back

    05-17-2008, 2:07 AM

    Hi,

    When the page is loaded put dataset in Session/ViewState -

    Session["dsMCQ"] = MyDataSet;

    And when you want to pull some data from that use it like -

    ((DataSet)Session["dsMCQ"]).Tables[0].Rows[loop][loop2].ToString(); 

    Hope it helps.

    -Manas

    =======================================
    If this post is useful to you, please mark it as answer.
  • Re: retain data set during post back

    05-18-2008, 2:34 AM
    • Loading...
    • vikasrami
    • Joined on 04-23-2008, 1:47 PM
    • Posts 2

    Hi Manas,

    Thanks a lot,  I am very new to ASP.net, have not done any web programming. I am in system programming.  Can you please suggest some online material to study C# and asp.net?

     

    Vikas  

  • Re: retain data set during post back

    05-18-2008, 3:22 AM
    Answer

     

     Well what I follow is MSDN.

     and you can find all the tutorials/ videos/ code  in Learn and getting started section of this   www.asp.net web site.

    Hope it helps.

    -Manas

    =======================================
    If this post is useful to you, please mark it as answer.
Page 1 of 1 (4 items)
Microsoft Communities
Page view counter