473,320 Members | 1,979 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 1336
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Guinness Mann | last post by:
When you guys talk about "dynamic SQL," to what exactly are you referring? Is dynamic SQL anything that isn't a stored procedure? Specifically, I use ASP.NET to communicate with my SQL Server...
4
by: jrefactors | last post by:
I want to distinguish between static SQL, dynamic SQL, and embedded SQL, but couldn't find too much useful resources in the web. For example, if we put SQL statements (SELECT, INSERT, UPDATE,...
3
by: JDPope | last post by:
I have a situation which I cannot get a good lead on how to resolve. One of the applications I support uses the Hibernate software to generate SQL. The app is JAVA with JDBC. In testing the users...
1
by: Nathan Bloomfield | last post by:
Does anyone know if there is any documentation which relates to Access2k + ? or can anyone help adjust the code? I am having trouble converting the DAO references. TITLE :INF: How to...
8
by: George Meng | last post by:
I got a tough question: The backgroud for this question is: I want to design an application works like a engine. After release, we can still customize a form by adding a button, and source code...
2
by: deejayquai | last post by:
Hi I'm trying to produce a report based on a dynamic crosstab. Ultimately i'd like the report to actually become a sub report within a student end of year record of achievement. The dynamic...
5
by: alingsjtu | last post by:
Hello, every body. When execute dynamic generated multiple OPENQUERY statements (which linkes to DB2) in SQLServer, I always got SQL1040N The maximum number of applications is already connected...
10
by: jflash | last post by:
Hello all, I feel dumb having to ask this question in the first place, but I just can not figure it out. I am wanting to set my site up using dynamic urls (I'm assuming that's what they're...
5
by: pittendrigh | last post by:
There must be millions of dynamically generated html pages out there now, built by on-the-fly php code (and jsp, perl cgi, asp, etc). Programatic page generation is transparently useful. But...
3
by: creative1 | last post by:
Here is how you create a complex data report that involves parent and child commands and you can update information at runtime. Its pretty straight forward to work with simple queries; however,...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.