3

I get a DateTime in SQL Server DataBase and want to display it in Input:

<input class="form-control" type="date" name="ExpireDate" id="expiredate" runat="server" value="31/12/2016" style="height: 30px; width: 267px">

and here is my C# codes:

Medicine med = new Medicine(); 
int a = GridView1.SelectedIndex; 
med.ID = int.Parse(GridView1.DataKeys[a].Value.ToString()); 
DataProcess bal = new DataProcess(); DataTable dt = bal.getInfo(med)
expiredate.Value = Convert.ToDateTime(dt.Rows[0][5]).ToString("dd/MM/yyyy");

I put a break point just after the last line , I saw dt.Rows[0][5] got "{2017/5/31 0:00:00}" and expiredate.Value got "31/05/2017",but it just shows nothing without error in webpage. And this DateTime record is inserted into DataBase through exactly the same Input, I just don't know how to display it in the same Input when I pull it out from DataBase.

First time to ask, many thanks!

7
  • 1
    What is the string value in dt.Rows[0][5] at run-time? Commented Jan 5, 2017 at 13:06
  • 1
    Please edit your questions and put that code in it Commented Jan 5, 2017 at 13:07
  • @RyanSearle i just edit my post Commented Jan 5, 2017 at 13:17
  • Maybe you could try to change the format of your date in the input to YYYY-MM-DD. Check w3schools.com/jsref/prop_date_value.asp for more infos Commented Jan 5, 2017 at 13:21
  • When you "inspect" that input in your browser (=find the html for it), do you see your date? Commented Jan 5, 2017 at 13:34

2 Answers 2

8

The problem here is the format of html date input and asp.net DateTime doesn't match. Html date input displays mm/dd/yyyy but the format behind is yyyy-MM-dd.

Try using expiredate.Value = Convert.ToDateTime(dt.Rows[0][5]).ToString("yyyy-MM-dd");

Hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

If you need vice versa go for; Date.ToString("yyyy/MM/dd")
0

It seems that you are pushing a date inside a input field which is date type. But this won't work. Because html do not support the default value for raw date format. So you have to customize the date field using a javascript. However if ou take any help from Razor Html helper then you would be able to do this either not as you are on .NET framwork. Easily you can take help from previous answers jQuerydatepicker, here you can set a default date through javascript, which will show on your input field.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.