[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2011-10-12
1
Medium Priority
?
332 Views
Last Modified: 2013-12-16
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

0
Comment
Question by:thenthorn1010
1 Comment
 
LVL 12

Accepted Solution

by:
Ramkisan Jagtap earned 2000 total points
ID: 36960171
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Loops Section Overview
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question