1

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;
2
  • ,whiat is your qst actually, clarify more Commented Mar 26, 2016 at 10:08
  • actually I want to store the result of the following query in variable myvar,String myvar = "select receiver from emailtable where username='" + Session["username"] + "'"; I dont know how to do it, when i check the result of this query, it gives me query string but i want result of query. Commented Mar 26, 2016 at 10:19

2 Answers 2

1

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"];
}
Sign up to request clarification or add additional context in comments.

6 Comments

OP problem is different . actually I want to store the result of the following query in variable myvar,String myvar = "select receiver from emailtable where username='" + Session["username"] + "'"; I dont know how to do it, when i check the result of this query, it gives me query string but i want result of query , so i dont think this is what OP is asking
You mean, you just want to store string in session, please see my updated answer.
two points i will clarify you, I'm not the OP , 2. you are understanding the Problem in a wrong way
Well, first I understand that you want to store result of your query, so definitely that will be in datatable. So I saved whole datatable in session. But as per your recent comment, you told that you want to store your variable. So I saved it within session. If you are looking something different, please let me know.
i am not the original Poster (OP ) of this Question
|
0

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;
            }
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.