Take the datepicker value - c#

i have a gridview that dynamically generate datepicker , i have taken the value of datepicker using
GMDatePicker dtp = (GMDatePicker)Gridview1.Rows[rowIndex].Cells[3].FindControl("frmdate_starttime");
and i have inserted using dtp.Date,while storing in the database it only store the date and not the time. how to retrive the time to save in database along with date

You can change the Format property to Custom and the CustomFormat property to what you require, something like dd MMM yyyy hh:mm.
You should then be able to use
DateTime dt = dateTimePicker1.Value;
to get the date and time.

Split the problem into two parts: are you getting the right value out of the control but failing to save/restore it in the database correctly, or are you failing to get the right value out of the control in the first place? Once you've worked out whether it's a UI or database problem, you can ignore the other half of it.
Have you enabled the time picker in the control?
What column type are you using in the database?
If you run your application in the debugger, what value do you see before you store it in the database?
If you run a query against the database in the relevant admin tool, does that show a time?

Related

C# Datetimepicker Way to Stop Validation

I have a datetimepicker with format dd.MM.yyyy when i have, for example, date 14.02.2011, save it and then want to enter 31.12.2011 then datetimepicker validator doesnt allow me to enter 31 because of month 02 inside,so it set automatically 29 in the datetimepicker.
In this case, i have to go to change a month and then back to change a day, is there a way to switch off this validation?
You have set a Value (DateTime) for the control and are now trying to change its elements one at a time, such that an intermediate value is invalid.
Assuming you are talking about the .Net Framework class (System.Windows.Forms), before capture (re)starts, you can use the ResetText() method or set the Value or Text property to an appropriate starting value for your use case. Otherwise, instead of allowing separate editing of Value.Day, .Month and .Year, capture the full text representation of the date and update the control's Text property afterwards.

how to retrieve the date via session variables?

So here is the scenario:
Page1.aspx:
I created a read-only textbox, followed by a simple calendar.
On selecting the date, the long date appears in the textbox.
Page1.aspx.cs:
created a session and stored the date in it.
Page2.aspx:
Created a label field to house the date value.
Page2.aspx.cs:
now i want to rerieve the date value into the label.
Problem1: on selecting the date from the calendar, the page refreshes and then the data is reflected into the textbox
Problem2: can't convert and store the date values into the label.
Please help.
DataType variable=(Datatype)Session["SessionVariable"];
Retrieve like this by typecasting the session value to its appropriate type.
For Example:
Date variable=(Date)Session["date"];
I'm not sure I understand Problem1 correctly, but it seems that you would like the page to display the date in the textbox without reloading. In that case, take a look at jQuery UI's Datepicker: http://jqueryui.com/datepicker/
As to Problem2, you probably can fix that by creating a new DateTime object with the Parse method. The other way around (from DateTime to string) can be done with the ToString method.

Change value of DateTimePicker

I'm having problem to change the displayed value of my DateTimePicker object in C# Winforms
I'm trying to change the date in the DateTimePicker according to the value, but the display won't change.
dtpDisplayTo.Value = DateTime.Today;
I know this is dated, but I just had the same problem and found a solution with a bit of research. You have to set the Checked property of the DateTimePicker to True before changing the date, otherwise the control will consider the change as null and not show the new date. Really strange stuff.
DateTimePicker displays today's date instead of displaying its actual Value

Datatable and Date Column Manipulation

I am working on a database driven c# application.
I have a datatable that has a date column in it. Bind this datatable to datagridview and let the user edit the records and upon clicking save, save these changes in the database.
Date format in grid changes based on the system settings between dd/MM/yyyy and MM/dd/yyyy.
Data entry operators are not smart enough to look at this difference, so while editing the records they sometime move the records to different month.
Date Format is dd/MM/yyyy 10/10/2010
They want to change it to 12th October from 10th October. They made this change 10/12/2010 assuming the date format is MM/dd/yyyy.
There always remains this problem, so I want to fix the date format in the datatable.
What are the approaches I could use?
Use this code
var style = new DataGridViewCellStyle();
style.Format = "dd-MMM-yy";
colDate.DefaultCellStyle = style;
You don't need to convert it back and forth, it'll do all the things you want to achieve,
Are you setting the Column DataType as DateTime? I don't understand why this issue can occur. Internally, .NET should be saving this cell value as DateTime, and passing this to the database. Date formatting is not an issue.

C# TableAdapter Fills in with DateTime Field. how do i edit so only Time is shown in the table

My windows form project displays a table using the TableAdapter with the following Columns: Name, Location, Start Time, Category. The Start Time column corresponds to a DateTime field in the MS Visual Studio 2010 Dataset. It is redundant in the table to display MM/DD/YYYY HH:MM:SS AM/PM when the label above the table displays "Events for MM/DD/YYY". How can I display just the Time in the Table? The table automatically populates with the information and there is no way for me to say date.ToShortTimeString() before the info enters the table.
Set the format for the column to t.
For specific instructions, see the documentation for your grid control (or whatever you're using).
EDIT: In .Net's built-in DataGridView, you can set the column's DefaultCellStyle.Format property to "t".
For more information, see Standard Date and Time Format Strings.

Categories