Search:
Namespaces
Discussions
.NET v1.1
Feedback
Function Help
Messages
Related Types
This message was discovered on
microsoft.public.dotnet.languages.csharp
.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...
jez123456 (VIP)
Hi, I have a vacation database with a calendar table to hold all the public
holidays and weekends.
I need a c# function which I pass in the vacation start and end dates and I
require it to return the duration. Here is my code but I can’t see what’s
wrong.
public int Duration(
DateTime
Start,
DateTime
End)
{
string sqlCmd = "SELECT COUNT(dtmDate) FROM tblCalendar WHERE (blnWeekday =
1) AND (blnHoliday = 0) AND (dtmDate BETWEEN '" + Start.ToString("dd MMMM
yyyy") + "' AND '" + End.ToString("dd MMMM yyyy") + "')";
SqlCommand
myCommand = new
SqlCommand
(sqlCmd,this.sqlConnection1);
this.sqlConnection1.Open();
int ans = (int) myCommand.ExecuteScalar();
this.sqlConnection1.Close();
}
How do I call this function to return the duration?
Is it something like:-
private void CalEnd_SelectionChanged(object sender, System.
EventArgs
e)
{
txtDuration =
Duration(
DateTime
.Parse(this.txtStart.Text),
DateTime
.Parse(this.txtEnd.Text));
}
Reply to this message...
Nicholas Paldino [.NET/C# MVP] (VIP)
jez,
It looks like your query is incorrect. Basically, you should be using
parameterized queries and passing the dates to your query through that.
Basically, it is something like this:
// Create the command text.
string sqlCmd = "select count(dtmDate) from tblCalendar where (blnWeekday =
1) and (blnHoliday = 0) and (dtmDate between @start and @end)";
// Create the command.
SqlCommand
myCommand = new
SqlCommand
(sqlCmd, this.sqlConnection1);
// Set the parameters. Note that there are two operations being performed
here.
// The parameter is being added, and the value is being set on the return
value.
myCommand.Parameters.Add("@start",
SqlDbType
.DateTime).Value = start;
myCommand.Parameters.Add("@end",
SqlDbType
.DateTime).Value = end;
// Execute the command normally...
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
-
Click here to reveal e-mail address
"jez123456" <
Click here to reveal e-mail address
> wrote in message
news:
Click here to reveal e-mail address
...
[Original message clipped]
Reply to this message...
Morten Wennevik
Hi jez123456,
Assuming your query returns the number of days (workdays?):
you need to return ans in your Duration method
public int Duration(
DateTime
Start,
DateTime
End)
{
string sqlCmd = "SELECT COUNT(dtmDate) FROM tblCalendar WHERE (blnWeekday =
1) AND (blnHoliday = 0) AND (dtmDate BETWEEN '" + Start.ToString("dd MMMM
yyyy") + "' AND '" + End.ToString("dd MMMM yyyy") + "')";
SqlCommand
myCommand = new
SqlCommand
(sqlCmd,this.sqlConnection1);
this.sqlConnection1.Open();
int ans = (int) myCommand.ExecuteScalar();
this.sqlConnection1.Close();
return ans;
}
Assuming txtDuration is a
TextBox
, add ToString() to get the resulting number as a text string.
private void CalEnd_SelectionChanged(object sender, System.
EventArgs
e)
{
txtDuration.Text =
Duration(
DateTime
.Parse(this.txtStart.Text),
DateTime
.Parse(this.txtEnd.Text)).ToString();
}
If you only need Duration to get the string value of the result you can simplify it:
public string Duration(string startString, string endString)
{
DateTime start =
DateTime
.Parse(startString);
DateTime end =
DateTime
.Parse(endString);
// query code
return ans.ToString();
}
Then in the event do
txtDuration.Text = Duration(txtStart.Text, txtEnd.Text);
--
Happy Coding!
Morten Wennevik [C# MVP]
Reply to this message...
jez123456 (VIP)
Thanks Morten, that works ok when all code is in the same module behind a
webform, but I really need to be able store the Duration method in a separte
class and call it from the webform.
The line
txtDuration.Text =
> Duration(
DateTime
.Parse(this.txtStart.Text),
DateTime
.Parse(this.txtEnd.Text)).ToString();
gives the error: The name 'Duration' does not exist in the class or namespace
and the namespaces are the same.
Does the Duration method need to need to be delcared in the calling code?
"Morten Wennevik" wrote:
[Original message clipped]
Reply to this message...
Morten Wennevik
On Thu, 9 Sep 2004 02:55:04 -0700, jez123456 <
Click here to reveal e-mail address
> wrote:
[Original message clipped]
No, but the calling code needs to know where the Duration method is located.
If Duration is in another class, you need to use it on an object of that class.
Something like
MyDurationClass m = new MyDurationClass()
textBox1.Text = m.Duration(...);
Also, the Duration method might benefit from being static, in which case you would do
textBox1.Text = MyDurationClass.Duration(...)
--
Happy Coding!
Morten Wennevik [C# MVP]
Reply to this message...
System.Data.SqlClient.SqlCommand
System.Data.SqlDbType
System.DateTime
System.EventArgs
System.Web.UI.MobileControls.TextBox
System.Web.UI.WebControls.TextBox
System.Windows.Forms.TextBox
Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us
-
Terms of Use
-
Privacy Policy
-
www.dotnet247.com