Click here to Skip to main content
15,921,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
Session["que"] = lb.Text;
            Response.Redirect("DisplayAnswer.aspx");



and in DisplayAnswer.aspx file...

XML
<asp:GridView ID="GridView1" runat="server" OnDataBound="GridView1_DataBound">
        </asp:GridView>



and DisplayAnswer.aspx.cs file...

C#
protected void Page_Load(object sender, EventArgs e)
   {
<pre lang="cs">String conn = "Data Source=localhost;Database= forum;User ID=root;Password=rachana;Allow User Variables=True";

       MySqlConnection con = new MySqlConnection(conn);
       try
       {
           con.Open();
           MySqlCommand md = new MySqlCommand("select answer,userid,date from thread where forumid='"+Session["idq"].ToString()+"' ", con);
            MySqlDataAdapter ada = new MySqlDataAdapter(md);
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            ada.Fill(dt);
            GridView1.DataSource = dt;
            GridView1.DataBind();
<pre lang="cs">}
        catch (Exception ex)
        { }

    }


.............
here i have tried to get Session value of que from lable and redirected to DisplayAnswer.aspx page...on this page i want to select only data where question is Session["que"] which i have added to where clause in select command..it is not giving me any error but also not binding the gridview. it displays nothing..how to bind gridview using session values in where clause??
please explain...
thanx...
Posted
Updated 21-Apr-13 13:56pm
v2
Comments
[no name] 21-Apr-13 19:09pm    
Session["idq"].ToString()...where question is Session["que"]... typo?

This means that query is executing successfully in database but not returning any rows. Your session value might be giving Empty String. Put a breakpoint and check for the session value. Try Like this:
C#
if(Session["idq"]!= "")
{
    con.Open();
    MySqlCommand md = new MySqlCommand("select answer,userid,date from thread where forumid='"+Session["idq"].ToString()+"' ", con);
    MySqlDataAdapter ada = new MySqlDataAdapter(md);
    DataSet ds = new DataSet();
    DataTable dt = new DataTable();
    ada.Fill(dt);
    GridView1.DataSource = dt;
    GridView1.DataBind();
}
else
{
    //Your session is empty. Show the message here.
}

Before using the session value you should always check for its existence. Check whether Session["idq"] is null or not before using it.

--Amit
 
Share this answer
 
Hi Member 9586157,

Also, if the column 'forumid' in your DB is an integer type, then the single quotes in the query
SQL
forumid='"+Session["idq"].ToString()+"' 
is not required.

Just set it as
SQL
forumid="+Session["idq"].ToString()+" 


Thank you,
Vamsi
 
Share this answer
 
Comments
Chanthol Khom 10-Nov-13 20:48pm    
hello, i am a new to asp.net. i want to know,
how can i store a whole gridview in session?

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