locked
passing gridview values RRS feed

  • Question

  • User135480177 posted

    I am trying to pass two gridview values to another page to be used in a query that returns more information. I am doing the same thing on another page using a text box rather than a gridview which works fine. How do I pass the value from the gridview column to another page and use it in a query to obtain additional data? I am not receiving any errors. The form is just blank. I can get the values being passed to display in a textbox but nothing from the query.

    Below is my gridview and the page_load code.

    My gridview

    <asp:GridView ID="gvClaims" CssClass="grid" runat="server" AutoGenerateColumns="False" Visible="false">
    <Columns>
    <asp:HyperLinkField HeaderText="ID Number (Click for detail)" DataNavigateUrlFields="id_number, reference_number" DataNavigateUrlFormatString="ClaimsDetail.aspx?ID={0}&REF={1}" DataTextField="id_number" />
    <asp:BoundField DataField="NAME_FIELD1" HeaderText="NAME" SortExpression="NAME_FIELD1" />
    <asp:BoundField DataField="REFERENCE_NUMBER" HeaderText="REFERENCE NUMBER" SortExpression="REFERENCE_NUMBER" />
    <asp:BoundField DataField="STATUS_DATE" HeaderText="STATUS DATE" SortExpression="STATUS_DATE" />
    </Columns>
    </asp:GridView>

    C# code behind

    public partial class GridDetail : System.Web.UI.Page
    {
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["myConn"].ConnectionString);

    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    {
    string ID = Request.QueryString["ID"];
    string REF = Request.QueryString["REF"];

    conn.Open();
    string Sql = @"SELECT [REFERENCE_CODE],[REFERENCE_NUMBER],[YEAR_CYCLE],[STATUS_DATE],
    ,[NAME_FIELD1],[ID_NUMBER],[CLAIM_DATE],[STREET_ADDRESS1] + [street_address2] as Address,[CITY],[STATE]
    ,[ZIP_CODE_PRE],
    FROM[misc].[dbo].[my_view] WHERE convert(varchar(9),ID_NUMBER) = '";
    SqlCommand cmd = new SqlCommand(Sql + ID + "'", conn);
    SqlDataReader rdr = cmd.ExecuteReader();
    while (rdr.Read())
    {
    txtClmDate.Text = rdr["claim_date"].ToString();
    txtRef.Text = rdr["reference_code"].ToString();
    txtRef2.Text = rdr["reference_number"].ToString();
    txtIDNum.Text = rdr["id_number"].ToString();
    txtName.Text = rdr["name_field1"].ToString();
    txtAdd.Text = rdr["Address"].ToString();
    txtCity.Text = rdr["city"].ToString();
    txtSt.Text = rdr["state"].ToString();
    txtZip.Text = rdr["zip_code_pre"].ToString();
    txtCyYr.Text = rdr["year_cycle"].ToString();
    txtStatDate.Text = rdr["STATUS_DATE"].ToString();
    }
    conn.Close();
    }

    }

    Tuesday, January 16, 2018 3:25 PM

All replies

  • User2103319870 posted

    scottdg

    <asp:GridView ID="gvClaims" CssClass="grid" runat="server" AutoGenerateColumns="False" Visible="false">

    You have set the Gridview to Visible false. so how are you selecting the HypelinkField which will redirect to ClaimsDetails.aspx

    scottdg

    DataNavigateUrlFormatString="ClaimsDetail.aspx?ID={0}&REF={1}"

    Also since you are redirecting to ClaimsDetails.aspx, you should have the code to populate records in Code behind of ClaimDetails.aspx

    Tuesday, January 16, 2018 3:45 PM
  • User135480177 posted

    Sorry if I wasn't clear.

    • There is code behind the page the grid is on that makes the grid visible when a search button is clicked.
    • The code behind above is from ClaimsDetails.aspx.cs. 
    Tuesday, January 16, 2018 3:49 PM
  • User-1716253493 posted

    Sorry if I wasn't clear.

    • There is code behind the page the grid is on that makes the grid visible when a search button is clicked.
    • The code behind above is from ClaimsDetails.aspx.cs. 

    AFAIK, you don't need set grid visibility because if no data bind to the grid, the grid is invisible.

    To show the data in the grid you need to set dr as grid datasource, then call grid.DataBind()

    Wednesday, January 17, 2018 12:52 AM
  • User-492460945 posted

    Hello scottdg,

    Can you please post code related to the button click event with which you're navigating to another page?

    Thanks,

    Rajesh.

    Wednesday, January 17, 2018 11:12 AM
  • User135480177 posted

    I was able to get this to work by adding all of the columns I want to display to the gridview but making only the ones I want visible. I am sure there is a better way and I am still interested in hearing any suggestions.

    The button I was referring to passes a two parameters and displays the results in the grid. This is working fine. As a previous poster mentioned I didn't need to set the visibility to false and then make it true on click so that has been changed. The desired behavior is then for the user to select an ID from the gridview column and pass that value to a new page for more detail. You can see the hyperlink code as part of the gridview in the original post.

    protected void btnSearch_Click(object sender, EventArgs e)
    {
    string ConnSQL = ConfigurationManager.ConnectionStrings["myConnStr"].ConnectionString;
    SqlConnection conn = new SqlConnection(ConnSQL);
    SqlCommand cmd = new SqlCommand();
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "myStoredProc";
    cmd.Parameters.Add("@Filter", System.Data.SqlDbType.Text).Value = ddlSearch.SelectedValue.Trim();
    cmd.Parameters.Add("@SearchStr", System.Data.SqlDbType.Text).Value = txtSearch.Text.Trim();
    cmd.Connection = conn;

    switch (ddlSearch.SelectedValue)
    {
    case "Select a Search Parameter":
    mpe1.Show();
    break;
    default:
    if (string.IsNullOrWhiteSpace(txtSearch.Text))
    {
    mpe1.Show();
    }
    else
    try
    {
    conn.Open();
    gvClaims.EmptyDataText = "No Records Found";
    gvClaims.DataSource = cmd.ExecuteReader();
    gvClaims.DataBind();
    //gvClaims.Visible = true;
    }
    catch (Exception ex)
    {
    throw ex;
    }
    finally
    {
    conn.Close();
    }
    break;
    }

    }

    Wednesday, January 17, 2018 2:25 PM