How to Add a Hyperlink to GridView Record in ASP.NET/C# to Call Another Query

I am attempting to autogenerate columns on a ASP.NET/C# page, called Results.aspx, and I need to have the ability for the user to click on one of the rows and be able to pull three values that are returned by the dataset that is feeding the gridview in order to call another query.( I have attached my current ASP.NET/C# code, and I am having trouble figuring out how to add an additional column within the autogenerated gridview so that the user would be able to click on an individual record to call another query, which will display an image once the user selects an individual row. I am little bit new to ASP.NET and have needed a little bit of help on building the grid and data binding it. I was wondering if someone could help me add another column in addition to the autogenerated columns that would have the three primary key values from the table.) Any help that could be provided would be greatly appreciated.

 
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OracleClient;
using System.Data.OleDb;
using System.Web.Configuration;
using AjaxControlToolkit;

namespace FuelReceiptsResearch
{
    public partial class Results : System.Web.UI.Page
    {
        String txtTtlDieselSales;
        String txtTtlSales;
        String txtTtlDieselVol;
        String txtLocationNum;
        String txtDateFrom;
        String txtDateTo;

        protected void Page_Load(object sender, EventArgs e)
        {
            //Display the results of the query from the previous page
            txtTtlDieselSales = Request.QueryString["DieselSales"];
            txtTtlSales = Request.QueryString["TotalSales"];
            txtTtlDieselVol = Request.QueryString["TotalDieselVol"];
            txtLocationNum = Request.QueryString["Location"];
            txtDateFrom = Request.QueryString["DateFrom"];
            txtDateTo = Request.QueryString["DateTo"];

            OracleConnection conn = new OracleConnection();
            conn.ConnectionString = WebConfigurationManager.AppSettings["ReceiptConnectionString"];
            conn.Open();
            DataSet ds = new DataSet("fuel_ticket");
            String strSelect = build_query();
            try
            {
                OracleDataAdapter DA = new OracleDataAdapter(strSelect, conn);
                ds.Tables.Add("fuel_ticket");
                DA.Fill(ds.Tables["fuel_ticket"]);

                //Bind the data to the datagrid

                gvfuelticket.DataSource = ds;
                gvfuelticket.DataBind();
                //gvRecordDetails.DataBind();


                //GridView g = new GridView();
                
                //Page.Controls.Add(g);
                //g.DataSource = ds;
                //Page.DataBind();
                ////Populate a table on the Results page

               





                DA.Dispose();
                DA = null;
            }//end try

            catch (Exception ex)
            {
                throw ex;
            }//end catch

            finally
            {
                conn.Close();
                conn.Dispose();
                conn = null;
            }//end finally



        }//End Page_Load

        private string build_query()
        {
            String temp = "";
            bool first = true;

            temp = "Select Location, Invoice, Truck, DIESEL_GAL, DIESEL_DOL, REEFER_DOL, TOTAL_DOL, DRIVER_ID, COMPANY, CARD_DESC, TXN_DATE, LOYALTY_CARD FROM FUEL_TICKET a ";
            temp += "WHERE ";

            if (txtLocationNum != "")
            {
                temp += "a.Location = '" + txtLocationNum + "' ";
                first = false;
            }//end if

            if (txtTtlSales != "")
            {
                if (first)
                {
                   temp += "a.total_dol = '" + txtTtlSales + "' ";
                   first = false;
                }//end if

                else
                {
                    temp += "AND a.total_dol = '" + txtTtlSales + "' ";
                }//end else

            }//end if

            if (txtTtlDieselSales != "")
            {
                if (first)
                {
                    temp += "a.diesel_dol = '" + txtTtlDieselSales + "' ";
                    first = false;
                }//end if

                else
                {
                    temp += "AND a.diesel_dol = '" + txtTtlDieselSales + "' ";
                }//end else

            }//end if

            if (txtTtlDieselVol != "")
            {
                if (first)
                {
                   temp += "a.diesel_gal = '" + txtTtlDieselVol + "' ";
                   first = false;
                }//end if

                else
                {
                   temp += "AND a.diesel_gal = '" + txtTtlDieselVol + "' ";
                }//end else

            }//end if

            if (txtDateFrom != "" && txtDateTo != "")
            {
                if (first)
                {
                    temp += "a.txn_date BETWEEN to_date('" + txtDateFrom + "','mm/dd/yyyy') AND to_date('" + txtDateTo + "','mm/dd/yyyy')";
                    first = false;
                }//end if

                else
                {
                    temp += "AND a.txn_date BETWEEN to_date('" + txtDateFrom + "','mm/dd/yyyy') AND to_date('" + txtDateTo + "','mm/dd/yyyy')";
                }//end else
            }//end if

            //Return the resulting query string
            return temp;
        }//end build_query


    }//End Results
}//End FuelReceiptsResearch

Open in new window


 
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Results.aspx.cs" Inherits="FuelReceiptsResearch.Results" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:GridView ID="gvfuelticket" runat="server" AutoGenerateColumns="true"></asp:GridView>
    </form>
</body>
</html>

Open in new window

thenthorn1010Asked:
Who is Participating?
 
Ramkisan JagtapConnect With a Mentor Lead DeveloperCommented:
Use following sample code :

 <asp:GridView ID="gdvListOfStudents" runat="server" AutoGenerateColumns="true" >
        <Columns>          
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:LinkButton ID="TestLinkButton" runat="server" Text="Select" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
 protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                BindGridview();
        }

        protected void BindGridview()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("AthleteUid");
            dt.Rows.Add(new object[] { 1  });
            dt.Rows.Add(new object[] {2  });
            dt.Rows.Add(new object[] { 3  });
            dt.Rows.Add(new object[] { 4 });
            dt.Rows.Add(new object[] { 5  });          
           
            gdvListOfStudents.DataSource = dt;
            gdvListOfStudents.DataBind();
        }
     
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.