Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / July 2007

Tip: Looking for answers? Try searching our database.

Dynamic SQL cause paging issues in GridView?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Froefel - 04 Jul 2007 07:35 GMT
I'm very new to the ADO.NET and WebForms arena, so the problem that
I'm having is probably trivial.

Using VS2005, I'm just creating a small test project that has a text
box allowing you to enter a SQL statement, a button to execute the
statement and a GridView control to display the results. The GridView
control should AllowPaging and AllowSorting. The data comes from a SQL
datasource, which I access through a connection string stored in
web.config (no problems there).

When I click the Execute Button, the SQL statement is correctly
executed and the GridView control is properly populated. The problem
arises when I want to jump to the next (or any other) page. It appears
the statement is executed but the gridview control disappears. Any
idea what's causing this?

Here is the relevant excerpt from the aspx page:
<body>
   <form id="form1" runat="server">
   <div>
                       <asp:TextBox ID="TextBox1" runat="server"
Text="SELECT * FROM
customerversion" Width="423px"></asp:TextBox>
                       <br />
                       <asp:Button ID="Button1" runat="server"
Text="Execute"
OnClick="Button1_Click" Width="109px" />
                       <br />
                       <br />
                       <asp:GridView ID="GridView1"
                                       runat="server"
                                       AllowPaging="True"

EnableSortingAndPagingCallbacks="true"

OnPageIndexChanging="GridView1_PageIndexChanging">
                       </asp:GridView>
                       &nbsp;
               </div>
   </form>

Below is the code that lives in the Code-Behind file:

       protected void Button1_Click(object sender, EventArgs e)
       {
               GetData();
       }

       protected void GridView1_PageIndexChanging(object sender,
GridViewPageEventArgs e)
       {
               GridView1.PageIndex = e.NewPageIndex;
               GetData();
       }

       private int GetData()
       {
               SqlConnection conn = new
SqlConnection(ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString);
               SqlCommand cmd = conn.CreateCommand();
               cmd.CommandType = CommandType.Text;
               cmd.CommandText = this.TextBox1.Text;
               SqlDataAdapter da = new SqlDataAdapter();
               da.SelectCommand = cmd;
               DataSet ds = new DataSet();
               da.Fill(ds);
               GridView1.DataSource = ds;
               GridView1.DataBind();

               return ds.Tables[0].Rows.Count;
       }
Froefel - 05 Jul 2007 10:01 GMT
I figured out what's happening... kind of.

Apparently I also need to call the GetData() function in the Page_Load
function as follows:
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
            GetData();
    }

-- Hans

> I'm very new to the ADO.NET and WebForms arena, so the problem that
> I'm having is probably trivial.
[quoted text clipped - 67 lines]
>                 return ds.Tables[0].Rows.Count;
>         }

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.