Expected query name after EXECUTE

Posted on 2006-05-19
Last Modified: 2008-01-09
Hi I am new to ASP.NET and I am trying to manage a DataGrid from an Excel file. The name of the Excel file data range is PressReleases.

I always get a "System.Data.OleDb.OleDbException: Expected query name after EXECUTE" at:

Line 48:             adapter.Fill(pr);

I have also tried adapter.Fill(pr, "PressReleases");

but nothing change...

below is my code:

<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="iso-8859-1" Debug="true"%>
<%@ import Namespace="System" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<script runat="server">
      const int RECORDS_PER_PAGE = 5;
      int totalRecords;
private void Page_Load(Object sender, System.EventArgs e) {

            // get the number of records found in the database
            totalRecords = GetProductsCount();
            // calculate and save the number of pages to the Pages hidden field
            Pages.Value = Math.Ceiling((double)totalRecords/RECORDS_PER_PAGE).ToString();
            if (!Page.IsPostBack)
                  // Bind records to repeater


      void BindData()
            int pageNumber = int.Parse(PageNumber.Value);
            int totalPages = int.Parse(Pages.Value);

            OleDbConnection connection;
            string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Server.MapPath("/Test3/PressReleases.xls")+ ";Extended Properties=Excel 8.0;";

      connection = new OleDbConnection(strConn);

      String query = "select * from PressReleases";

  OleDbCommand command = new OleDbCommand("SelectByPage", connection);

            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@PageNumber", pageNumber);
            command.Parameters.Add("@PageSize", RECORDS_PER_PAGE);
            command.CommandText = query;
            DataSet pr = new DataSet();
            OleDbDataAdapter adapter = new OleDbDataAdapter(query ,connection);
            adapter.SelectCommand = command;
            adapter.Fill(pr, "Name");
            // Bind Data to the repeater
            PressReleasesRepeater.DataSource = pr;

            // Display the page links
            PagesDisplay.Text = "";
            for (int i=1; i<=totalPages; i++)
                  if (pageNumber != i)
                        PagesDisplay.Text += "<a href=\"javascript:ChangePage("+i+")\">"+i+"</a>&nbsp;&nbsp;";
                        PagesDisplay.Text += "[" + i + "]&nbsp;&nbsp;";
            // enable/disable the links to navigate through the pages
            FirstPage.Enabled = (pageNumber != 1);
            PrevPage.Enabled = (pageNumber != 1);
            NextPage.Enabled = (pageNumber != totalPages);
            LastPage.Enabled = (pageNumber != totalPages);
            Info.Text = totalRecords + " records are found and divided into " + Pages.Value + " pages<br><br>";
      // return the number of total records in database
      int GetProductsCount()
      String strFile = "/Test3/PressReleases.xls";
      string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Server.MapPath("/Test3/PressReleases.xls")+ ";Extended Properties=Excel 8.0;";
      OleDbConnection connection = new OleDbConnection(strConn);
      //OleDbDataAdapter command=new OleDbDataAdapter("select count(*) from PressReleases", connection);

    OleDbCommand command = new OleDbCommand("select count(*) from PressReleases", connection);

            //object result = command.ExecuteScalar();
            //int count = Convert.ToInt32(result);

            int count = Convert.ToInt32(command.ExecuteScalar());

            return count;
      // execute when user clicks on the next/prev/first/last button
      void Page_Changed(object sender, CommandEventArgs e)
            switch (e.CommandName)
                  case "3":
                        PageNumber.Value = "1";
                  case "PrevPage":
                        PageNumber.Value = (int.Parse(PageNumber.Value) -1).ToString();
                  case "NextPage":
                        PageNumber.Value = (int.Parse(PageNumber.Value) +1).ToString();
                  case "LastPage":
                        PageNumber.Value = Pages.Value;
            // rebind data
      // execute when user clicks on the page link
      void Page_Click(object sender, System.EventArgs e)
            // rebind data
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="javascript">
      function ChangePage(id)
            // save the page clicked
            document.all.PageNumber.value = id;
            // call the __doPostBack function to post back the form and execute the PageClick event
<body bgcolor="black">
      <form id="Topics" method="post" runat="server">
      <div style="background-color: white; width: 300">
            <!-- Hidden fields store the current page number and total pages -->
            <input type="hidden" runat="server" id="PageNumber" value="1">
            <input type="hidden" runat="server" id="Pages" value="0">
            <!-- Hidden button to handle the click event when user clicks on a pge link-->
            <asp:button ID="PageClick" OnClick="Page_Click" runat="server" Visible="false"></asp:button>
            <asp:label ID="Info" runat="server"></asp:label>
            <asp:linkbutton ID="FirstPage" runat="server" CommandName="FirstPage" OnCommand="Page_Changed">First</asp:linkbutton>&nbsp;
            <asp:linkbutton ID="PrevPage" runat="server" CommandName="PrevPage" OnCommand="Page_Changed">Prev</asp:linkbutton>&nbsp;
            <asp:label ID="PagesDisplay" runat="server"></asp:label>
            <asp:linkbutton ID="NextPage" runat="server" CommandName="NextPage" OnCommand="Page_Changed">Next</asp:linkbutton>&nbsp;
            <asp:linkbutton ID="LastPage" runat="server" CommandName="LastPage" OnCommand="Page_Changed">Last</asp:linkbutton>
            <table width="300" style="border: 1 solid gray" align="center">
                        <td bgcolor="gray" style="color: white">Product ID</td>
                        <td bgcolor="gray" style="color: white">Product Name</td>
                  <asp:repeater ID="PressReleasesRepeater" runat="server">
                              <td><%# DataBinder.Eval(Container.DataItem, "PressReleaseID") %></td>
                              <td><%# DataBinder.Eval(Container.DataItem, "PressReleaseTitle") %></td>

Any hints?
Question by:cartom
    LVL 27

    Expert Comment

    command.CommandType = CommandType.StoredProcedure; should be changed to command.CommandType = CommandType.Text;

    You are using a select statement from a string and telling the command object to look for a stored procedure


    Author Comment

    Thank you sammy,

    finally something changed!  Now it returns an error:
    "System.InvalidOperationException: Fill: SelectCommand.Connection property has not been initialized."

    Any idea?

    LVL 27

    Accepted Solution

    Thats because you didnt open the connection and tried to use it :-)
    after this line
     connection = new OleDbConnection(strConn);
    add this

    Good luck

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now