473,625 Members | 2,687 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

calling methods from within sql select statement

Hi. I have a web form with a datagrid that displays employees who are on holiday. One of the datagrid columns shows the date of their last day of holiday, but what I really require is the Start Back at work date, i.e. if their last day of holiday is a Friday, then the Start Back date should be the following Monday (ignoring any weekends and public holiday dates).

I have a table which holds the national holiday dates (tblUKHolidays) , but how do I build the datagrid to include a Start Back column. I have a c# function which calculates the Start Back date from the EndDate but I don’t know how to include this in the datagrid.

The DataColumn.Expr ession will not allow me to call the function.

Is there a way to call the function from within the SQL select string?

Here is the datagrid source code.

private DataView CreateSource(st ring SortBy) {
SqlDataAdapter daGrid = new SqlDataAdapter( String.Format(@ "SELECT qryFullName.Ful lName AS FullName, tblAbsence.dtmA bsStart AS StartDate, tblAbsence.dtmA bsEnd AS EndDate, tblAbsence.blnC ontactable AS Contactable, " +
"CASE " +
"WHEN tblAbsence.dtmA bsStart = '{0}' THEN tblAbsence.strA MPMStart " +
"WHEN tblAbsence.dtmA bsEnd = '{0}' AND tblAbsence.strA MPMEnd IS NOT NULL THEN tblAbsence.strA MPMEnd " +
"ELSE '' " +
"END AS AMPM " +
"FROM qryFullName INNER JOIN tblAbsence ON qryFullName.str LogonName = tblAbsence.strL ogonName " +
"WHERE (tblAbsence.dtm AbsStart <= '{0}') AND (tblAbsence.dtm AbsEnd >= '{0}') AND tblAbsence.blnD eleted = 0", ActiveDate), HolData.TimeOff Conn);
DataSet dsGrid = new DataSet();
daGrid.Fill(dsG rid,"Absence");
DataView vwBookings = dsGrid.Tables["Absence"].DefaultView;
vwBookings.Sort =SortBy;
return vwBookings;
}

Here is the function that I want call.

public DateTime StartBackDate(D ateTime dtEndDate)
{
DateTime MyDate;
int MyDays = 0;

MyDate = dtEndDate.AddDa ys(1);
SqlDataAdapter daUKHolidays = new SqlDataAdapter( "Select * from tblUKHolidays", HolData.TimeOff Conn);
DataSet dsUKHolidays = new DataSet();
daUKHolidays.Fi ll(dsUKHolidays , "tblUKHolidays" );
DataView dvUKHolidays = new DataView(dsUKHo lidays.Tables["tblUKHolid ays"]);
dvUKHolidays.So rt = "dtmDate";
while (MyDays < 1)
{
if ((MyDate.DayOfW eek.ToString() == "Monday")|(MyDa te.DayOfWeek.To String() == "Tuesday")|(MyD ate.DayOfWeek.T oString() == "Wednesday")|(M yDate.DayOfWeek .ToString() == "Thursday")|(My Date.DayOfWeek. ToString() == "Friday"))
{
int rowIndex = dvUKHolidays.Fi nd(MyDate);
if (rowIndex == -1)
{
MyDays = MyDays + 1;
}
}
if (MyDays >= 1)
break;
MyDate = MyDate.AddDays( 1);;
}
return MyDate;
}

Nov 16 '05 #1
2 5437
Hi
What you but inside an SQL query string is considered by c# compiler as
string so it is taken as is. It is your DBMS that try to analyze this
string to be executed against your database. So in other words, whatever
you but inside the SOL query string is bypassed by the C# compiler and
therefore, you can not call a function within such string.
What you can do however, is to have a calculated column in your datagrid
that is not bound to your datatable . write a function that do the
calculation and use it to fill this column .
hope that helps
Mohamed Mahfouz
MEA Developer Support Center
ITworx on behalf of Microsoft EMEA GTSC

Nov 16 '05 #2
Hi,

You can call a function when you bind your grid, the code below may help
you, it's from a web page with a similar situation then yours.
<asp:datagrid id=recordgrid runat="server" DataSource="<%#
GetDataGridSour ce()%>"
showfooter="Fal se" visible="true" OnEditCommand=" RecordEditComma nd"
OnDeleteCommand ="RecordDeleteC ommand" Width="100%" CellPadding="0" >
<columns>
<asp:templateco lumn ItemStyle-VerticalAlign=" Top"
ItemStyle-Width="65" ItemStyle-HorizontalAlign ="left" >
<itemtemplate >
<span ><%# RecordStatusToS tring(
((CtpRecord)Con tainer.DataItem ).Status )%></span>
</itemtemplate>
</asp:templatecol umn>
You see how I call a method in the bind expression, you could do a similar
thing.
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"jez123456" <je*******@disc ussions.microso ft.com> wrote in message
news:EA******** *************** ***********@mic rosoft.com...
Hi. I have a web form with a datagrid that displays employees who are on holiday. One of the datagrid columns shows the date of their last day of
holiday, but what I really require is the Start Back at work date, i.e. if
their last day of holiday is a Friday, then the Start Back date should be
the following Monday (ignoring any weekends and public holiday dates).
I have a table which holds the national holiday dates (tblUKHolidays) , but how do I build the datagrid to include a Start Back column. I have a c#
function which calculates the Start Back date from the EndDate but I don't
know how to include this in the datagrid.
The DataColumn.Expr ession will not allow me to call the function.

Is there a way to call the function from within the SQL select string?

Here is the datagrid source code.

private DataView CreateSource(st ring SortBy) {
SqlDataAdapter daGrid = new SqlDataAdapter( String.Format(@ "SELECT qryFullName.Ful lName AS FullName, tblAbsence.dtmA bsStart AS StartDate,
tblAbsence.dtmA bsEnd AS EndDate, tblAbsence.blnC ontactable AS Contactable, "
+ "CASE " +
"WHEN tblAbsence.dtmA bsStart = '{0}' THEN tblAbsence.strA MPMStart " +
"WHEN tblAbsence.dtmA bsEnd = '{0}' AND tblAbsence.strA MPMEnd IS NOT NULL THEN tblAbsence.strA MPMEnd " + "ELSE '' " +
"END AS AMPM " +
"FROM qryFullName INNER JOIN tblAbsence ON qryFullName.str LogonName = tblAbsence.strL ogonName " + "WHERE (tblAbsence.dtm AbsStart <= '{0}') AND (tblAbsence.dtm AbsEnd >= '{0}') AND tblAbsence.blnD eleted = 0", ActiveDate), HolData.TimeOff Conn); DataSet dsGrid = new DataSet();
daGrid.Fill(dsG rid,"Absence");
DataView vwBookings = dsGrid.Tables["Absence"].DefaultView;
vwBookings.Sort =SortBy;
return vwBookings;
}

Here is the function that I want call.

public DateTime StartBackDate(D ateTime dtEndDate)
{
DateTime MyDate;
int MyDays = 0;

MyDate = dtEndDate.AddDa ys(1);
SqlDataAdapter daUKHolidays = new SqlDataAdapter( "Select * from tblUKHolidays", HolData.TimeOff Conn); DataSet dsUKHolidays = new DataSet();
daUKHolidays.Fi ll(dsUKHolidays , "tblUKHolidays" );
DataView dvUKHolidays = new DataView(dsUKHo lidays.Tables["tblUKHolid ays"]); dvUKHolidays.So rt = "dtmDate";
while (MyDays < 1)
{
if ((MyDate.DayOfW eek.ToString() == "Monday")|(MyDa te.DayOfWeek.To String() == "Tuesday")|(MyD ate.DayOfWeek.T oString() ==
"Wednesday")|(M yDate.DayOfWeek .ToString() ==
"Thursday")|(My Date.DayOfWeek. ToString() == "Friday")) {
int rowIndex = dvUKHolidays.Fi nd(MyDate);
if (rowIndex == -1)
{
MyDays = MyDays + 1;
}
}
if (MyDays >= 1)
break;
MyDate = MyDate.AddDays( 1);;
}
return MyDate;
}

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2391
by: M Wells | last post by:
Hi All, This seems like a tricky question to me. I have a Stored Procedure that encapsulates a number of updates to various tables within a transaction. However, at a later part of the transaction I need to be able to select records changed by an update statement made earlier within the same stored proc (and within the same transaction) and need for that select to reflect the changed values.
7
7254
by: TT (Tom Tempelaere) | last post by:
Hi there The line marked *** doesn't compile. I wonder why the designers of C# decided to disallow this. Rationale? Are there plans to change this? This feature forces me to make things public when the operations are in fact protected. This breaks a lot of patterns for me public class C public void F( C other) this.G() other.G(); // **
8
5355
by: Brett Robichaud | last post by:
I understand how code-behind can handle events for a page, but can I call a code-behind method from within a <script> tag in my ASP.Net page, or can I only call methods defined in other <script> sections? I can't seem to figure out the syntax for for calling code-behind directly. The method is within the class my page inherits from and is public, but when I try to call it from my page I get this error: CS1520: Class, struct, or...
4
1597
by: Mervin Williams | last post by:
I have several tables involved in my application, but the two in question here are the company and address tables. The company table has business_address_id and mailing_address_id columns, which are both foreign keys to the address table. So, the stored procedure to which my SelectCommand points to reads as: ALTER PROCEDURE dbo.CompanyInfoByCompanyID ( @companyid int
3
4390
by: Shiraz | last post by:
Updated to the latest version of DBD-mysql using perl -MCPAN -e "install DBD-mysql" and now the calling mysql function r2() within perl work > $SQL_Text = "select r2() from dual " ; > $sth=$dbh->prepare($SQL_Text); > $sth->execute(); > while ( ($tt) = $sth->fetchrow_array( ) ) { print $tt; } for reference here is the mysql Funtion
12
5538
by: Andrew Poulos | last post by:
With the following code I can't understand why this.num keeps incrementing each time I create a new instance of Foo. For each instance I'm expecting this.num to alert as 1 but keeps incrementing. Foo = function(type) { this.num = 0; this.type = type this.trigger(); } Foo.prototype.trigger = function() {
2
1137
by: Pieter | last post by:
Hi, How do I do this in Visual Studio .NET 2005? I want to kind of jump out of a function, but without executing the next lien of code? For instance: If I have a method MyTestMethod(strParam as String), which is called over 100 times in my application from different other methods. When an exception occurs in MyTestMethod, I would like to be able to go to the original Method that called MyTestMethod.
4
1220
by: wisaunders | last post by:
I have several different classes that contain the same four properties. I tell my application property of which class to call in the configuration file. Here are the values from my config file <class>AAAA</class> <property>1111</property>
22
1679
by: DL | last post by:
Hi, What I wanted to do is to call a function from a newly created element. But it stumbled me. Here's the line that references the newly created element and I used the alert function for debugging for now. Did I mess up all these quotes? // once again this is the key line of code, problem area, disregard
0
8251
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8182
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8352
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7178
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6115
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4085
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2614
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1800
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.