QPR
asked on
Passing date param to update
I have a gridview (editable) 2 of the columns are date fields, when the user clicks edit and changes the date (or even don't change anything) and presses update then they get the error converting date field from string. I have update parameters of...
<asp:Parameter Name="StartDate" Type="DateTime" />
<asp:Parameter Name="EndDate" Type="DateTime" />
I can't see anywhere the gridview properties to specify that the column should be treated as a date value. The data format is dd/mm/yyyy
<asp:Parameter Name="StartDate" Type="DateTime" />
<asp:Parameter Name="EndDate" Type="DateTime" />
I can't see anywhere the gridview properties to specify that the column should be treated as a date value. The data format is dd/mm/yyyy
Is htmlencode set to false?
What type of columns are you using for these 2 parameters in the GridView? BoundField? TemplateField?...
ASKER
<asp:BoundField DataField="StartDate" HeaderText="Start Date" SortExpression="StartDate" />
<asp:BoundField DataField="EndDate" HeaderText="EndDate" SortExpression="End Date" />
<asp:BoundField DataField="EndDate" HeaderText="EndDate" SortExpression="End Date" />
In the Select stored procedure, are the StartDate and EndDate DateTime or strings (formatted)?
ASKER
Good thinking!
Yes...
select NoticeID, NoticeText, convert(varchar,StartDate, 103) as StartDate, convert(varchar,ExpiryDate ,103) as EndDate
from Notices
I needed to do this to drop the time portion returned from the DB
Yes...
select NoticeID, NoticeText, convert(varchar,StartDate,
from Notices
I needed to do this to drop the time portion returned from the DB
Ok. If you want to keep everything like it is, then do the following: On the "RowUpdating" event of the GridView, parse the text value of both dates from dd/mm/yyyy to DateTime using the following:
e.NewValues["StartDate"] = DateTime.ParseExact(GridVi ew.Rows[e. RowIndex]. Cells[Star tDateIndex ].Text, "dd/MM/yyyy", System.Globalization.Cultu reInfo.Inv ariantCult ure);
Do this for the ExpiryDate also
e.NewValues["StartDate"] = DateTime.ParseExact(GridVi
Do this for the ExpiryDate also
ASKER
Sorry, do you have a vb.net version?
Also... as you seem to be so good on dates etc (grovel grovel :-) any ideas on this one?
https://www.experts-exchange.com/questions/22892438/Calendar-control-populates-txt-bo-with-mm-dd-yyyy.html
Also... as you seem to be so good on dates etc (grovel grovel :-) any ideas on this one?
https://www.experts-exchange.com/questions/22892438/Calendar-control-populates-txt-bo-with-mm-dd-yyyy.html
Here is the VB version:
e.NewValues("StartDate") = DateTime.ParseExact(GridVi ew1.Rows(e .RowIndex) .Cells(Sta rtDateInde x).Text, "dd/MM/yyyy", System.Globalization.Cultu reInfo.Inv ariantCult ure)
Note that "StartDateIndex" is the index of the StartDate column in your GrdiView
e.NewValues("StartDate") = DateTime.ParseExact(GridVi
Note that "StartDateIndex" is the index of the StartDate column in your GrdiView
ASKER
e.RowIndex is not a member of rowupdate args
Also what do I need to replace StartDateIndex with - I am new to altering gridviews (programmatically)
Also what do I need to replace StartDateIndex with - I am new to altering gridviews (programmatically)
First, It is "RowUpdating" event, and not "RowUpdated".
Second, the "StartDateIndex" is the index of the BoundField of the StartDate column. If it is the first column, then its index is zero.
Second, the "StartDateIndex" is the index of the BoundField of the StartDate column. If it is the first column, then its index is zero.
ASKER
Thanks, now I get the error String was not recognized as a valid DateTime.
Here is what I have....
Protected Sub GridView1_RowUpdating1(ByV al sender As Object, ByVal e As System.Web.UI.WebControls. GridViewUp dateEventA rgs) Handles GridView1.RowUpdating
e.NewValues("StartDate") = DateTime.ParseExact(GridVi ew1.Rows(e .RowIndex) .Cells(2). Text, "dd/MM/yyyy", System.Globalization.Cultu reInfo.Inv ariantCult ure)
e.NewValues("EndDate") = DateTime.ParseExact(GridVi ew1.Rows(e .RowIndex) .Cells(3). Text, "dd/MM/yyyy", System.Globalization.Cultu reInfo.Inv ariantCult ure)
End Sub
Here is what I have....
Protected Sub GridView1_RowUpdating1(ByV
e.NewValues("StartDate") = DateTime.ParseExact(GridVi
e.NewValues("EndDate") = DateTime.ParseExact(GridVi
End Sub
ASKER
tried all permutations of this but don't seem to be able to get past the error
String was not recognized as a valid DateTime
String was not recognized as a valid DateTime
Are the sql columns typed for date? if so them the date format for mssql is MM/dd/yyyy
Try using the templatefield method of doing this below. I gave an example of startdate it wouldn't take much to change it for enddate. Also take those ugly convert functions out of your sql. notice how this below format out the datetime.
<asp:TemplateField HeaderText="Start Date" SortExpression="StartDate" >
<EditItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("StartDate", "{0:MM/dd/yyy}") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("StartDate", "{0:MM/dd/yyy}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Try using the templatefield method of doing this below. I gave an example of startdate it wouldn't take much to change it for enddate. Also take those ugly convert functions out of your sql. notice how this below format out the datetime.
<asp:TemplateField HeaderText="Start Date" SortExpression="StartDate"
<EditItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("StartDate", "{0:MM/dd/yyy}") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("StartDate", "{0:MM/dd/yyy}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
ASKER
You mean the sql in my SP, the convert that chops off the time portion of my datefield from the db?
The datatypes for the 2 columns are Datetime yes.
I've never had any SQL date stored as mm/dd/yyyy on any of my sql boxes!
The datatypes for the 2 columns are Datetime yes.
I've never had any SQL date stored as mm/dd/yyyy on any of my sql boxes!
ASKER
"Is htmlencode set to false?"
Not sure how this relates to dates being considered strings - can you elaborate?
Not sure how this relates to dates being considered strings - can you elaborate?
ok in the code i posted before the eval in the labels format the data to cut off the time, so you dont need to convert the format in the sql. if you use a bound column you want to set htmlencode to false because it may interpret the slashes to ⁄. What sql are you using according to .net best practices datetimes are foratted as such 2/20/2008 12:00:00 AM so if you try to parse 20/2/2008 you will get an error. sql 2000 and sql 2005 both use this format for datetime.
ASKER
"according to .net best practices datetimes are foratted as such 2/20/2008 "
only in your part of the world :)
only in your part of the world :)
ASKER
removing the convert from the SP and displaying the date in all it's glory in the gridview e.g. dd/mm/yyyy hh:mm:ss and everything works perfectly.
However I'd rather not show the hh:mm:ss
So it seems to be a simple case of displaying the date as dd/mm/yyyy in the gridview and then when passing the values back to the update statement appending the time (00:00:00) back on the end of the date and ensuring it is actually a date datatype and not a string.
?
However I'd rather not show the hh:mm:ss
So it seems to be a simple case of displaying the date as dd/mm/yyyy in the gridview and then when passing the values back to the update statement appending the time (00:00:00) back on the end of the date and ensuring it is actually a date datatype and not a string.
?
Just for my reference will this not work?
<asp:TemplateField HeaderText="Start Date" SortExpression="StartDate" >
<EditItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("StartDate", "{0:dd/MM/yyyy}") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("StartDate", "{0:dd/MM/yyyy}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Start Date" SortExpression="StartDate"
<EditItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("StartDate", "{0:dd/MM/yyyy}") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("StartDate", "{0:dd/MM/yyyy}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
ASKER
I tried that before and it showed the date fine but when I try to update I end up back where I started.
Not a date.
Not a date.
I see, what if you don't specify a type in the param? I messed around in vs05 and tried a few string conversions. When I did this it failed:
string dt = "13/9/2007";
DateTime.ParseExact(dt, "dd/MM/yyyy", System.Globalization.Cultu reInfo.Inv ariantCult ure);
Response.Write(dt);
But when i changed it to this it parsed fine.
string dt = "13/09/2007";
DateTime.ParseExact(dt, "dd/MM/yyyy", System.Globalization.Cultu reInfo.Inv ariantCult ure);
Response.Write(dt);
string dt = "13/9/2007";
DateTime.ParseExact(dt, "dd/MM/yyyy", System.Globalization.Cultu
Response.Write(dt);
But when i changed it to this it parsed fine.
string dt = "13/09/2007";
DateTime.ParseExact(dt, "dd/MM/yyyy", System.Globalization.Cultu
Response.Write(dt);
That makes sense but for most users its impractical to place the 0 in front of the months or days, so the most secure way would be to add the extra time programmatically, but the easiest way would be to leave out the type property of the param and allow sql to add the extra time, though i think that may open you up to sql injection.
ASKER
The affected users are a select few (with better than average brains thankfully!) this is an admin section (content management) for an intranet so sql injection is not an issue here. I'll try your suggestion above and report back
Your solution, dusion, seems to be good (even though I haven't tried it). But there is one thing mission in your solution which is using of "Eval". This function is a one way binding. You have to use "Bind" instead which is a 2 way binding.
ASKER
I've tried using bind and eval, both allowed me to format the date (or string as it seems) but neither got me past the update error.
In an ideal world I would take my date from SQL display either as a date dd/mm/yyyy (or as a string if need be) in the gridview, allow the user to edit the date by hand and then do whatever it takes to convert this new date/string value back into whatever it needs to be to allow the update function to work.
If I use this....
<asp:TemplateField HeaderText="Start Date" SortExpression="StartDate" >
<EditItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("StartDate", "{0:dd/MM/yyyy}") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("StartDate", "{0:dd/MM/yyyy}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
I get the error that the SP expects parameter @StartDate which is not supplied.
Should the label IDs be called StartDate for this to work?
In an ideal world I would take my date from SQL display either as a date dd/mm/yyyy (or as a string if need be) in the gridview, allow the user to edit the date by hand and then do whatever it takes to convert this new date/string value back into whatever it needs to be to allow the update function to work.
If I use this....
<asp:TemplateField HeaderText="Start Date" SortExpression="StartDate"
<EditItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("StartDate", "{0:dd/MM/yyyy}") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("StartDate", "{0:dd/MM/yyyy}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
I get the error that the SP expects parameter @StartDate which is not supplied.
Should the label IDs be called StartDate for this to work?
The error "SP expects parameter @StartDate which is not supplied" is due to using "Eval". That's why you should use "Bind" instead.
Don't forget to remove the formatting from the SQL Statement since it is being done on the ASP.NET side. And in the <EditItemTemplate>, you should put a textbox instead of a label.
If nothing works, best thing is to do the update by yourself by doing the following:
1. on the "RowUpdating" event of your gridview, put the following: e.Cancel = true
2. Get the values of your StartDate and EndDate (this depends on whether you are using a templatefield or a boundfield)
3. Write the update there and do not rely on the update of your GridView
If you have any more questions, let me know
Don't forget to remove the formatting from the SQL Statement since it is being done on the ASP.NET side. And in the <EditItemTemplate>, you should put a textbox instead of a label.
If nothing works, best thing is to do the update by yourself by doing the following:
1. on the "RowUpdating" event of your gridview, put the following: e.Cancel = true
2. Get the values of your StartDate and EndDate (this depends on whether you are using a templatefield or a boundfield)
3. Write the update there and do not rely on the update of your GridView
If you have any more questions, let me know
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
What should I put in place of e.NewValues ?
I get an error saying that e.NewValues is not a member of system.eventargs
I get an error saying that e.NewValues is not a member of system.eventargs
gridview rowupdating is handled by GridViewUpdateEventArgs
so in your void replace system.EventArgs with GridViewUpdateEventArgs
so in your void replace system.EventArgs with GridViewUpdateEventArgs
You have to use the "RowUpdating" event, QPR.