Thursday, February 16, 2012

<asp:SqlDataSource> generate SQL for GridView?

Hi guys.

Im using a gridview to show some training data on my website which is populated by:

<asp:SqlDataSource ID="ARENATraining" runat="server" ConnectionString="<%$ ConnectionStrings:ARConnection %>"
SelectCommand="SELECT [EventType], [CourseLink], [EventDate], [EventTitle], [EventLocation], [EventWebsite] FROM [qry_FutureEvents] WHERE ([EventPrivate] = @.EventPrivate) ORDER BY [EventDate]">
<SelectParameters>
<asp:Parameter DefaultValue="FALSE" Name="EventPrivate" Type="Boolean" />
</SelectParameters>

So far so good..

However I want to be able to filter the data.

A) Show everything

B) Show individual EventType i.e. Seminar, Training etc etc

Is it possible to generate the Select Command via a function?

(im not using stored procedures for this part of the site, nor really want to at the moment).

Id appreciate any help, otherwise ill be forced to curse and swear and use the good ol repeater and figureing out how to page it!

Cheers guysBig Smile

Well, repeaters work the exact same way as gridviews when it comes to databinding.

In any case, the sqldatasource control does expose its selectcommand property. So really, just programmatically set the property, then call the databind method of the gridview, and you're set.

Another possibility is to generate a SQL statement that uses parameters (like I beleive you're currently doing). Then on the "Selecting" event of the sqldatasource, you can set the parameter like: e.InputParameters["paramName"] = "whatever" (so if your query ends with "where somefield LIKE @.someparam", you can use % to show everything as a wildcard, or specify a parameter.

Another option is to set the parameter declaratively, and bind it to Session or a control or something (when you create the sqldatasource, thats what the parameter part of the wizard is for), but I tend to find that a little annoying to paintain, so I suggest the above methods.

|||

Thanks very much for your help!

I didnt know you could do define SqlDataSource outwith the aspx page.

Incase anyone found this thread trying to do the same thing I found this URL with an example

http://geekswithblogs.net/azamsharp/archive/2005/07/19/47711.aspx

Thanks again Shados!

|||

Sorry guys,

Im having real problems with this still :(

Ive been trawling through google all morning and im not quite getting there.

So far ive came up with

Public Sub AddDataSource()Dim dsAs SqlDataSource =New SqlDataSource() ds.ID ="mySqlSourceControl" Page.Controls.Add(ds) ds.ConnectionString = ConfigurationManager.ConnectionStrings("ARENAConnection").ConnectionString ds.SelectCommand ="SELECT [EventType], [CourseLink], [EventDate], [EventTitle], [EventLocation], [EventWebsite] FROM [qry_FutureEvents] WHERE ([EventPrivate] = @.EventPrivate AND [EventType] = @.EventType) ORDER BY [EventDate]"Dim cpAs Parameter cp =New Parameter("@.EventPrivate", TypeCode.Boolean,"False") cp =New Parameter("@.EventType", TypeCode.String,"Training") ds.SelectParameters.Add(cp)Dim paramAs ControlParameter =New ControlParameterEnd Sub
This leaves me the error:
System.Data.SqlClient.SqlException: Must declare the variable '@.EventPrivate'.
I'm that close i can smell it... 
Have pity someone [:'(]
Cheers 
|||

Sorry for taking so long to reply to you. I just woke up, AND I needed to test this by myself before replying to you. It is actualy much simpler than what you're trying to do.

First, make your SQLDatasource normally (you know, like with the wizard, or manually, etc).

Then all what you have to do is, in the designer, double click on the datasource. This will register the Selecting event. Now, one of the parameters is "e", and it has a member, called "command". This is the readonly property that contains the SqlCommand object. Now, the sqlcommand itself is readonly...however, its methods can still be used!

so you can do this:

e.Command.Parameters.Add(newSqlParameter("@.EventPrivate",false)); //note how it will take objects, so you don't need to specify a type

and you're set. No need to do anything else.

Your issue in your case, is that onload, the grid will try to bind on the datasource, and you didn't set its parameters yet, thus it cannot find them.
If your way had worked, you would have ended up with a datasource that binds twice on every load =P Anyway, so just register the Selecting
event of the datasource: this is called -everytime- the datasource is called. And you can do all the work there, as shown above ^_^

|||

Hi Shados

I really appreciate you taking your time helping me with this, it worked perfectly Big Smile

Sorry i didnt reply sooner, I just moved house and my ISP are being lazy!

It is indeed much simpler!

Thanks again

|||Don't worrie about, most people never even reply once they found their solutions :)

No comments:

Post a Comment