Variables

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2008
Posts: 8
Reputation: amith003 is an unknown quantity at this point 
Solved Threads: 0
amith003 amith003 is offline Offline
Newbie Poster

Variables

 
0
  #1
Jan 3rd, 2008
Hi Everyone

I'm using ASP.NET with C# for an application. I want to know how to take values from a database(sqlserver) and store it in session variables in .net. Can anyone pl help?

Thanks in Advance
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 370
Reputation: greeny_1984 is an unknown quantity at this point 
Solved Threads: 29
greeny_1984's Avatar
greeny_1984 greeny_1984 is offline Offline
Posting Whiz

Re: Variables

 
0
  #2
Jan 3rd, 2008
HI,
u just write a select statement for the table u need to assign session .
using a dataset or dataadapter retrieve the data and assign the data to the session
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 1,181
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Solved Threads: 67
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: Variables

 
0
  #3
Jan 3rd, 2008
Use ADO.NET to get data from the database.

I recommend you create a custom class that has a property for each of the data columns. Iterate the database resultset (be it a SqlDataReader or DataTable or whatever) and create an array of your custom class. One per record. Assign the array to the Session.

You could of course just bung the disconnected DataTable or DataSet straight into the session variable. But, they don't serialize very efficiently. I would not recommend this approach if your application has a lot of users say more than 25.
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 2
Reputation: majoka is an unknown quantity at this point 
Solved Threads: 0
majoka majoka is offline Offline
Newbie Poster

Re: Variables

 
0
  #4
Jan 3rd, 2008
add using System.Data.SqlClient;
and add following table data

try
{
int CustomerID;
string CompanyName;
SqlConnection con = new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=northwind");
SqlCommand cmd = new SqlCommand("SELECT CustomerID, CompanyName FROM Customers", con);
cmd.CommandTimeout = 30;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
con.Open();
DataSet ds = new DataSet();
da.Fill(ds, "Customers");
if (ds != null)
{
DataTable dt = ds.Tables[0];
if (dt != null)
{
DataRow dr = dt.Rows[0];
if (dr["CustomerID"].ToString() != "")
{
CustomerID = int.Parse(dr["CustomerID"].ToString());
}
if (dr["CompanyName"].ToString() != "")
{
CompanyName = dr["CompanyName"].ToString();
}
}
}
con.Close();
}
catch (Exception)
{
throw;
}
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 8
Reputation: amith003 is an unknown quantity at this point 
Solved Threads: 0
amith003 amith003 is offline Offline
Newbie Poster

Re: Variables

 
0
  #5
Jan 3rd, 2008
Thank You Very much, The code you gave is working fine.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for ASP.NET
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC