• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 298
  • Last Modified:

Error Creating GridView From Within ASP.NET/C# Code

I am redirecting a web page with some variables that were entered by a user from a page called "Default.aspx", and I am receiving the attached error message when I attempt to submit the information the user has entered. (I am a bit new to ASP.NET programming, and I was wondering if someone could help me figure out what I am doing wrong syntactically with the code. The GridView being used on the page is created from within the ASP.NET/C# code, which has been attached.

Any help that could be provided would be greatly appreciated.

 
Server Error in '/' Application.
--------------------------------------------------------------------------------

Control 'ctl02' of type 'GridView' must be placed inside a form tag with runat=server. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Web.HttpException: Control 'ctl02' of type 'GridView' must be placed inside a form tag with runat=server.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace: 


[HttpException (0x80004005): Control 'ctl02' of type 'GridView' must be placed inside a form tag with runat=server.]
   System.Web.UI.Page.VerifyRenderingInServerForm(Control control) +8693299
   System.Web.UI.WebControls.GridView.Render(HtmlTextWriter writer, Boolean renderPanel) +40
   System.Web.UI.WebControls.GridView.Render(HtmlTextWriter writer) +33
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +134
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
   System.Web.UI.Page.Render(HtmlTextWriter writer) +29
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1266

 


--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.3623; ASP.NET Version:2.0.50727.3618

Open in new window

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
                
                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

0
thenthorn1010
Asked:
thenthorn1010
  • 3
  • 2
1 Solution
 
chandrasekar1Commented:
can you provide aspx code here
0
 
thenthorn1010Author Commented:
chandrasekar1,

Here is the Results.aspx code.

 
<%@ 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">
    </form>
</body>
</html>

Open in new window

0
 
Ramkisan JagtapLead DeveloperCommented:
please add form tag with runat="server" on default.aspx
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
thenthorn1010Author Commented:
ramkisan,

The default.aspx already has a form tag with the "runat" attribute set equal to "server." I receive the above error message after I click on the "submit" button from the default.aspx page. The default.aspx page runs without any issue. When the Results.aspx page attempts to load, after the submit button is clicked on the default.aspx page, I receive the above error message.
0
 
chandrasekar1Commented:
check the below updated code

ASPX

<body>
    <form id="form1" runat="server">
    <asp:GridView ID="gvfuelticket" runat="server" AutoGenerateColumns="true">
    </asp:GridView>
    </form>
</body>

Open in new window


Code behind

 //Bind the data to the datagrid
                
                //GridView g = new GridView();
                //Page.Controls.Add(g);
                //g.DataSource = ds;
                //Page.DataBind();

                gvfuelticket.DataSource = ds;
                gvfuelticket.DataBind();
                //Populate a table on the Results page

Open in new window

0
 
chandrasekar1Commented:
if the gridview displays empty rows, then use below code

    gvfuelticket.DataSource = ds.Tables[0];
                gvfuelticket.DataBind();

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now