This is a migrated thread and some comments may be shown as answers.

reset of values when try to save via button

3 Answers 98 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Nikola
Top achievements
Rank 2
Nikola asked on 29 Nov 2012, 03:39 PM

Heey

I have a strange error when i try to uppdate my data in db an Error appears( Value of '0001-01-01 00:00:00' is not valid for 'SelectedDate'. 'SelectedDate' should be between 'MinDate' and 'MaxDate'. Parameter name: SelectedDate)

Story: I have a grid in in my UserEditPanel where i try to create users that works perfectly but in this same UserEditPanel there is a little gird where you can add (with new record button) FromDate ToDate WorkingTime and VacationDays for one user that is in same time how to say in creation . if I try to save/update it wont it just reset only in the fields FromDate and ToDate to (0001-01-01 00:00:00') some help would be just awesome btw i'm working with Viewstate

I think that the error appears in this part of the code and i really dont know what is wrong here ...


Code 

       <telerik:RadDatePicker ID="fromdatepicker" runat="server" DbSelectedDate='<%# Eval("FromDate") == null ? null : Eval("FromDate") %>' TabIndex="4">
                                                        </telerik:RadDatePicker>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td>To</td>
                                                    <td>
                                                        <telerik:RadDatePicker ID="todatepicker" runat="server" DbSelectedDate='<%# Eval("ToDate") == null ? null :Eval("ToDate")%>'
                                                            TabIndex="4">
                                                        </telerik:RadDatePicker>


<
asp:Button ID="btnUpdate"  Text='<%# (Container is GridEditFormInsertItem) ? "Insert" : "Update" %>'
                                                runat="server" CommandName='<%# (Container is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>'></asp:Button
                                            </div>
                                        </td>
                                        <td>
                                            <asp:Button  ID="btnCancel" Text="Cancel" runat="server" CausesValidation="False"
                                                CommandName="Cancle" OnClick="btnCancel_Click"></asp:Button>
                                        </td>
                                    </tr>
                                </table>
                        </FormTemplate>
                    </EditFormSettings>
                 </MasterTableView>
                <FilterMenu EnableImageSprites="False">
                </FilterMenu>
            </telerik:RadGrid>
        </telerik:RadAjaxPanel>
    </div>
    <div class="BackAndSave">
        <telerik:RadButton ID="rbBack" CssClass="rowButton" runat="server" Text="Back" OnClick="rbBack_Clicked"
            CausesValidation="false" />
        <telerik:RadButton ID="rbSave" CssClass="rowButton" runat="server" Text="Save" OnClick="rbSave_Clicked" />


nothing special just some buttons & fields  



code behind 

the error appears here (exitingAnnualVacation.FromDate = editedAnualVacation.FromDate;) the second from date is reset

protected void rgAnnualVacation_UpdateCommand(object sender, GridCommandEventArgs e)
    {
        try
        {
            GridEditableItem item = e.Item as GridEditableItem;
            Hashtable values = new Hashtable();
            item.ExtractValues(values);
 
            AnnualVacation editedAnnualVacation = new AnnualVacation();
            item.UpdateValues(editedAnnualVacation);
          //newAnnualVacation.IsNew = true;
            List<AnnualVacation> annualVacationSource = (List<AnnualVacation>)ViewState["AnnualVacationSource"];
            AnnualVacation existingAnnualVacation = annualVacationSource.Where(av => av.AnnualVacationId == editedAnnualVacation.AnnualVacationId).FirstOrDefault();
            existingAnnualVacation.WorkingTime    = editedAnnualVacation.WorkingTime;
            existingAnnualVacation.VacationDays   = editedAnnualVacation.VacationDays;
            //Set values to zero when update
            existingAnnualVacation.FromDate = editedAnnualVacation.FromDate;
            existingAnnualVacation.ToDate   = editedAnnualVacation.ToDate;
        }
        catch
        {
            //Hier fehlermeldung
        }
    }



Thanks for help and fast answer !!

3 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 30 Nov 2012, 11:28 AM
Hello,

if (editedAnnualVacation.FromDate == DateTime.MinValue && editedAnnualVacation.ToDate== DateTime.MinValue)
        {
            existingAnnualVacation.FromDate = null;
            existingAnnualVacation.ToDate = null;
        }
        else
        {
            existingAnnualVacation.FromDate = editedAnnualVacation.FromDate;
            existingAnnualVacation.ToDate = editedAnnualVacation.ToDate;
        }

If you not assigning the date in datetime then it will take '0001-01-01 00:00:00' Ans SQL's allow greater then or equl to 'January 1, 1900'.


Thanks,
Jayesh Goyani
0
Nikola
Top achievements
Rank 2
answered on 30 Nov 2012, 12:14 PM
thanks for help
but if itry now to add a new record somehow wont work  but i dont see the error 

protected void rgAnnualVacation_InsertCommand(object sender, GridCommandEventArgs e)
  {
      try
      {
          GridEditableItem item = e.Item as GridEditableItem;
          Hashtable values = new Hashtable();
          item.ExtractValues(values);
          AnnualVacation newAnnualVacation = new AnnualVacation();
          item.UpdateValues(newAnnualVacation);
          newAnnualVacation.IsNew = true;
 
          ((List<AnnualVacation>)ViewState["AnnualVacationSource"]).Add(newAnnualVacation);
      }
      catch
      {
          //Hier fehlermeldung
      }

0
Accepted
Angel Petrov
Telerik team
answered on 04 Dec 2012, 12:19 PM
Hello Nikola,

By using item.ExctractValues you won't be able to extract the data from the insert item. The correct way to do so is to reference the controls in the template and extract the values from them. On how to achieve this please review the following help article.

Greetings,
Angel Petrov
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Nikola
Top achievements
Rank 2
Answers by
Jayesh Goyani
Top achievements
Rank 2
Nikola
Top achievements
Rank 2
Angel Petrov
Telerik team
Share this question
or