Reputation: 11
I have used DATEDIFF function but since it requires yyyy/mm/dd this code is giving wrong value ,because i have stored date from date time picker in this formate day,month,dd,yyyy
private void button3_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet();
DataSet ds1 = new DataSet();
DataSet ds2 = new DataSet();
DBHandling db = new DBHandling();
db.GetDataset(" select JoiningDate from Emp where name = '" + textBox2.Text + "'", ref ds);
db.GetDataset(" select LeavingDate from AddDate where name = '" + textBox2.Text + "'", ref ds1);
string date1, date2;
date1 = ds.Tables[0].Rows[0][0].ToString();
date2 = ds1.Tables[0].Rows[0][0].ToString();
db.GetDataset("select DATEDIFF(day,2008/1/15,2008/1/12) ", ref ds2);
textBox5.Text = ds2.Tables[0].Rows[0][0].ToString();
}
so how should i do this so that i will gate right Difference between 2 dates i.e number of days between 2 dates,thanks in advance
Upvotes: 0
Views: 546
Reputation:
You can use the DateDiff utility of the Time Period Library for .NET.
Upvotes: 1
Reputation: 15754
I suggest looking up Timespan.
Get your two dates and then use the timespan to subtract one from the other. Then the timespan object can tell you how many seconds,days,hours etc... of a difference tehre is.
If the dates are in two separate formats, than convert them to the same format. You can do this in your SQL using CAST or CONVERT or using C#.
Here is a good link on formatting dates.
Also, your SQL is wide open to SQL Injection attacks. You should be using parameterized queries at the least.
Upvotes: 1