Click here to Skip to main content
15,881,819 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm doing a task of accepting data from user and without saving it in database i'm showing that data in grid
when user clicks btnGo data in txtbx is shown in gridview present in page..
when user submits data second time using txtbx new row is generated and shown in grid with its old data for this i'm doing this:

C#
public partial class Store_Table_in_List : System.Web.UI.Page
{
    
    static int count;

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["list"] = "";
            count = 0;
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        count++;
        List<a> list1 = new List<a>();
        a class1 = new a();
        class1.No = count.ToString();
        class1.Fname = txtFname.Text;
        class1.Lname = txtLname.Text;

        list1.Add(class1);
        Session["list1"] += list1.ToString();
        GridView1.DataSource =  (DataTable)Session["list1"];///throws exception: Unable to cast   //object of type 'System.String' to type 'System.Data.DataTable'.
        GridView1.DataBind();
        txtFname.Text="";
        txtLname.Text="";
    }

}
class a
{
    public string No { get; set; }
    public string Fname { get; set; }
    public string Lname { get; set; }
}

the problem is: how can i use session as my datasource...?

I know this can be solved by using DataTable's object but what if I dont want to use DataTable's Object....?
is there any way of doing this without DataTable...?

Thanks in advance.... :)
Posted
Updated 8-Oct-13 0:36am
v2
Comments
Dholakiya Ankit 8-Oct-13 6:51am    
Session["list"] = ""; at this line you should understand that you have passed string to session and using as a datatable what is this?

prashantttt wrote:
how can i use session as my datasource
This is very bad idea. Session is not meant for such stuff. Storing large values inside Session is not recomended. You must refine your logic/design to some extent.

Regards..
 
Share this answer
 
Hi,
Please modify your Button click Code like below:
C#
protected void Button1_Click(object sender, EventArgs e)
   {
       count++;
       List<a> list1 = new List<a>();
       a class1 = new a();
       class1.No = count.ToString();
       class1.Fname = txtFname.Text;
       class1.Lname = txtLname.Text;

       list1.Add(class1);

       List<a> tempList = new List<a>();
       tempList = (List<a>)Session["list1"];
       list1.AddRange(tempList);

       Session["list1"] = list1.ToString();
       GridView1.DataSource = (DataTable)Session["list1"];
       GridView1.DataBind();
       txtFname.Text = "";
       txtLname.Text = "";
   }
I used a new temp List to store value of session and then add it to actual List
 
Share this answer
 
v2
C#
public partial class Store_Table_in_List : System.Web.UI.Page
{

    static int count;

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["list"] = "";
            count = 0;
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        count++;
        List<a> list1 = new List<a>();
        a class1 = new a();
        class1.No = count.ToString();
        class1.Fname = txtFname.Text;
        class1.Lname = txtLname.Text;

        list1.Add(class1);
        Session["list1"] += list1.ToString();
        GridView1.DataSource =  Session["list1"];
        GridView1.DataBind();
        txtFname.Text="";
        txtLname.Text="";
    }

}
[Serilizable]
class a
{
    public string No { get; set; }
    public string Fname { get; set; }
    public string Lname { get; set; }
}
 
Share this answer
 

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