Lightswitch Time Control - c#

I have a time field that i would like to represent on a lightswitch screen. They only offer a date time picker and viewer. Does anybody know a work around for this?
I do not want the date, just time. In my database the column is defined as time(7). Just need to get that in Lightswitch now. Help please.
I tried change my database type to varchar and use a regular textbox, however Lightswitch doesn't offer me to format the textbox (e.g. : AM/PM). If I can do that, that it would be a good workaround, however I cannot do that in Lightswitch.

For the control, I'm pretty sure you will be stuck with a Date Time Picker/Viewer unless you create a custom control that will just do time. In that case the Article linked by #MichaelWashington in the answer mentioned by #MattThalman is the way to go.
But as for display, Lightswitch certainly allows you to format it to your liking. Looking at this MSDN article: How to: Format Numbers and Dates in a LightSwitch Application. Basically you would open your table in the Data Designer and then select your Date Time field. In that field's Properties, you can enter a Format Pattern.
From the MSDN article: Reference: Number and Date Formats. A Format Pattern of "t" would result in a displayed result of: 1:45 PM.
#marc_s is correct. Use the appropriate types. Storing dates and times as strings will cause you a massive amount of headaches later on. Don't do it.
I'm not sure as to the ramifications of using a Date Time type in Lightswitch and time(7) is SQL. I would probably just change your database type to datetime or datetime2. If you don't use the date part, so be it. But it will be there later on if the requirements change.

I had the same problem and came up with this solution.
On your datetime control representing your date field, you can use JQuery on the post render event to hide the part you don't want.
myapp.AddEditEvents.FinishTime_postRender = function (element, contentItem) {
// Hide the Date part of the Control by removing the first fieldset tag it comes to,
$(element).find('fieldset:first').remove();
};

Related

How to search a date field using wild cards?

I have a date search field in my ASP.Net application where the user enters the date he's looking for and the SQL query looks for the exact date provided.
I'm asked to allow the user to enter the date search using wildcards as follows:
- */12/2015 would mean get all the month of December, 2015
- */*/2015 would mean get all the year of 2015
- */*/* would mean get all the data you got
The only way I could think about to do this is to look in my C# code for '*' in the date, if it exists I will replace the query by a date range based on the position of the *.
This solution doesn't look practical to me, is there an easier way to do it in C# or SQL ?
the solution you found is the only one that makes sense while properly handling the information.
what you've been asked for is to give the users 'something' that allows them to search the dates as text and imho there are 2 possible solution:
the one you found (imho the 'right' one)
store the date as formatted string (BAD, AVOID, NIGHTMARE)
the wildcard approach would be simple to implement should you (wrongly, badly, poorly) store the date information and the format in a text field but this would prevent you from manipulating any date as a date and will put you in huge troubles at the first request that involves date handling (consolidation by month, calculate any rate in a given time span, etc...) or if you happen to have users in different countries (that expect a format different from the one stored into the text field).
implementing the search logic in the frontend the database will store the information using the correct data type and all the manipulation needed to accomplish the 'search the date as text' task are performed by the frontend that will feed to the RDBMS a set of parameters that allows it to handle the date properly.
the result would be as follows:
*/12/2015 --> between '20151201' and '20151231'
*/*/2015 --> between '20150101' and '20151231'
*/*/* --> no filter at all
if stored procedures are in use instead the result would be a couple of date parameters filled with the date values.
I would do as you already suggested and parse the search-string. Then depending on the search-string you can select your data. However you do not have to create dateranges, you can use YEAR and MONTH isntead.
Your SQL-Query could look something like this
SELECT * FROM YourTable WHERE YEAR(YourDate) = 2015 and MONTH(YourDate) = 12
In SQL you can replace * with % and use LIKE. Here is one example:
SELECT *
FROM (
SELECT cast('20150611' as date) as dateColumn
UNION
SELECT cast('20150610' as date)) as X
WHERE convert(varchar, dateColumn, 103) LIKE N'%/06/2015'
Keep in mind that this is practical and not the best solution from performance point of view.

When I convert date and time to only time, the cell becomes read only

I would like to understand the concept behind this.
I am making a database in c#. Now, I wish to have only date instead of date and time.
So, I went for the following command in sql query pane:
SELECT CONVERT(varchar, deal_start_date, 101) AS 'deal_start_date'
FROM client
The desired result comes but the data becomes read only and hence cant be edited.
Further, it does not stay permanently. I mean,
On clicking show table data again the date-time format comes.
Can any one tell me why the cells become read-only and how to keep the changes permanently through UI only??
Many thanks.
My guess on the read only part, is that since you are now converting the original value, you loose the link towards the column in the database. Just like a computed column can't be edited (how would you for example write to the column from the query that is defined as A+B as 'C'.
Inside what type of component are you showing this in your GUI? Maybe you can ahve your query remain as SELECT deal_start_date FROM client, and filter out the time part from your component?
Or, if you don't use the time in any other place in your application, change the column from datetime to date in the database.
I did not get a perfect answer but I found an alternative. I was trying with datetime datatype in MS SQL database. When I changed it to varchar(12), I got the desired result. i.e in date format.
(Thanks to insights provided by Øyvind Knobloch-Bråthen )
This is actually improper to follow as with size 12 in varchar, the time part is truncated.
(If the size of varchar is increased, the time part will be present)
But It served my purpose.
But I am still waiting for a correct answer,if any.

C#: Sorting on date field in a Winforms app

In an sqlite database I have a date field stored as a TEXT property with the following format:
dd/mm/yyyy hh:mm:ss
(e.g., the text in the field looks like: 28/04/2009 19:15:17)
I have bound a DataGridView to the database via the GUI in VS2008. So basically all the code was automatically generated for me. When I start my app it reads the database and displays all the rows in the DataGridView. I can sort on all columns except the date column. I suspect this is because the DataGridView thinks the date column is just plain text. How do I get this to work as I want it to?
UPDATE
As far as I am aware there is no DATE type in sqlite so I have to store as TEXT. Happy to be told otherwise though.
UPDATE
The user would like to see the date in dd/mm/yyyy format.
Thank you.
If you are treating the date as a string and you want it to sort correctly as a string you should change the date format to yyyy/mm/dd hh:mm:ss, that would make the date string sortable. Ideally you should be treating the data type as a date of course.
I would do one of these (in order of preference):
Change the database so that it stores the date values as a Date type, not a string. This is "the right thing to do", since you will be able to apply sorting on all levels without any problems. It is likely to be more space efficient as well, since date types are typically just a few bytes in size.
If you can't change the database design, change the format in which you store the date text into ISO-8601 format (yyyy-MM-dd HH:mm:ss).
Create a layer in between the database and the grid where you read the data into a custom object, where the date value is represented by a DateTime field. Use DateTime.TryParseExact to parse the string from the database into your custom object.
There are a number of implementations of SortableBindingList that you can find on the internet, including one from MSDN. This blog example may help. You can use known datatypes and and get correct sorting without having to worry so much about date format strings. The only requirement would be to convert the date string from the db to a datetime in your bound objecttype.

How to show only month and year of date in DataColumn of Datatable?

I am populating one DataGridView in C# windows application. I have one DataTable in which I am displaying dates which I retrieve from database. My problem is, dates that I am getting from database includes time information also. For example: Jan 2003 12:00:00 AM.
I want to show only the year and month (Jan 2003) part of that date in cells of datatable. I tried to change the culture information of current thread, but it didnt worked. Is there any other way to achieve this?
I am using MySQLas my database and there I have set datatype of this column as DATE, but I am still getting this time information. Can anyone explain this behavior of MySQL as well?
Set the Format property on your column to the Month/Year format specifier:
// Set the Format property on the "Last Prepared" column to cause
// the DateTime to be formatted as "Month, Year".
dataGridView1.Columns["Last Prepared"].DefaultCellStyle.Format = "y";
If you prefer to do this at the SQL level, you can do this in your select;
select convert(varchar(max),getdate(),107) as thedate
I'd always recommend not abstracting the data and letting the datagridview doing the cosmetics however.
Thanks everyone.
I solved my problem by changing my query. I used date_format() function of MySql which gave me exact format .
Here is the link which gives more information about this function.
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format

How to programmatically assign the date to a datetimepicker from a database in c#

i hav a datetimepicker control whose format i have set to custom as dd/mm/yyyy (with no time). i store this date in a database as varchar and not as datetime because it was giving some sort of error regarding invalid datetime format. i then use a datagrid to display the records from the database. the thing is i need to retrive the date from the datagrid (when the user selects the particular date cell in datagrid) and want to display it back on the datetimepicker control (so that the user can edit it). i am using the following code to do so:
dtDate.Value = Convert.ToDateTime(dgDetails.SelectedCells[1].FormattedValue);
the error i am getting is "String was not recognized as a valid DateTime".
where the dtDate is datetimepicker control, dgDetails is the datagrid and SelectedCells[1] is the cell containing the date.
You shouldn't have reacted to the error by changing the type in your database inappropriately - if you're trying to store dates, you should use the closest available type in the database.
The problem was almost certainly that you were querying/updating/inserting by putting the value directly in the SQL instead of using a parameterized query - the right solution is to use a parameterized query.
You've done the same thing with the data grid, by the sounds of it. Why bother parsing something when you can get at the value without going via a string representation? Try:
dtDate.Value = (DateTime) dgDetails.SelectedCells[1].Value;
Note that it almost certainly wasn't the assignment that was failing - it was the call to Convert.ToDateTime. (The stack trace should have shown you this.)
You can set date format for Date column from designer or set below property as required
Column.DefaultCellStyle.Format

Categories