473,405 Members | 2,167 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Dynamically binding causes paging issue?

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.ConnectionStrin gs["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;
}

Jul 3 '07 #1
1 2548
Froefel,
"Webforms" is asp.net. This is the C# language newsgroup. You want to post
here:
http://msdn.microsoft.com/newsgroups...&lang=en&cr=US
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder(BETA): http://www.blogmetafinder.com

"Froefel" wrote:
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>

</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.ConnectionStrin gs["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;
}

Jul 3 '07 #2

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

Similar topics

1
by: RJN | last post by:
Hi Sorry for posting again. I have a datagrid which is put inside a div tag to make it scrollable. I need to page the datagrid. The page numbers appear at the bottom of the datagrid and has...
2
by: djc | last post by:
On the page_load event I am querying a database and binding data to some text boxes, list boxes, and a repeater control. When the page loads it uses the value of one of the database fields (status)...
7
by: pmclinn | last post by:
I was wondering if it is possible to dynamically create a structure. Something like this: public sub main sql = "Select Col1, Col2 from Table a" dim al as new arraylist al =...
30
by: lgbjr | last post by:
hi All, I've decided to use Options Strict ON in one of my apps and now I'm trying to fix a late binding issue. I have 5 integer arrays: dim IA1(500), IA2(500), IA3(500), IA4(500), IA5(500) as...
6
by: Mikus Sleiners | last post by:
Is there any way to enable exception throws in VS 2005, that occur during binding operations? I am upset that i can't see exceptions that are thrown during binding operations. It's very hard to...
8
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...
2
by: ami | last post by:
In my code, I need to implement x numbers of Gridviews dynamically based on user selection. Because I am unaware of the number of gridviews, this is done after the user enters input. I need to...
2
by: gnewsgroup | last post by:
I have a GridView, in which the header of one column changes depending on the selected value of a DropDownList outside of this GridView. I did this dynamic header through protected void...
4
by: Craig Buchanan | last post by:
I dynamically add data-bound templates to a gridview in my ascx control. while this works correctly when the gridview is databound to the datatable, i'm having issues on postback. i would like...
0
by: Sobin Thomas | last post by:
Hi All, How can I bind the Gridview control to Sql Datasource control on a button click(I see majority of the articles binding datasource at page load) I need to enable the paging and sorting of...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...
0
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...

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.