Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > how to use dynamic c# code from the database

Reply
Thread Tools

how to use dynamic c# code from the database

 
 
Antonio
Guest
Posts: n/a
 
      12-29-2005
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!

 
Reply With Quote
 
 
 
 
Remy
Guest
Posts: n/a
 
      12-29-2005
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

 
Reply With Quote
 
 
 
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      12-29-2005
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]
- http://www.velocityreviews.com/forums/(E-Mail Removed)

"Antonio" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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!
>



 
Reply With Quote
 
Ignacio Machin \( .NET/ C# MVP \)
Guest
Posts: n/a
 
      12-29-2005
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


 
Reply With Quote
 
=?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
Guest
Posts: n/a
 
      12-29-2005
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!
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Database Database Database Database scott93727@gmail.com Computer Information 0 09-27-2012 02:43 AM
DataBase DataBase DataBase DataBase scott93727@gmail.com Computer Information 0 09-26-2012 09:40 AM
Microsoft JET Database Engine error '80004005' Could not use ''; filealready in use. Mel ASP General 2 12-16-2007 04:49 PM
reusing code for repeated database use Martin Eyles ASP .Net 0 06-08-2006 03:36 PM
Availability of Dynamic Architect - Build advanced dynamic web pages without code! jonjon Javascript 0 10-29-2003 10:10 PM



Advertisments