Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
<tr>
<td valign="top" align="center">Training Date</td>
<td valign="top" align="left"><b>FROM</b>&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="txtDatefrom" runat="server" Width="240px" Height="30px"></asp:TextBox>

    <asp:Image ID="imgDatefrom" runat="server" ImageUrl="~/img/cal.gif" />
     <cc1:TextBoxWatermarkExtender ID="TextBoxWatermarkExtender1" runat="server"
        TargetControlID="txtDatefrom" WatermarkText="1/1/2001">
    </cc1:TextBoxWatermarkExtender>
    <cc1:CalendarExtender ID="CalendarExtender1" runat="server" PopupButtonID="imgDatefrom" TargetControlID="txtDatefrom" PopupPosition="TopRight"  >
    </cc1:CalendarExtender>
</td>
<td valign="top" colspan="5" align="left"><b>TO</b>&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="txtDateto" runat="server" Width="300px" Height="30px"></asp:TextBox>
    <asp:Image ID="imgDateto" runat="server" ImageUrl="~/img/cal.gif" />
    <cc1:TextBoxWatermarkExtender ID="TextBoxWatermarkExtender2" runat="server"
        TargetControlID="txtDateto" WatermarkText="1/1/2001">
    </cc1:TextBoxWatermarkExtender>
    <cc1:CalendarExtender ID="CalendarExtender2" runat="server" PopupButtonID="imgDateto" TargetControlID="txtDateto" PopupPosition="TopRight" >
    </cc1:CalendarExtender>
</td>
</tr>

<tr>
<td valign="top" align="center">No of Days(in numeric)</td>
<td valign="top" colspan="6" align="left">
    <asp:TextBox ID="txtNoofdays" runat="server" Width="300px" Height="30px"></asp:TextBox>
</tr>



in .CS

C#
public void InsertData()
      {
          string fromdate = System.DateTime.Now.ToString();
          txtDatefrom.Text = Convert.ToDateTime(fromdate).ToString();
          string todate = System.DateTime.Now.ToString();
          txtDateto.Text = Convert.ToDateTime(todate).ToString();
          string noofday = 

da = new SqlDataAdapter("Insert Into Feedback_training(From_training_date,To_training_date,Noof_days)values('" + txtDatefrom.Text + "','" + txtDateto.Text + "','"+noofday+"')", con);
            ds = new DataSet();
            da.Fill(ds);

NOTE :- i have already insert the value of fromdate and todate. i wanted the no of days records and stored in database (how to diff b/w fromdate and todate) i have already one column no of days in database and datatype is int
Posted

Hi Try this..

.Days will gives you the diff in integer whole number.
C#
DateTime dtFrom = DateTime.Now.AddDays(-5);
            DateTime dtTo = DateTime.Now; 
            int NoOfDays = (dtTo - dtFrom).Days;




TotalDays will gives you result in double including the time hours
C#
DateTime dtFrom = DateTime.Now.AddDays(-5);
            DateTime dtTo = DateTime.Now.AddHours(6);
            double  NoOfDays = (dtTo - dtFrom).TotalDays;
 
Share this answer
 
v2
See Code

string noofday = (System.DateTime.Now - Convert.ToDateTime(fromdate)).TotalDays.ToString();

Note that I used the TotalDays property to get the number of days in between. This gets me the number of days putting in consideration the years with fraction days (years with 366 days). I could also use the property "Days" that would get me the value considering that all the years consist of 365 days only.
 
Share this answer
 
v3
First off, don't do that! Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

Pass the DateTime values directly, and the number of days directly as numbers - don't play around with converting the to strings, because that just introduces errors when you SQL server is not configured the same as the Web server.

To Calculate the number of days is simple:
C#
int noOfDays = (todate - fromdate).Days;
 
Share this answer
 
Comments
Karthik_Mahalingam 10-Dec-13 4:15am    
good information..
ashishmagneti 10-Dec-13 4:15am    
days does not support
OriginalGriff 10-Dec-13 4:51am    
Um. Yes it does - show the exact code you used, and the error message the compiler gives.
ashishmagneti 10-Dec-13 4:17am    
i will have to add any name space for days bocz .days not support
[no name] 10-Dec-13 4:44am    
You have already one column no of days in database and datatype is int. And you code for string.
For this
You need to change parameter format also
int noofday = Convert.ToInt32((Convert.ToDateTime(DateTime.Now.AddMonths(2)) - System.DateTime.Now).TotalDays);....
And query.....
values('" + txtDatefrom.Text + "','" + txtDateto.Text + "',"+noofday+")", con);
Firstly I would change some of your code.

1. I would change this

Quote:
string fromdate = System.DateTime.Now.ToString();
txtDatefrom.Text = Convert.ToDateTime(fromdate).ToString();
string todate = System.DateTime.Now.ToString();
txtDateto.Text = Convert.ToDateTime(todate).ToString();


to

C#
 DateTime ToDate = DateTime.Now;
 DateTime FromDate = Convert.Todate("21/10/2013");

 txtDateFrom.Text = FromDate.ToShortString();
 txtDateTo.Text = ToDate.ToShortString();

TimeSpan ts = FromDate - ToDate;
int NoDays = ts.Days;



2. Secondly I would change you insert Query to use parameterised queries.

http://social.msdn.microsoft.com/Forums/en-US/ad526a4b-61af-4f99-b6f1-2c0752006541/parameterized-query-and-adonet[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900