I am displaying the data in a gridview and I have enabled the paging and it can show 10 records. But when I click on the next Page number, then it comes up error " An unexpected error has occurred. Troubleshoot issues with Windows SharePoint Services " Could any one tell me what is missed here.

using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;

using System.Data;
using System.Data.SqlClient;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

namespace EVentHandlerWebPartGridView
    public class EventHandlerWebPartGridView : System.Web.UI.WebControls.WebParts.WebPart

        // Constructors for the web Part
        public EventHandlerWebPartGridView()


        // Member variable for the class

        private string _cnString = string.Empty;

        // control definitions
        protected Label lblError;
        protected Button btnExecuteSQL;
        protected TextBox txtSql;
        protected GridView gvResults;

        // WebPart Parameter for getting the Connection string
        WebDisplayName("Connection String"),


        public string CNString
            get { return _cnString; }
            set { _cnString = value; }

        // Override Method- place to initialize the controls
        protected override void CreateChildControls()
            //label initialization and Properties
            lblError = new Label();

            // textbox control and Propetrties
            txtSql = new TextBox();
            txtSql.Width = Unit.Pixel(400);
            txtSql.Height = Unit.Pixel(200);
            txtSql.TextMode = TextBoxMode.MultiLine;

            //button Initialization and Properties
            btnExecuteSQL = new Button();
            btnExecuteSQL.Text = "Execute SQL";
            btnExecuteSQL.ToolTip = "Click On this to see the Reults of the SQL Query";
            btnExecuteSQL.Click += new EventHandler(btnExecuteSQL_Click);

            //gridview Initialization 

            gvResults = new GridView();
            gvResults.Width = Unit.Percentage(100);
            gvResults.AlternatingRowStyle.BackColor = System.Drawing.Color.Blue;

            gvResults.AllowPaging = true;
            gvResults.PageSize = 10;

        //Button Click Event handler
        protected void btnExecuteSQL_Click(object sender, EventArgs e)
            // if the connection string is not empty
            if (_cnString.Trim() != string.Empty)
                    SqlConnection con = new SqlConnection(_cnString);
                    SqlCommand cmd = new SqlCommand(txtSql.Text, con);
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    DataSet ds = new DataSet();
                    gvResults.DataSource = ds.Tables[0].DefaultView;

                // Any Errors in the above Code
                catch (Exception ex)

                    gvResults.DataSource = null;
                    lblError.Text = ex.Message;
                    lblError.Visible = true;
            // If the Connection string is Empty
                gvResults.DataSource = null;
                lblError.Text = "Please Enter a Connection string";
                lblError.Visible = true;



        // Rendering the Data nows
        protected override void Render(HtmlTextWriter writer)
            writer.RenderBeginTag(HtmlTextWriterTag.Table); //table Starts

            writer.RenderBeginTag(HtmlTextWriterTag.Tr); // Tr Starts

            writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");// Adds an attribute tag

            writer.RenderBeginTag(HtmlTextWriterTag.Td); // Td tag starts Here
            writer.RenderEndTag(); //tr nd

            writer.AddAttribute(HtmlTextWriterAttribute.Valign, "top");
            writer.RenderBeginTag(HtmlTextWriterTag.Tr); // tr
            writer.RenderBeginTag(HtmlTextWriterTag.Td); //td
            writer.Write("Enter a SQL Statement");
            writer.RenderEndTag();// end td

            // Now Display the Text Box

            writer.RenderBeginTag(HtmlTextWriterTag.Td); // Td start Tag
            writer.RenderEndTag(); // td End Tag
            writer.RenderEndTag();//tr End

            // Now Display the Button

            //Now Display the GridView

            writer.RenderEndTag(); //Table End




By just looking at your code I see two possibilities.

After you define:

 gvResults.AllowPaging = true;
 gvResults.PageSize = 10;

you should add:

 gvResults.PageIndexChanging += new GridViewPageEventHandler(grid_PageIndexChanging);

and then you need to add event handler:

 void grid_PageIndexChanging(object sender, GridViewPageEventArgs e)
      grid.Paent geIndex = e.NewPageIndex;

Further IMO you should not override Render method because by doing so you are breaking SharePoint DOM. Here is one older (but still good) article by Andrew Connell on this issue:

It applies on Microsoft.SharePoint.WebPartPages.WebPart but even if you are using System.Web.UI.WebControls.WebParts.WebPart you are still developing webpart on SharePoint so keeping things neat is essential.

Unfortunately I don't have 2007 dev environment handy and I cannot do any testing to debug your problem.


IMO best way is to use SPGridView and ObjectDataSource like explained here:


I’ve found the ObjectDataSource easier to use than a DataSet (for example, sorting and paging require no additional code whatsoever)

Your code can be easily converted to use ObjectDataSource.

Some additional info can be found on Kit Menke's Blog:

And yes I am posting this link only because @KitMenke is SPSE moderator :)

  • Tried with by adding event handler. But this time it does not display any data after clicking on the Pageing numbers. But no error is being displayed. – Hari Gillala Sep 18 '11 at 17:32
  • I included some extra info in my answer. Is populating gridview by button some requirement because IMO there is your biggest problem. Take a look at link I provided on how to use SPGridView. – Vedran Rasol Sep 18 '11 at 19:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.