how to retrieve date and time separately from the currently selected value in datetimepicker? - datetimepicker

i want to know the way to extract date and time separately from the currently selected value in datetime picker control??

if you just want them for display purposes, you can to the following:
DateTimePicker.Value.ToShortDateString() - will give you a date string
DateTimePicker.Value.ToShortTimeSttring() - will give you time string

DateTimePicker.Value returns a DateTime, so you can use the Date and TimeOfDay properties of DateTime.
Important Note:
If you're saving the value to a database, put the date value in a data parameter, don't concatenate strings to build SQL queries; that'll leave you open to the SQL injection attack. Here's a brief example (not tested code, just to give you an idea):
DateTime theDate = dtPicker.Value.Date;
IDbCommand command = GetDbCommand("insert into table_name (name, thedate) values (#name, #thedate)");
command.Parameters.Add(command.CreateParameter("#name", theName));
command.Parameters.Add(command.CreateParameter("#thedate", theDate));
command.ExecuteNonQuery();
More info on avoiding SQL injection attacks (and solving your "invalid month") problem can be found here:
http://blogs.msdn.com/raulga/archive/2007/01/04/dynamic-sql-sql-injection.aspx

DateTimePicker.Value
Worked for me. It shows the date and time.

Related

linq query convert text to date to compare with another date vb.net

I have a database field, Field260, that stores date representations as text. For instance a value for 3/29/2018 would be stored as a string "03/29/2018"
Dim Db = New DataClasses1DataContext
Return From cf In Db.FileDatas
Where cf.Field260 <= System.DateTime.Today
Returns error
System.Data.SqlClient.SqlException: 'Explicit conversion from data type text to datetime is not allowed.'
When I tried to Parse the date
Where DateTime.Parse(cf.Field260) <= System.DateTime.Today
I receive
System.NotSupportedException: 'Method 'System.DateTime Parse(System.String)' has no supported translation to SQL.'
I'm stumped.
I encountered a similar issue - have an app that I used to have a DatePicker for a field, and my database field defined as DateTime, but the users absolutely wanted to copy/paste whatever garbage they wanted into the field. Problem is, I'm allowing them to search for records by that "Date" field.
As a workaround, I do some basic validations on the garbage they enter to attempt to only allow dates (IsDate function captures most errors, but still allows them to enter years like 208 instead of 2018, so I also check that Year(DateValue(txtDate.Text))>2000, which works for me because it's an application-received date, so should never be older than this year). Then I store it as varchar in the database.
When searching for the records, you have two options - either grab all records, then perform a date conversion on the list returned to pare down the results, or hope and pray that your faulty LINQ WHERE clause will get everything you want.
Figured it out cuz I'm a genius.
Return From cf In Db.FileDatas
Where cf.Field260.ToString() <= System.DateTime.Today.ToString("MM/dd/yyyy")
Somehow comparing string to string works, who knew!

"Input value not long enough for date format" When updating with TableAdapter

My queries are managed by a DataAdapter, so I have little to no control over it's generation.The problem query goes this way:
UPDATE MYSCHEMA.MYTABLE
SET MYDATETIME = :myDateTime
WHERE (MYKEY = :myKey)
The data access layer is automatically generated. I can't change how it works...
The type of MYDATETIMEin the database side is Timestamp.
The type of the parameter myDateTime of the DataAdapter query and the field from the dataset is DateTime.
Not sure if it should matter. Also, it seems that on the Oracle side, the hour is 24 hour but the hour managed by the app has a 12 hour base. But Why would it matter? When calling the update method, I pass Date.Now. How more legit could it be?? But it still is rejected with:
ORA-01840: Input value not long enough for date format
Any ideas?
As per comment chat, it seems that a string conversion is occurring because the parameter database type is set to varchar
Set the ProviderType of the parameter to be something more relevant to the type Oracle is expecting - it'll either be Date, DateTime, Timestamp, OracleDateTime or some variation on this theme (the names change according to whether you use .net oracle client or oracle's one) , hopefully...

converting date/time format in SQL

My question is similar to some other questions here in Stackoverflow, but none of them worked for me. I have date and time column in the format 1991-04-01 00:00:00.000, and I want to convert it to this format: 19910401
I have tried this code below:
Update [TestFamily].dbo.List
set dbo.List.DtBirth = convert(
varchar(10), cast(dbo.List.DtBirth as
datetime ),112)
after executing it says 320104 rows affected , but actually nothing has changed , the date format remains the same.
Every suggestion will be appreciated.
DateTime data in SQL Server doesn't have a format. They are just values that are only changed to a certain format when you display them. So it is taken into account when you view the results of a query in SSMS, show them in an app, report, etc.
Displaying dates is almost always the job of the display layer, not the database. I would strongly suggest making this change at that level (report, app, etc.) rather than at the SQL level. Otherwise, you'll have issues with sorting, filtering, etc.
I think your problem is that you are trying to use an update. SQL updates change values, not data types. You are trying to create a different datatype, a char or varchar.
The field dbo.List.DtBirth is presumably a datetime, and your update does not change that. You could add another field, perhaps called DTBirthAsVarChar varchar(8), then update that based on the date field.
Update [TestFamily].dbo.List
set dbo.List.DTBirthAsVarChar = convert(
varchar(10), cast(dbo.List.DtBirth as
datetime ),112)
You also don't need both a convert and a cast in the same statement, they basically do the same thing.

Is it possible to add just Date in a datetime column and not get appended 00:00.000

MY Table consists of a datetime column and I wanted to know if its possible to add just Date in the column without getting appended zeros in Time part. Currently I used
CONVERT(date, getdate()) and tried to insert , but it comes in as
2013-01-20 00:00:00.000 in the column.
Pardon me if this is a very basic question
Yes, it is. Use the DATE data type instead of DATETIME.
However do not concern yourself with whether the 0s are there are not - that's not how SQL Server stores it internally, it's just how Management Studio is presenting it to you.
If you are using SQL Server you could use the Date type rather than DateTime. Otherwise the zeros will be there for the time portion. Of course you can choose not to display the time portion in your application if desired.

Access SQL Date Format

How can I retrieve a record based on a Date property? I'm trying:
WHERE Meetings.[MDate] = '16/12/2011'
which is the format I use but I get :
"Data type mismatch in criteria expression"
Problem solved: It should have been:
WHERE Meetings.[MDate] = 16/12/2011
No quotation marks.
For where clauses use
columnName = #mm/dd/yyyy#
You'll want to use the SQL date format: '#2011-12-16#'
Use the cast to DATETIME function, CDATE(), which will honour the machine's regional settings. That said, it still a good idea to use an unambiguous date format and the ISO 8601 format is a good one.
Also note that Access doesn't have a date data type: its sole temporal data type is DATETIME and, as its name suggests, always has a time element accurate to one second time granule, even if that time happens to be midnight. Therefore, it is a good idea to always include a time value to one second time granule in all DATETIME literals e.g.
WHERE Meetings.MDate = CDATE('2011-12-16 00:00:00');
Another advantage to the above is that the Access UI will not attempt to reformat the DATETIME literal because it is held as a string.