2010-09-02 4 views
0

Obtention de cette erreur lors de la mise à jour d'une ligne via un gridview avec un sqldatasource dans Vb.net/SQL Server 2000. Quelle que soit l'entrée (1/1/2010, vide, etc.) je lui donne Je n'arrive pas à bien faire les choses.Erreur de syntaxe lors de la conversion de datetime à partir d'une chaîne de caractères

code avant l'entrée est transmis à la sp:

Dim sqldatenull As DateTime 
    Dim DateVerify As DateTime 
     sqldatenull = DateTime.MaxValue 
    If (e.NewValues("Date_Start") Is Nothing) Then 
     e.NewValues("Date_Start") = sqldatenull 
    Else 
     DateTime.TryParse(e.NewValues("Date_Start").ToString, DateVerify) 
     MsgBox("Worked!") 
     e.NewValues("Date_Start") = DateVerify 
    End If 

SP:

@ISTag varchar(10), 
@PCISTag varchar(10), 
@User varchar(50), 
@Date_Start datetime, 
@Date_End datetime, 
@Status varchar(50), 
@Cost money, 
@Notes varchar(500), 
@CreatedBy varchar(50), 
@ModifiedBy varchar(50) 
AS 
BEGIN 
    SET NOCOUNT ON; 

    EXEC sp_changeLog 'HardDrive', @ISTag, @ModifiedBy 

    UPDATE T_HardDrive 
     SET PCIStag = @PCISTag, 
      [User] = @User, 
      Date_Start = @Date_Start, 
      Date_End = @Date_End, 
      [Status] = @Status, 
      Cost = @Cost, 
      Notes = @Notes, 
     ModifiedBy = @ModifiedBy 
     WHERE ISTag = @ISTag 

définition SQLDataSource:

<asp:SqlDataSource ID="InventoryHardDrive" runat="server" 
         ConnectionString="<%$ ConnectionStrings:InventoryConnectionString %>" 
         DeleteCommand="DELETE FROM [T_HardDrive] WHERE [ISTag] = @original_ISTag" 
         InsertCommand="sp_HardDriveInsert" InsertCommandType="StoredProcedure" 
         OldValuesParameterFormatString="original_{0}" 
         SelectCommand="sp_HardDriveSelect" SelectCommandType="StoredProcedure" 
         UpdateCommand="sp_HardDriveUpdate" UpdateCommandType="StoredProcedure"> 
         <DeleteParameters> 

         </DeleteParameters> 
         <InsertParameters> 
          <asp:Parameter Name="ISTag" Type="String" /> 
          <asp:Parameter Name="PCISTag" Type="String" /> 
          <asp:Parameter Name="User" Type="String" /> 
          <asp:Parameter Name="Date_Start" Type="DateTime" /> 
          <asp:Parameter Name="Date_End" Type="DateTime" /> 
          <asp:Parameter Name="Status" Type="String" /> 
          <asp:Parameter Name="Cost" Type="Decimal" /> 
          <asp:Parameter Name="Notes" Type="String" /> 
          <asp:Parameter Name="CreatedBy" Type="String" /> 
          <asp:Parameter Name="ModifiedBy" Type="String" /> 
         </InsertParameters> 
         <SelectParameters> 
          <asp:ControlParameter ControlID="hideInactiveCheckBox" Name="Active" 
           PropertyName="Checked" Type="Boolean" /> 
          <asp:ControlParameter ControlID="filterText" DefaultValue="%" Name="ISTag" 
           PropertyName="Text" Type="String" /> 
         </SelectParameters> 
         <UpdateParameters> 
          <asp:Parameter Name="ISTag" Type="String" /> 
          <asp:Parameter Name="PCISTag" Type="String" /> 
          <asp:Parameter Name="User" Type="String" /> 
          <asp:Parameter Name="Date_Start" Type="DateTime" /> 
          <asp:Parameter Name="Date_End" Type="DateTime" /> 
          <asp:Parameter Name="Status" Type="String" /> 
          <asp:Parameter Name="Cost" Type="Decimal" /> 
          <asp:Parameter Name="Notes" Type="String" /> 
          <asp:Parameter Name="CreatedBy" Type="String" /> 
          <asp:Parameter Name="ModifiedBy" Type="String" /> 
         </UpdateParameters> 
        </asp:SqlDataSource> 

Si je quitte la case vide la valeur est définie au "31/12/9999 23:59:59 PM". Est-ce que ce n'est pas le bon format pour DateTime?

Erreur:

Syntax error converting datetime from character string. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Data.SqlClient.SqlException: Syntax error converting datetime from character string. 

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. 

Stack Trace: 


[SqlException (0x80131904): Syntax error converting datetime from character string.] 
    System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +1951450 
    System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +4849003 
    System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +194 
    System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +2394 
    System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +204 
    System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +954 
    System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +162 
    System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) +175 
    System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +137 
    System.Web.UI.WebControls.SqlDataSourceView.ExecuteDbCommand(DbCommand command, DataSourceOperation operation) +386 
    System.Web.UI.WebControls.SqlDataSourceView.ExecuteUpdate(IDictionary keys, IDictionary values, IDictionary oldValues) +325 
    System.Web.UI.DataSourceView.Update(IDictionary keys, IDictionary values, IDictionary oldValues, DataSourceViewOperationCallback callback) +92 
    System.Web.UI.WebControls.GridView.HandleUpdate(GridViewRow row, Int32 rowIndex, Boolean causesValidation) +907 
    System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +704 
    System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source, EventArgs e) +95 
    System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37 
    System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e) +123 
    System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37 
    System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +118 
    System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +135 
    System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10 
    System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 
    System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +175 
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565 
+0

Quelle est l'erreur exacte s'il vous plaît? – gbn

+0

C'est l'erreur renvoyée à l'aide de la variable sqldatenull. – Shawn

Répondre

0

Je dirais que vous essayez de stocker une date dans SQL Server qui dépasse la plage de son type de date. Le DateTime de .NET stockera heureusement des dates quelques millénaires dans le futur, mais SQL Server est plus limité. Pourquoi n'utilisez-vous pas un DateTime Nullable à la place, et passez le null réel (ou mieux encore, DBNull) quand vous devez?

+0

Le remplacement de sqldatenull par DBNull.Value génère l'erreur "L'objet ne peut pas être converti à partir de DBNull vers d'autres types". – Shawn

+0

Y a-t-il une différence entre DateTime et DateTime nullable? – Shawn

+0

'DateTime' est un type de valeur, et en tant que tel, il ne peut pas être' null'/'Nothing'. Un 'DateTime' nullable, cependant, peut. C'est exactement le genre de chose que les types nullables ont été inventés. – tdammers

Questions connexes