Date text field asp.net - css

Can anyone lead me in the right direction as to how to set a textbox such as this:
<td><asp:TextBox ID="txtBoxDate" runat="server" CssClass="fields"></asp:TextBox></td>
Into a masked text box that meets this style "mm/dd/yyyy"

Why not just simply convert the DateTime object to that format in server-side code?
Try this
DateTime dateTime = "your-date-time";
var asString = dateTime.ToString("mm/dd/yyyy");
asString will now have the format you've converted the DateTime object to. It will be string. So, after this step you can execute this
txtBoxDate.Text = asString;
..this might be the code you're looking for.

Related

Textbox dont allows null value in ASP .Net

i have column in database with Int data type, but textbox don't allows null. it gives error "Input string was not in a correct format".
objinsert.VehGrpID = Convert.ToInt32(txtVehGroupID.Text);
Use TryParse instead.
int GrpID = 0;
int.TryParse(txtVehGroupID.Text, out GrpID)
if(GrpID > 0)
objinsert.VehGrpID = GrpID;
You can just special-case out empty values. I am, of course, assuming here that an empty value should map to null.
objinsert.VehGrpID = string.IsNullOrWhiteSpace(txtVehGroupID.Text) ? null : (int?)Convert.ToInt32(txtVehGroupID.Text);
You'll also want to adjust your code to set the textbox's text accordingly.
Your question is also a bit unclear, because you say the textbox won't allow nulls, but you haven't shown us any code that adjusts the textbox text.
You can also use DBNull.Value Field. If a database field has missing data, you can use the DBNull.Value property.
check this link http://msdn.microsoft.com/en-us/library/system.dbnull.value%28v=vs.110%29.aspx
try this
int VehGrpID = Convert.ToInt32(txtVehGroupID.Text);
OR
int VehGrpIDtxtVehGroupID.Text = int.Parse(txtVehGroupID.Text);
The Text property of your textbox is a String type, so you have to perform the conversion in the code.

xml and condition by dateTime

XML
<CalendarFairs>
<CalendarFair>
<DateStart>2011-04-05T00:00:00</DateStart>
<DateEnd>2011-04-09T00:00:00</DateEnd>
<Title>aaaa</Title>
<IdExecutive>1</IdExecutive>
</CalendarFair>
<CalendarFair>
<DateStart>2011-04-16T00:00:00</DateStart>
<DateEnd>2011-04-19T00:00:00</DateEnd>
<Title>bbb</Title>
<IdExecutive>2</IdExecutive>
</CalendarFair>
<CalendarFairs>
Code
var elements = from element in doc.Descendants("CalendarFair")
where DateTime.Parse (element.Elements ("DateStart").ToString())==DateTime.Now
select new
{
dateStart = element.Element("DateStart").Value,
dateEnd=element.Element("DateEnd").Value,
title=element.Element("Title").Value,
idExcutive = element.Element("IdExecutive").Value ,
};
foreach (var item in elements)//send this error
{}
System.FormatException: The string was not recognized as a valid DateTime. There is a
unknown word starting at index 0.
why error?
Try to change it as follows:
var elements = from element in doc.Descendants("CalendarFair")
let start = element.Element("DateStart").Value
where DateTime.Parse (start)==DateTime.Now.Date
select new
{
dateStart = start,
dateEnd=element.Element("DateEnd").Value,
title=element.Element("Title").Value,
idExcutive = element.Element("IdExecutive").Value ,
};
EDIT: based on the XML you have posted the query above works pretty well. Try to test it with this input:
<CalendarFairs>
<CalendarFair>
<DateStart>2011-04-05T00:00:00</DateStart>
<DateEnd>2011-04-09T00:00:00</DateEnd>
<Title>aaaa</Title>
<IdExecutive>1</IdExecutive>
</CalendarFair>
<CalendarFair>
<DateStart>2011-03-20T00:00:00</DateStart>
<DateEnd>2011-04-19T00:00:00</DateEnd>
<Title>bbb</Title>
<IdExecutive>2</IdExecutive>
</CalendarFair>
</CalendarFairs>
Note that I have inserted today's start date. Actually I think the result was empty just because there weren't entries with actual date.
It sounds like one or more of your input <DateStart> strings is not in a valid DateTime format.
Can you post some sample input XML?
It may be that you need to provide the date format using ParseExact - http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx

Gridview time format

I have a BoundField that's using a Datafield linked to a datetime type variable. I want to display only the time, not the date. How do you show the time in 24-hour format or in AM/PM format, depending on a boolean in the code behind.
Thanks.
Try something like this:
<%# Eval("AmPmMode").ToString().Equals("true")) ?
String.Format("{0:hh}:{0:mm} {tt}", Eval("date")) :
String.Format("{0:HH}:{0:mm}", Eval("date")) %>
You can format your date for AM PM mode or for 24h mode. tt is the AM/PM designator.
AmPmMode is flag from you DB, should be true or false, but you could change it to 0 or 1.
See also:
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

Convert time to decimals in .net

Is there an easy way to present time (hh:mm) as a decimal value?
Example, 01:08 should become 1,13.
I have an asp:textbox masked (ajax) as time with the mask format "99:99". Next to this box I need to present the entered value as decimal.
<asp:TextBox ID="time" runat="server" /> hh:mm ([time in decimal format])
<ajaxToolkit:MaskedEditExtender runat="server" Mask="99:99" TargetControlID="time" MaskType="Time" />
You can use the TotalHours property of the TimeSpan, like this:
DateTime endHour = new DateTime(2010, 1, 2, 5, 30, 0);
double totalHours = new TimeSpan(endHour.Hour, endHour.Minute, 0).TotalHours;
First up, you might find it more appropriate to use a TimeSpan rather than a DateTime. So for your example, this:
TimeSpan t = new TimeSpan(0, 1, 8);
decimal d = t.Minutes + (t.Seconds / 60m);
Console.WriteLine(d.ToString());
produces the correct result. The m after the 60 forces that calculation to be decimal.
You can't hook this C# directly to the onblur of the textbox, as it runs server side. If you need this to run client side you will have to either use an ajax callback to evaluate it, or use javascript to cut the string up, then calculate each bit (minutes and seconds) individually, then output the result to the second textbox or label.
double decimalTime = DateTime.Now.Hour + (double)DateTime.Now.Minute/60
Edit: Better way, using GenEric's TimeSpan idea:
double hours = new TimeSpan(DateTime.Now.Hour,
DateTime.Now.Minute,
0).TotalHours;
(CDate("01:08").TimeOfDay.TotalHours).ToString("N2"))

Flex: Converting DateField text to seconds?

Is there anything wrong with following snippet of code?
var d:Date = DateField.dateToString(myDateField.text,"DD/MM/YYYY");
testTextArea.text = d.getSeconds().toString();
Error: Implicit coercion of a value of
type String to an unrelated type Date.
Here is your problem: DateField.dateToString's first parameter is supposed to be a date. It then takes that date and returns a string using the second parameter as a format string.
It looks like you're trying to convert the string to a date (the other way around) so you can get the seconds from it and put it in the text area. The DateField control has a selectedDate parameter that will give you the date you need. Then you just run this code to put it in the text area:
testTextArea.text = myDateField.selectedDate.getSeconds().toString();

Resources