SqlDataSource and GridView sorting

B

BrassicaNigra

Greetings,

I have an asp.net 2.0 application that uses a sqldatasource to populate a
gridview. The sqldatasource.selectcommand is set programatically in the page
load function (the declaration in the .aspx file does not specify a select
command).

The grid loads normally but when you try to sort on any of the columns, the
gridview goes blank (as though it contained no records).

If I move the select command into the sqldatasource declaration in the .aspx
file then it sorts normally.

Since the select command must be set programatically based on user selection
criteria I cannot use the declarative approach.

What am I missing? Do I need to use separate gridviews for each subset of
the data (the select command always returns the same columns) in order to get
sorting to work?

Please advise.

Dale
 
S

Steven Cheng [MSFT]

Hi Dale,

From your description, you're using GridView+SqlDataSource control to
display some data and have enabled sorting on the GridView. Also, you
programmtically set the SqlDataSource.SelectCommand instead of set it
statically in aspx. However, you find that if you click header link in
GridView to sort the data, the Gridview will become empty , correct?

Based on my experience, since you programmticaly set the "SelectCommand",
my first thought is that your code only set the "SelectCommand" at the
initial page request such as below:

===================
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlDataSource1.SelectCommand = "SELECT [id], [name], [price]
FROM [numtable]";
}
}
=======================

If this is the case, you should remove the "IsPosback" detection and make
sure you set the Select command in every request. e.g.

===================
protected void Page_Load(object sender, EventArgs e)
{
SqlDataSource1.SelectCommand = "SELECT [id], [name],
[price] FROM [numtable]";
}
===================

Thus, the GridView's sorting will work correctly. Please have a test and
let me know whether it works.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
B

BrassicaNigra

That worked! Your synopsis of the issue was right on target as well.
Thanks for the help.

Dale

Steven Cheng said:
Hi Dale,

From your description, you're using GridView+SqlDataSource control to
display some data and have enabled sorting on the GridView. Also, you
programmtically set the SqlDataSource.SelectCommand instead of set it
statically in aspx. However, you find that if you click header link in
GridView to sort the data, the Gridview will become empty , correct?

Based on my experience, since you programmticaly set the "SelectCommand",
my first thought is that your code only set the "SelectCommand" at the
initial page request such as below:

===================
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlDataSource1.SelectCommand = "SELECT [id], [name], [price]
FROM [numtable]";
}
}
=======================

If this is the case, you should remove the "IsPosback" detection and make
sure you set the Select command in every request. e.g.

===================
protected void Page_Load(object sender, EventArgs e)
{
SqlDataSource1.SelectCommand = "SELECT [id], [name],
[price] FROM [numtable]";
}
===================

Thus, the GridView's sorting will work correctly. Please have a test and
let me know whether it works.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.


--------------------
From: =?Utf-8?B?QnJhc3NpY2FOaWdyYQ==?= said:
Subject: SqlDataSource and GridView sorting
Date: Wed, 16 Jul 2008 13:53:02 -0700
Greetings,

I have an asp.net 2.0 application that uses a sqldatasource to populate a
gridview. The sqldatasource.selectcommand is set programatically in the page
load function (the declaration in the .aspx file does not specify a select
command).

The grid loads normally but when you try to sort on any of the columns, the
gridview goes blank (as though it contained no records).

If I move the select command into the sqldatasource declaration in the .aspx
file then it sorts normally.

Since the select command must be set programatically based on user selection
criteria I cannot use the declarative approach.

What am I missing? Do I need to use separate gridviews for each subset of
the data (the select command always returns the same columns) in order to get
sorting to work?

Please advise.

Dale
 
S

Steven Cheng [MSFT]

Hi Dale,

I'm glad that it helps you resolve the issue.

Have a nice day!

Sincerely,

Steven Cheng
Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
Subject: RE: SqlDataSource and GridView sorting
Date: Thu, 17 Jul 2008 07:01:01 -0700
That worked! Your synopsis of the issue was right on target as well.
Thanks for the help.

Dale

Steven Cheng said:
Hi Dale,

From your description, you're using GridView+SqlDataSource control to
display some data and have enabled sorting on the GridView. Also, you
programmtically set the SqlDataSource.SelectCommand instead of set it
statically in aspx. However, you find that if you click header link in
GridView to sort the data, the Gridview will become empty , correct?

Based on my experience, since you programmticaly set the "SelectCommand",
my first thought is that your code only set the "SelectCommand" at the
initial page request such as below:

===================
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlDataSource1.SelectCommand = "SELECT [id], [name], [price]
FROM [numtable]";
}
}
=======================

If this is the case, you should remove the "IsPosback" detection and make
sure you set the Select command in every request. e.g.

===================
protected void Page_Load(object sender, EventArgs e)
{
SqlDataSource1.SelectCommand = "SELECT [id], [name],
[price] FROM [numtable]";
}
===================

Thus, the GridView's sorting will work correctly. Please have a test and
let me know whether it works.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.


--------------------
From: =?Utf-8?B?QnJhc3NpY2FOaWdyYQ==?= said:
Subject: SqlDataSource and GridView sorting
Date: Wed, 16 Jul 2008 13:53:02 -0700
Greetings,

I have an asp.net 2.0 application that uses a sqldatasource to populate a
gridview. The sqldatasource.selectcommand is set programatically in
the
page
load function (the declaration in the .aspx file does not specify a select
command).

The grid loads normally but when you try to sort on any of the columns, the
gridview goes blank (as though it contained no records).

If I move the select command into the sqldatasource declaration in the .aspx
file then it sorts normally.

Since the select command must be set programatically based on user selection
criteria I cannot use the declarative approach.

What am I missing? Do I need to use separate gridviews for each subset of
the data (the select command always returns the same columns) in order
to
get
sorting to work?

Please advise.

Dale
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top