asp.net convertion of date with mysql database - asp.net

which is better convert the date by database or c# to bind it on grid event rowDataBound ? ,
and why & when we use this or this ?

If you are converting the date to some other date, it would be more efficient to do the conversion in C# and save the database some processing.
If you are trying to format the date, use the DataFormatString on the bound field.

You don't "convert" the date on the scenario you describe; you simply format it according to your needs. The date should be a date type on the database side and on the presentation layer, whatever it may be, can be displayed according to your needs. For example in asp.net you can format your date on the GridView as follows:
<asp:boundfield datafield="SomeDate" DataFormatString="{0:MM/dd/yyyy}" />
or
<asp:boundfield datafield="SomeDate" DataFormatString="{0:dd/MM/yy}" />

Related

Show date in UTC in ASP:GridView BoundField

I'm trying to use a GridView to show a DateTime value. So far I have been able to show the data using the format string that I want by using:
<asp:GridView ID="myGrid" ... >
<Columns>
<asp:BoundField DataField="myField" DataFormatString={0:MM/dd/yyyy hhmmss} ... />
...
</Columns>
...
</asp:GridView>
The problem I'm having now is that this is always being shown as local time. My users will be expecting it to always be shown as UTC, regardless of where they are. Is there some way I can force the GridView to show what the UTC time would be, instead of the local time?
For example, I have found out that in vb.net, to convert a string to a date I can tell it to use universal time with:
Dim myDateAsString as String = "03/31/2014 19:00"
Dim myDate as DateTime = DateTime.ParseExact(myDateAsString, "MM/dd/yyyy HH:mm", System.Globalization.DateTimeFormatInfo.InvariantInfo, System.Globalization.DateTimeStyles.AssumeUniversal)
I'm looking for something similar to the .AssumeUniversal part that I can use when I set up my GridView in asp.
I hope what I'm asking is clear. If not I can try to elaborate more, if someone let's me know what isn't clear.
UPDATE:
The DataSource for my GridView is a DataSet that is filled by a SELECT query that just pulls data from my database. Since the field I'm interested in here is a DateTime field in my database, I am assuming it will still be a DateTime when it gets assigned as the DataSource. Is that correct?
It looks to me like the only code related to filling the GridView is
myGrid.DatqaSource = mySQLObject.sel_from_table(vars)
myGrid.DataBind()
Where mySQLObject.sel_from_table(vars) will return a DataSet as described above. The answers so far have suggested that I need to change the data to UTC before sending it to the GridView - Where/how should that be done? Do I have to change it in my query somehow? Or in the DataSet before I assign it as the DataSource?
The grid view is merely a display mechanism. It'll display whatever value you pass it in the format you specified. If you want to change the value itself (Utc Date) then 2 options are:
Template Item where you can call an Eval option to compute Utc Date.
Change the value of myField server side so that the grid view can bind that. e.g. in your SQL query results.
Either ways, your server side code needs to convert the date into UTC (ToUniversalTime() or DateTime.UtcNow etc.) and send it across for display.
here is how you can do using TemplateField.
in your ASPX
<asp:TemplateField HeaderText="My Fields">
<ItemTemplate>
<asp:Label runat="server" text='<%# GetUtcFormattedDate((DateTime)Eval("myField")) %>' />
</ItemTemplate>
</asp:TemplateField>
In your code behind, you can have a static method
public static string GetUtcFormattedDate(DateTime date)
{
return date.ToUniversalTime().ToString(YOUR_DATE_FORMAT_STRING);
}
You could convert the time to UTC in your query to pull the data from the database.
In your select clause just use the below.
SELECT DATEADD (HH, 'Your UTC Offset', DateYouWantSelected)

Date parameter in SQL Server stored procedure from ASP.NET

I've written an SQL Server stored procedure that, amongst other things, accepts a date parameter:
CREATE PROCEDURE data.addScore
... (some parameters),
#testDate date
AS
On an ASP.NET page I have a textbox with a Calender Extender control (from the AJAX Toolkit) that allows the user to select a date; the date is stored in the textbox in the format DD/MM/YYYY. I have some code that is supposed to execute the stored procedure with the parameters, using the date value from the textbox:
cmd.Parameters["#testDate"].Value = Convert.ToDateTime(((TextBox)li.FindControl("date")).Text);
I know I can definitely get to the textbox's value using ((TextBox)li.FindControl("date")).Text, but for the life of me I can't get it into a format that doesn't throw up one of a few errors, mainly 'Input string was not in the correct format'.
When I run the stored procedure directly on the server I usually bind the parameter like #testDate = 'YYYYMMDD'.
I would suggest you don't store anything in the textbox using regional, ambiguous formats like dd/mm/yyyy. If you use an unambiguous format like YYYYMMDD, you're not going to have any issues. And if users are picking from a calendar control or something, it should be easy to separate the date you present to them from the date you pass to the database.
<asp:Textbox id="date" runat="Server"></asp:Textbox>
<cc1:CalendarExtender ID="calwhendate" runat="server" Enabled="true" Format="YYYYMMDD"
TargetControlID="date"></cc1:CalendarExtender>
Change your Ajax Calendar Control to your desired format

GridView Column filtering

I have a GridView control set up, populated from a database through the BL, and one of the fields contains currency amounts. Right now it is displaying in the format of 0000.0000, and I need to bring it to a currency format that adds $ + dataNumber down to hundredths.
I have looked for filters, but so far I haven't seen a direct and simple answer. Could using a regular expression to filter work? :/
It is my first attempt to do so. Any answers are appreciated, but please provide site documentation for me to reference while attempting to implement your suggestions.
The GridView.BoundColumn type provides the DataFormatString property you can use to specify the display format. You can use the C format to display in currency format.
<Columns>
...
<asp:BoundColumn DataField="MyFieldName" DataFormatString="C" />
...
</Columns>

<asp:ControlParamenter /> and nonstandard (en-US) DateTime binding

Database date format: "yyyy-MM-dd"
C# default date output: "MM/dd/yyyy"
My date format: "dd/MM/yyyy"
How do I use the asp:SqlDataSource with an asp:ControlParameter connected to an asp:TextBox using my date format? Specifically, I wanted to know
if there was a way to do this without having it done in code-behind
if I'm bound to code-behind, how to do it?
I've read about the Culture class, but I'm not sure that's the right path to explore. I've also had a check on some stackoverflow posts and came accross this Binding Date of DateTime in a control, but I've never seen this structure of Text formatting before in ASP.NET.
Thanks in advance,

how to show only date?

I have a column in the data table called CurrentDate as datatype string (12/2/1983).
When I am displaying on the screen this is showing as 12/2/1983 12:00:00AM.
I am not sure why I am getting timestamp here?
Can anybody help me out?
If you are binding this from a datasource, like a SQL Data Source, you can use CONVERT in your query:
CONVERT(VARCHAR(10),nameOfColumn,101) AS Date
If you need to do this in C#, try the ToShortDateString() method:
string date = myDate.ToShortDateString();
In C# if you convert it to a DateTime object:
DateTime.ToShortDateString();
Are you using data binding to a gridview, formview, or other formatable data source? If so you can simply use the "d" format flag, like below:
<asp:BoundField DataField="MyDate" DataFormatString="{0:d}" HeaderText="My Date" />
An example of its usage within the context of setting a label would be:
MyLabel.Text = DateTime.Now.ToString("A FORMATTING STRING HERE);
Two very useful MSFT posts I use are: "Standard DateTime formatting strings" and "Custom date time formatting strings"

Resources