Trido
Trido

Reputation: 545

Grab date, add hours and pass to SQL

When a user clicks a Submit button, I want the code to grab the date to a Calendar control (The Ajax Control Toolkit one), convert it to DateTime and then add 17 hours so that it then places the date as selected and time as 5pm into a SQL database but I can't seem to get it right. I have a OnClick event in my code behind with the following code:

string dd = Convert.ToDateTime(DueDate);
DueDate.Text = dd.AddHours(17);

Can anyone please tell me where I am going wrong? I thought that the text in the Calendar control would be a string? It inputs it into a TextBox control. VS is telling me that I cannot implicitly convert it from a TextBox to String, and from DateTime to string. Below is what I have in my aspx file. Nothing special, very basic.

<asp:TextBox ID="DueDate" runat="server" TabIndex="6"></asp:TextBox>
<ajaxToolkit:CalendarExtender runat="server" TargetControlID="DueDate" />

Once that's done, the final part of adding it to SQL will be easy.

Upvotes: 0

Views: 188

Answers (1)

Eli Gassert
Eli Gassert

Reputation: 9763

var dd = Convert.ToDateTime(DueDate.Text);
DueDate.Text = dd.AddHours(17).ToString();

The first error (TextBox to string) is because you were passing in the control and trying to get a string out.

Then dd.AddHours wouldn't exist, because String doesn't have an AddHours method. And lastly datetime to string because DueDate.Text expects a string, and you were using a DateTime

Upvotes: 3

Related Questions