Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am creating an application in which i retrive data from database. i am store data in session variable and display a data but session display only one record. but in database contains 10 record . i want session should display 10 record.

C#
protected void Page_Load(object sender, EventArgs e)
   {
       SqlConnection con = new SqlConnection("Data Source=Aarambh;Initial Catalog=rebuild_technology;Integrated Security=True");
       SqlCommand cmd = new SqlCommand("select * from content_managment ", con);
       DataSet ds = new DataSet("temp");
       SqlDataAdapter ad = new SqlDataAdapter();

       ad.SelectCommand = cmd;
       ad.Fill(ds);
      // DataRow dr = ds.Tables[0].Rows[0];
       Session["divhtml_heading1"] = ds.Tables[0].Rows[0];
      // Session["divhtml_heading2"] = dr["divhtml_content"].ToString();

   }
Posted

you can store whole datatable in session and when you getting data then cast session in datatable. like that

C#
Session["SessionName"] = dt; // dt is data table

// get data like this

DataTable Dtbl= (DataTable)Session["SessionName"];
 
Share this answer
 
Comments
VJ Reddy 16-Jun-12 2:02am    
Good answer. 5!
Session["divhtml_heading1"] = ds.Tables[0].Rows[0];
You stored one Row value in session and so you just get back one!

Do:
C#
Session["divhtml_heading1"] = ds.Tables[0];
// Later to retrieve it
DataTable dt = (DataTable)Session["divhtml_heading1"];


Refer:
MSDN: ASP.NET Session State Overview[^]
 
Share this answer
 
Comments
VJ Reddy 16-Jun-12 2:04am    
Good answer. 5!
Sandeep Mewara 16-Jun-12 4:43am    
Thanks VJ.
Hello Manohar

Just try this

C#
SqlCommand cmd1 = new SqlCommand("select * from content_managment ", con);


SqlDataReader dr1 = cmd1.ExecuteReader();
var yourList = new List<int>();
if (dr1.HasRows)
{
    while (dr1.Read())
    {
       yourList.Add(Convert.ToInt32(dr1[0]));


    }
}
Session["divhtml_heading1"] = yourList;
dr1.Close();
cmd1.Dispose();
 
Share this answer
 
v2
Hi,

because you have selected a row to store in your session variable.

C#
Session["divhtml_heading1"] = ds.Tables[0].Rows[0];

instead
C#
Session["divhtml_heading1"] = ds.Tables[0];


use above code and iterate through all the rows,

although this is not the right way of storing data in session variable and display on your page. instead you can return List of your data and bind it with your page.

Let us know why you would like to store in session ? there should be some strong reason for storing information in session.

thanks
-Amit
 
Share this answer
 
v2

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