Sunday, February 19, 2012

Dynamic SqlDataSource

Ok, here's my situation.

I have a dynamic page that accepts a "type" query string. This type query string is the name of a table I want to display on the page in the GridView. Creating a different SqlDataSource/Strongly typed class for every type isn't possible because I need to make this flexible for future updates. So therefor I basically need to be able to Sort, Page, Edit and Delete with the Grid View without knowing the table name at compile-time.

Any help is appreciated,

Thanks!

Displaying the table contents is relatively straightforward. In your code-behind, you just create a SelectCommand of SELECT * FROM " + Request.QueryString["table"]. Of course, you would have to verify that the contents of the querystring is a valid value. The bigger problem is with the update and delete commands. You could create stored procedures for all tables, then fetch the names of the appropriate ones from a database depending on the value of "table" in the querystring. But that isn't particularly flexible. An alternative is toprogrammatically examine the selected table's schema, and dynamically construct update and delete commands based on the column names.|||Thanks for your response. I actually found a way to dynamically get the data itself to enable paging/sorting. I did this just by creating a stored procedure called GetTable with a @.table parameter. So this works well. As for updating and deleting I'll look into that, thanks.|||At least you are using Stored Procedures. Kewl!Big Smile

No comments:

Post a Comment