views:

37

answers:

2

I have an ASP.NET project and would like to create a page where my system admins can modify database table data (insert, update, and delete rows).

First, I have a drop down that is databound based on the tables in the database:

DdlTable.DataSource = from x in dc.Mapping.GetTables()
                      orderby x.TableName.Replace("dbo.", "")
                      select new {TableName = x.TableName.Replace("dbo.", "")};
DdlTable.DataTextField = "TableName";
DdlTable.DataValueField = "TableName";
DdlTable.DataBind();
DdlTable.Items.Insert(0, "Select a Table");

Next, I would like to have a gridview (or some other data object) that is bound to a table upon selection, and have the columns, insert, update, and delete functionality built dynamically. Can this be done without coding for each specific table?

Obviously, I can create x number of gridviews and datasources, but I would like it to be built dynamically for flexibility and minimal coding.

Essentially, I want to have a simple web-based SQL manager.

A: 

Try...

Create a seperate datasource for each table (in code or mark-up, doesn't matter).

Make sure the gridview doesn't have a datasource set when the page loads.

On some event (button click, selectedindexchanged of your dropdown, etc.), set the gridview's datasource to whichever datasource is selected in the dropdown, make sure autogenerate = true, and then databind().

dave
Of course I could do this...I don't want to have to create a datasource for each table -- I want it to build dynamically. That way I don't have to modify my mark-up / code every time I modify the database, or 'code-out' 50+ datasources.
dfarney
A: 

In short - no - you change your tables, you have to change your CRUD - there's no magic bullet.

You can try something like CodeSmith. Or you can just write something yourself -- shouldn't be too difficult, should it? Query the InformationSchema view for your table's fields and you have all of the information you need to build a function in your code-behind to generate the CRUD statements.