471,305 Members | 1,233 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,305 software developers and data experts.

how to use dynamic c# code from the database

hello, I have a data grid being populated from any number of stored
procedures. I need to have the totals in the footer, so I use the
ItemDataBound event to sum up and display the results.

this works fine, but since each stored procedure has different
structures (columns, datatypes...) I either have to code one data grid
for each stored procedure or do something else.

also note, i cannot use in my T-SQL the options COMPUTE, WITH ROLLUP,
or WITH CUBE. those are not available.

so I was thinking I could store the code from the ItemDataBound event
in the database and when I call the stored procedure i could call the
stored procedure's appropriate ItemDataBound event code.

my question is how? i need to store C# code in a table (that is the
easy part) but when I retrieve it, how do I make my code use it at
runtime?

Thanks!

Dec 29 '05 #1
4 2015
To compile and run C# code on the fly, the following should give you
some hints:

using( CSharpCodeProvider provider = new CSharpCodeProvider() )
{
ICodeCompiler compiler = provider.CreateCompiler();
...
string fullSource = this.MakeCode( code );
CompilerResults results =
compiler.CompileAssemblyFromSource(options, fullSource);
...
try
{
Assembly assembly = results.CompiledAssembly;
Type type = assembly.GetType( NAMESPACE_NAME + "." + CLASS_NAME
);

object obj = Activator.CreateInstance( type );
MethodInfo method = type.GetMethod(METHOD_NAME);
method.Invoke( obj, null );
}
...
}

It's copied from:
http://msdn.microsoft.com/smartclien...ationmodel.asp

But in general I don't think that is a very good approach. Populating
the datagrid generically from any datasource should not really be an
issue, right? Your core problem is how to compute the total?
Could you use some naming convention and then have a stored proc that
returns the total for every stored proc that you use to fill the
datagrid?

Remy Blaettler
http://www.collaboral.com

Dec 29 '05 #2
Antonio,

This would seem like an extreme waste. Why not just code your data grid
to check the type of the column. If the column is a number, then sum it up.
It's not like you can apply totals to bit fields, text fields, or date
fields (not without some sort of conversion, but if you need to do that, the
design of your database is corrupted).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Antonio" <bl*****@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
hello, I have a data grid being populated from any number of stored
procedures. I need to have the totals in the footer, so I use the
ItemDataBound event to sum up and display the results.

this works fine, but since each stored procedure has different
structures (columns, datatypes...) I either have to code one data grid
for each stored procedure or do something else.

also note, i cannot use in my T-SQL the options COMPUTE, WITH ROLLUP,
or WITH CUBE. those are not available.

so I was thinking I could store the code from the ItemDataBound event
in the database and when I call the stored procedure i could call the
stored procedure's appropriate ItemDataBound event code.

my question is how? i need to store C# code in a table (that is the
easy part) but when I retrieve it, how do I make my code use it at
runtime?

Thanks!

Dec 29 '05 #3
Hi,

this works fine, but since each stored procedure has different
structures (columns, datatypes...) I either have to code one data grid
for each stored procedure or do something else.

also note, i cannot use in my T-SQL the options COMPUTE, WITH ROLLUP,
or WITH CUBE. those are not available.
What if you return your total as output parameters?

so I was thinking I could store the code from the ItemDataBound event
in the database and when I call the stored procedure i could call the
stored procedure's appropriate ItemDataBound event code.


I do not think that is possible (store the code ), what you can do is store
the method name accordingly, and then using reflection assign the correct
method to the event.

Now, unless that your logic inside the SP is complex I will create another
SP (in case the output parameters can not be used) to return just the
totals, you will get two hit to the DB (which you will have if store the
method name) and probably will save the ItemDataBound handler.

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Dec 29 '05 #4
Antonio,
What I would consider in this case is to include a second select statement
in your stored procedure for each that returns a totals column. Your DataSet
will now have an extra table, and you can use this to provide the information
in each case.

Hope it helps.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Antonio" wrote:
hello, I have a data grid being populated from any number of stored
procedures. I need to have the totals in the footer, so I use the
ItemDataBound event to sum up and display the results.

this works fine, but since each stored procedure has different
structures (columns, datatypes...) I either have to code one data grid
for each stored procedure or do something else.

also note, i cannot use in my T-SQL the options COMPUTE, WITH ROLLUP,
or WITH CUBE. those are not available.

so I was thinking I could store the code from the ItemDataBound event
in the database and when I call the stored procedure i could call the
stored procedure's appropriate ItemDataBound event code.

my question is how? i need to store C# code in a table (that is the
easy part) but when I retrieve it, how do I make my code use it at
runtime?

Thanks!

Dec 29 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Guinness Mann | last post: by
1 post views Thread by Nathan Bloomfield | last post: by
8 posts views Thread by George Meng | last post: by
2 posts views Thread by deejayquai | last post: by
10 posts views Thread by jflash | last post: by
5 posts views Thread by pittendrigh | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.