title: how do i get the fields selected in the gridview
body: <p>So basically i want to create a booking and a booking can have multiple orders but i dont know how to store the orders that have been selected in the other pages etc. but I already know i have to do a cycle to run the gridview bue i dont know how to do that with a pager and with a search textbox.</p>
  <p>this is how it looks:
  <a href="https://i.stack.imgur.com/GnZV5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GnZV5.png" alt="enter image description here" /></a></p>
  <p>Current CS</p>
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Data.SqlClient;
  using System.Data;
  using System.Linq;
  using System.Web;
  using System.Web.Configuration;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  
  namespace Gestão_de_embarques
  {
      public partial class CriarEcomenda : System.Web.UI.Page
      {
          SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings[&quot;constring&quot;].ConnectionString);
          protected void Page_Load(object sender, EventArgs e)
          {
              con.Open();
              string user = Convert.ToString(Session[&quot;user&quot;]);
  
              username.Text = user;
  
              if (Session[&quot;user&quot;] == null || Session[&quot;login&quot;] == null)
              {
                  Response.Redirect(&quot;Login.aspx&quot;, false);
              }
  
              if (!Page.IsPostBack)
              {
                  refreshdata();
              }
              con.Close();
          }
  
          public void refreshdata()
          {
              SqlCommand cmd = new SqlCommand(&quot;select DISTINCT No_ from [Encomenda]&quot;, con);
              SqlDataAdapter sda = new SqlDataAdapter(cmd);
              DataTable dt = new DataTable();
              sda.Fill(dt);
              GridView1.DataSource = dt;
              GridView1.DataBind();
          }
  
          protected void btnsearch_Click(object sender, EventArgs e)
          {
              con.Open();
  
              if (txtSearch.Value == &quot;&quot;)
              {
                  refreshdata();
              }
              else
              {
                  try
                  {
                      SqlCommand cmd = new SqlCommand(&quot;select DISTINCT No_ from [encomenda] where No_= @No_&quot;, con);
                      cmd.Parameters.Add(&quot;@No_&quot;, SqlDbType.NVarChar).Value = txtSearch.Value;
  
                      SqlDataReader sdr = cmd.ExecuteReader();
  
                      if (sdr.Read())
                      {
                          sdr.Close();
                          SqlDataAdapter sda = new SqlDataAdapter(cmd);
  
                          DataTable dt = new DataTable();
                          sda.Fill(dt);
                          GridView1.DataSource = dt;
                          GridView1.DataBind();
  
                          con.Close();
                      }
                      else
                      {
                          SqlDataAdapter sda = new SqlDataAdapter(cmd);
                          sdr.Close();
                          DataTable dt = new DataTable();
                          sda.Fill(dt);
                          GridView1.DataSource = dt;
                          GridView1.DataBind();
                          ButtonCreate.Visible = false;
                          
                      }
                  }
  
                      
                  catch(Exception ex)
                  {
  
                  }
              }
          }
  
          protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
          {
              GridView1.PageIndex = e.NewPageIndex;
              refreshdata();
          }
      }
  }
  </code></pre>