title: binding grid view on button click in listview
body: <p>I have a list view with a button for each row:</p>
  
  <pre><code> &lt;td&gt; &lt;asp:Button ID="Button2" runat="server" Text="select" CommandName="view" CommandArgument='&lt;%# Eval("inquiry_id")%&gt;'   onclick="buttonClick"/&gt;&lt;/td&gt;
  </code></pre>
  
  <p>On click of this button i retrieve the id of the clicked row, set the session with the same id and bind the grid view.This the code behind the button click in list view:</p>
  
  <pre><code>ListViewItem item = (sender as Button).NamingContainer as ListViewItem;
      Button butDetails = (Button)item.FindControl("Button2");
      Int64 inquiryID = Convert.ToInt64(butDetails.CommandArgument);
      Session["session_view_id"] = inquiryID;
      this.BindGrid();               
      return;
  </code></pre>
  
  <p>The session is retrieved in bindGrid function. But the problem is gridview is not displayed at first click but on second click it gets displayed but with the data of the previous id clicked.The session is set but while binding the grid it uses old session value. Where am i going wrong to bind the grid?
  The code for the bindGrid() function is :</p>
  
  <pre><code> int inquiryID = Convert.ToInt32(Session["session_view_id"]);
     MySqlConnection conn = null;
      try
      {          MySqlCommand cmd = new MySqlCommand("SELECT * FROM crm_support_inquiry inner join  crm_inquiry_perticipant on crm_support_inquiry.inquiry_id=?id inner join crm_mailer_types on crm_support_inquiry.mailer_id=crm_mailer_types.mailer_id limit 4", connect);
  
          using (MySqlDataAdapter sda = new MySqlDataAdapter())
          {
              cmd.Parameters.AddWithValue("?id", inquiryID);
              cmd.Connection = connect;
              sda.SelectCommand = cmd;
              using (DataTable dt = new DataTable())
             {
  
                 sda.Fill(dt);
                  lblComp.Text = dt.Rows[0]["company"].ToString();
                  lblCname.Text = dt.Rows[0]["contact_name"].ToString();
                lblEmail.Text = dt.Rows[0]["email"].ToString();
                  GridView1.DataSource = dt;
                  GridView1.DataBind();
             }
          }
  </code></pre>