VB.NET: dynamically change SqlDataSource SelectCommand and re-exec

M

Mike J.

I want to know if it's possible to dynamically change the query,
SelectCommand, of a SqlDataSource and then re-execute the query. Then reload
the GridView with different data.

My situation is a page needs two links above a GridView. One link to export
the current location's data (already in the GridView) and the second link to
export the data from all locations (requiring a new query and refresh of the
GridView).

Is this possible in VB.NET/ASP.NET? It already is being done using another
programing application, but we required to stop using the other one soon.

Thanks!
 
M

Mr. Arnold

Mike J. said:
I want to know if it's possible to dynamically change the query,
SelectCommand, of a SqlDataSource and then re-execute the query. Then
reload
the GridView with different data.

My situation is a page needs two links above a GridView. One link to
export
the current location's data (already in the GridView) and the second link
to
export the data from all locations (requiring a new query and refresh of
the
GridView).

Is this possible in VB.NET/ASP.NET? It already is being done using
another
programing application, but we required to stop using the other one soon.

It's just text, right? So what's to stop you from using a "Select * from
table1" or "Select fld1, fld2 from table2", using the same SQLDataSource
and running the query again.

dim strSQL as string

strSQL = "Select * from table1"

or

strSQL = "Select fld1, fld2 from table2"

or

build strSQL with dynamic data -- strSQL = "some dynamic built string
representing the Select statement with fields and From Table"



SelectCommand = strSQL

and execute the query with the data returned representing what the Select
statement in strSQL was about.

There is nothing stopping you from doing that and binding the resultset to a
control.
 
C

Cor Ligthert[MVP]

Mike,

It depends if you are creating your program mainly by code or by drag and
drop.

As you use code, then there is not any problem to fulfil what you ask, it is
even quiet easy.

(Don't forget to clear your datasources as the datatable or whatever).

Are you using drag and drop, then there are some protected areas, which have
to be overcome, and which cost mostly more trouble then that you did more in
code.

Cor
 
M

Mike J.

Cor,
I am using Visual Studio 2005, Website project, and I am trying to have as
little code on the .vb code behind pages as possible. Most of the project is
using the Design view/Source view with small things on the code-behind page.
The queries are all from SqlDataSource objects and bound to the GridView of
that page via the ASP.NET code in the Source file. The links in question are
actually link-buttons and one of them works to export the GridView to an
Excel file using code in the .vb file. But the second link-button needs to
change the query (different in the 'where' clause) and re-execute it.
Because I am new to VB.NET (previously I did some eVB programming on PDA
devices), I don't fully understand how to re-execute a query or refresh a
GridView. I have tried several websites and books and help files but so far
have not found the solution, so here I am.

If I can't change the query and re-execute it, then I will have to link to a
different page with a different SqlDataSource and query.

Thank you!
 
C

Cor Ligthert[MVP]

Mike,

As you mainly want to use the design view, you limit your possibilities.

However, by using a partial class extra to the generated designed class, you
can make it posible to do what you want.

You need then make the private tableAdapter.Text field accessible using a
public property.

In my idea are the possibilities of a newsgroup to little, to deliberate
this in depth.

Cor
 

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

Top