.NETGURU
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




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification