473,788 Members | 2,919 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamic SQL cause paging issues in GridView?

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="Button 1_Click" Width="109px" />
<br />
<br />
<asp:GridView ID="GridView1"
runat="server"
AllowPaging="Tr ue"

EnableSortingAn dPagingCallback s="true"

OnPageIndexChan ging="GridView1 _PageIndexChang ing">
</asp:GridView>
&nbsp;
</div>
</form>

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

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

protected void GridView1_PageI ndexChanging(ob ject sender,
GridViewPageEve ntArgs e)
{
GridView1.PageI ndex = e.NewPageIndex;
GetData();
}

private int GetData()
{
SqlConnection conn = new
SqlConnection(C onfigurationMan ager.Connection Strings["SCMConnectionS tring1"].ConnectionStri ng);
SqlCommand cmd = conn.CreateComm and();
cmd.CommandType = CommandType.Tex t;
cmd.CommandText = this.TextBox1.T ext;
SqlDataAdapter da = new SqlDataAdapter( );
da.SelectComman d = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataS ource = ds;
GridView1.DataB ind();

return ds.Tables[0].Rows.Count;
}

Jul 4 '07 #1
1 2170
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(objec t sender, EventArgs e)
{
if (IsPostBack)
GetData();
}

-- Hans
On Jul 4, 8:35 am, Froefel <hansdeschry... @gmail.comwrote :
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="Button 1_Click" Width="109px" />
<br />
<br />
<asp:GridView ID="GridView1"
runat="server"
AllowPaging="Tr ue"

EnableSortingAn dPagingCallback s="true"

OnPageIndexChan ging="GridView1 _PageIndexChang ing">
</asp:GridView>
&nbsp;
</div>
</form>

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

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

protected void GridView1_PageI ndexChanging(ob ject sender,
GridViewPageEve ntArgs e)
{
GridView1.PageI ndex = e.NewPageIndex;
GetData();
}

private int GetData()
{
SqlConnection conn = new
SqlConnection(C onfigurationMan ager.Connection Strings["SCMConnectionS tring1"].ConnectionStri ng);
SqlCommand cmd = conn.CreateComm and();
cmd.CommandType = CommandType.Tex t;
cmd.CommandText = this.TextBox1.T ext;
SqlDataAdapter da = new SqlDataAdapter( );
da.SelectComman d = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataS ource = ds;
GridView1.DataB ind();

return ds.Tables[0].Rows.Count;
}

Jul 5 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2736
by: ck388 | last post by:
For some reason when I enable the callback feature of the gridview I still get a page refresh, that is it seems like there is a postback that occurs, not a callback which is just supposed to update not the whole page, but a portion of the page. Strangely enough the URL below http://beta.asp.net/QUICKSTARTV20/aspnet/doc/ctrlref/data/gridview.aspx (VB GridView Paging and Sorting Callbacks example)
4
3861
by: Paul Hale | last post by:
Hi, Currently using VS2005 c# Scenario: I have a gridview control displaying many items with paging enabled. Column is a checkbox column which users can check, but when the gridview is paged to another page and then back again, the value of the checkbox has been forgotten. Is there a better way of persisting the values through the pages in VS2005 other than writing tedious code behind in the OnPageIndexChanging event? Maybe using the...
2
16346
by: | last post by:
Hello, I have a GridView in my ASP.NET 2.0 application that performs the paging feature perfect when I have it bound to a data source. But now I have it bound to a dataset and the paging feature will not work. When I try to use paging I get this error: The GridView 'gvResults' fired event PageIndexChanging which wasn't handled.
4
7345
by: Bishop | last post by:
I have a number of simple select queries that a user needs to be able to execute and display the data on the screen in a gridview. My thought was that I could use a Gridview set to dymanically generate columns and bound to a SQLDataSource. Then the user can click buttons (or whatever) and it changes the SelectCommand property of the SQLDataSource and re-binds the GridView. That worked great until I enabled paging. When paging is...
8
8535
by: AG | last post by:
ASP.NET 2.0, VS 2005 I have a gridview with paging enabled, bound to an objectdatasource. Both were dropped on to the page and configured via the wizards. Basically working as expected. The gridview's databind method is apparently called when the page is loaded as I have no code calling the databind method. How can I keep the gridview from databinding automatically and control it myself?
0
2007
by: Don Miller | last post by:
Here is an example of what I believe is a bug in ASP.NET 2.0 GridView paging without postbacks (or at least not documented how to fix it). Once the GridView is displayed, clicking on any of the paging tools causes the GridView to completely disappear! This happens when either DataSets or DataTables are used (it doesn't work at all for DataReaders because they don't support paging apparently). If you remove the...
7
2331
by: =?Utf-8?B?SmVmZiBCZWVt?= | last post by:
The default paging behavior of the gridview doesn't work well with very large sets of data which means we have to implement some sort of custom paging. The examples I have seen (4guysfromrolla, etc.) suggest using an ObjectDataSource which has built-in paging functionality that, when used in conjunction with certain SQL 2005 functionality, only works with the records to be displayed on the page rather than the entire set. The problem with...
1
2621
by: =?Utf-8?B?cGVsZWdrMQ==?= | last post by:
i have a GridView which i can Page on (go to page 2,10,...) the thing is that i have a dynamic query which set the GridView from starts on page 1. the thing is that when i start to go over the pages - the dynamic query isnt saved and i cant go to the real page 4 for example of the dynamic query,rather i go to page 4 of the original basic query
1
1116
by: Cirene | last post by:
I'm having some wierd paging issues with a gridview. Here it is: http://www.gulfstreamonline.com/NonAuctions.aspx Page 2 is empty Page 3-5 have 1 item each Any ideas why?
0
9656
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10370
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10177
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9969
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8995
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7519
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5402
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4074
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.