8 Replies - 2083 Views - Last Post: 13 September 2012 - 09:39 AM Rate Topic: -----

#1 siddu1281  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 17-August 12

how to caluclate the days between the two selected dates

Posted 06 September 2012 - 11:13 PM

hello friends,

Iam developing an leave management application. i want to find number of days a member applied the leaves. iam getting error in finding the days between the two selected dates in ajax calender tool kit.

my error is
Compiler Error Message: CS0029: Cannot implicitly convert type 'System.TimeSpan' to 'System.DateTime'

my code is

tbStartdate_CalendarExtender.StartDate = DateTime.Today;

tbEndDate_CalendarExtender.StartDate = DateTime.Today.AddDays(1);



DateTime StartDate1 = DateTime.ParseExact(tbStartdate.Text, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);

DateTime EndDate1 = DateTime.ParseExact(tbEnddate.Text, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);



DateTime leavesRemaining1 = EndDate1.Subtract(StartDate1);


Is This A Good Question/Topic? 0
  • +

Replies To: how to caluclate the days between the two selected dates

#2 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1357
  • View blog
  • Posts: 2,993
  • Joined: 05-April 11

Re: how to caluclate the days between the two selected dates

Posted 07 September 2012 - 01:02 AM

The error tells you that you are trying to assign a TimeSpan to a DateTime.

This should work
int days = (EndDate1 - StartDate1).Days;


Was This Post Helpful? 1
  • +
  • -

#3 siddu1281  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 17-August 12

Re: how to caluclate the days between the two selected dates

Posted 07 September 2012 - 01:18 AM

thank you CasiOo,
Was This Post Helpful? 0
  • +
  • -

#4 siddu1281  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 17-August 12

Re: how to caluclate the days between the two selected dates

Posted 07 September 2012 - 01:23 AM

how to update a record or value in sqlserver 2005 using asp.net.
my problem is if iam having a number 20 in my database, now iwant to subtract 5 and update that record into 15.

my DATABASE Leaverecord
Id-------LeaveRemaining
1-------------20
2-------------18
3-------------18

the code which i developed was not correct actually, so please help me in ExecuteUpdate() method. i want to know how to pass the value into that method

public partial class Applying : System.Web.UI.Page

{
    String connectionString = ConfigurationManager.ConnectionStrings["SQLDbconnection"].ToString();
    SqlConnection con;

    public Applying()
    {
        con = new SqlConnection(connectionString);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        tbStartdate_CalendarExtender.StartDate = DateTime.Today;
        tbEndDate_CalendarExtender.StartDate = DateTime.Today.AddDays(1);
    }
    public void btnOk_Click(object sender, EventArgs e)
    {
DateTime StartDate1 = DateTime.ParseExact(tbStartdate.Text, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);

DateTime EndDate1 = DateTime.ParseExact(tbEnddate.Text, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);

   int date = (EndDate1 - StartDate1).Days;

   [b]ExecuteUpdate(NoOfDays);[/b]             

    }
} 

[b]private void ExecuteUpdate() // help me in this code
    {
     con.Open();
     SqlCommand cmd1 = new SqlCommand();
     string sql_update="UPDATE LeaveRecord SET LeavesRemaining WHERE Id=' "+tbID.Text+" ' ";
     cmd1.CommandType = CommandType.Text;
     cmd1.ExecuteNonQuery();
     con.Close();
    }[/b]

Was This Post Helpful? 0
  • +
  • -

#5 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1357
  • View blog
  • Posts: 2,993
  • Joined: 05-April 11

Re: how to caluclate the days between the two selected dates

Posted 07 September 2012 - 01:34 AM

You need to tell it what the new value should be
string sql_update = "UPDATE LeaveRecord SET LeavesRemaining = LeavesRemaining - 10 WHERE Id=' "+tbID.Text+"' ";


Maybe this will work :P

Also you should take a read about SQL injection
Was This Post Helpful? 0
  • +
  • -

#6 siddu1281  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 17-August 12

Re: how to caluclate the days between the two selected dates

Posted 07 September 2012 - 01:39 AM

Is it possible to pass 'NoOfDays' instead of '10' inthe string
Was This Post Helpful? 0
  • +
  • -

#7 siddu1281  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 17-August 12

Re: how to caluclate the days between the two selected dates

Posted 07 September 2012 - 02:12 AM

actually i want to pass the date variable into that method how it can be done?
Was This Post Helpful? 0
  • +
  • -

#8 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: how to caluclate the days between the two selected dates

Posted 12 September 2012 - 07:27 AM

Change the method to accept an integer parameter.

As for giving the variable to the SQL query, take a look at doing parameterized queries.
Was This Post Helpful? 1
  • +
  • -

#9 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1181
  • View blog
  • Posts: 1,673
  • Joined: 24-August 11

Re: how to caluclate the days between the two selected dates

Posted 13 September 2012 - 09:39 AM

Tutorial on parameterized queries.

EDIT: What do you want to do with the date? Aren't you looking to update the LeavesRemaining? CasiOo's update statement should work, except you'll want to do this:

private void ExecuteUpdate(int noOfDays) // help me in this code
{
    con.Open();
    SqlCommand cmd1 = new SqlCommand();
    string sql_update = "UPDATE LeaveRecord SET LeavesRemaining = (LeavesRemaining - @noOfDays) WHERE Id = @tbID";
    
    // This is how to register your parameters
    cmd1.Parameters.AddWithValue("@noOfDays", noOfDays);
    cmd1.Parameters.AddWithValue("@tbId", tbID.Text);

    cmd1.CommandType = CommandType.Text;
    cmd1.ExecuteNonQuery();
    con.Close();
}



Curtis Rutland's tutorial does an excellent job of explaining SQL parameters, and it also contains examples of how to restructure this entire method to automate disposal of your connection, command, etc (the using that you'll see in his examples).

This post has been edited by h4nnib4l: 13 September 2012 - 09:53 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1