I am new to ASP.NET C#, I am working to develop web forms to send and receive messages (SQL Server as database) like emails. I want to store the result of SQL query in Session Variable and use it later in another page.
Kindly help me how can I do that, please correct if any things goes wrong.
Here is my code:
SqlConnection con = new SqlConnection("Data Source=AZEEM\\SQLEXPRESS; Initial Catalog=kics;User ID=sa;Password=123");
con.Open();
String query = "select username as sender,subject,message from emailtable where receiver='" + Session["username"] + "'";
enter code here
//this is the query for which I want to store the result in variable myvar, how can I store the result of following query in variable myvar and use it later, when I execute it, string is shown instead of result of string.
String myvar = "select receiver from emailtable where username='" + Session["username"] + "'";
SqlDataReader reader = null;
SqlCommand cmd = new SqlCommand(query, con);
SqlCommand cmd2 = new SqlCommand(myvar, con);
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
reader = cmd2.ExecuteReader();
GridView1.DataSource = dt;
GridView1.DataBind();
DataSet ds = new DataSet();
ds = myvar;
Try below,
SqlConnection con = new SqlConnection("Data Source=AZEEM\\SQLEXPRESS; Initial Catalog=kics;User ID=sa;Password=123");
con.Open();
String query = "select username as sender,subject,message from emailtable where receiver='" + Session["username"] + "'";
enter code here
//this is the query for which I want to store the result in variable myvar, how can I store the result of following query in variable myvar and use it later, when I execute it, string is shown instead of result of string.
String myvar = "select receiver from emailtable where username='" + Session["username"] + "'";
SqlDataReader reader = null;
SqlCommand cmd = new SqlCommand(query, con);
SqlCommand cmd2 = new SqlCommand(myvar, con);
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
reader = cmd2.ExecuteReader();
GridView1.DataSource = dt;
GridView1.DataBind();
session["dt"] = dt;
When you retrieve,
if (session["dt"] != null) {
DataTable dt = (DataTable)session["dt"];
}
You Need to Pass that Query to this Method which will return you the result Set and then try to assign it to the Session
String myvar = "select receiver from emailtable where username='" + Session["username"] + "'";
Session["myvar"] = GetData(myvar);
Method for GetData as Follows
private static DataTable GetData(string query)
{
DataTable dt = new DataTable();
string constr = "Data Source=AZEEM\\SQLEXPRESS; Initial Catalog=kics;User ID=sa;Password=123";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
sda.SelectCommand = cmd;
sda.Fill(dt);
}
}
return dt;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.