2012-04-16 56 views
1

我有一個使用了Ajax日期日曆Web表單。這工作正常。我的問題是,當我提交我的表單時,我收到以下消息。System.ArgumentException:字符串值不能轉換爲日期

'String value can not be converted to a date' .AgendaDate = New SmartDate(txtAgendaDate.Text) 

這裏是保存日曆和關聯的文本框中我的網頁形式...

<td> 
    <asp:TextBox ID="txtAgendaDate" runat="server" ForeColor="Black" ></asp:TextBox> 
</td> 
<td> 
    <asp:ImageButton runat="Server" ID="ImageButton1" ImageUrl="~/images/calendarpic.png" 
       AlternateText="Click here to display calendar" /> 

    <cc1:calendarextender ID="CalendarExtender1" runat="server" 
        TargetControlID="txtAgendaDate" PopupButtonID="ImageButton1" > 
    </cc1:calendarextender> 
</td> 

我有一個Web窗體上的關聯屬性的類。除了ajax日曆的文本字段以外,其餘字段處理數據並將數據提交到數據庫。

這裏是我的簡化版爲類和txtAgendaDate代碼的代碼......

#Region " Agenda Variables " 

'Declare Variables and data types and set default values 
Private mAgendaID As Integer = 0 
Private mOrganiser As String = "" 
Private mMeeting As String = "" 
Private mAgendaDate As SmartDate = New SmartDate() 

#End Region 

#Region " Constructors " 

Public Sub New() 
End Sub 

Public Sub New(ByVal reader As SafeDataReader) 
    ' Public Sub New(ByVal reader As SQLDataReader) 

    'Combine variables & property types 
    With reader 
     mAgendaID = .GetInt32("AgendaID") 
     mOrganiser = .GetString("Organiser") 
     mMeeting = .GetString("Meeting") 
     mAgendaDate = .GetSmartDate("AgendaDate") 
    End With 
End Sub 

#End Region 

#Region "Properties" 

'Define form field properies so that they can be used when adding the data to the database on the add button is pressed. 
Public Property AgendaID() As Integer 
    Get 
     Return mAgendaID 
    End Get 
    Set(ByVal Value As Integer) 
     mAgendaID = Value 
    End Set 
End Property 

Public Property Organiser() As String 
    Get 
     Return mOrganiser 
    End Get 
    Set(ByVal value As String) 
     mOrganiser = value 
    End Set 
End Property 

Public Property Meeting() As String 
    Get 
     Return mMeeting 
    End Get 
    Set(ByVal value As String) 
     mMeeting = value 
    End Set 
End Property 

Public Property AgendaDate() As SmartDate 
    Get 
     Return mAgendaDate 
    End Get 
    Set(ByVal Value As SmartDate) 
     mAgendaDate = Value 
    End Set 
End Property 

#End Region 


End Class 

這裏是我的命令看起來所連接到數據庫,並在存儲過程中,也有參數。

Public Class Agenda_TempDAL 

Public Shared Function AddAgenda_Temp(ByVal Agenda_Temp As Agenda_Temp) As Integer 

    'Declare i as integer as 0 
    Dim iAgendaID As Integer = 0 

    'Database conn, this is linked to the web config file .AppSettings 
    Using dbconnection As New SqlConnection(ConfigurationManager.AppSettings("dbconnection")) 
     dbconnection.Open() 

     'Command to state the stored procedure and the name of the stored procedure 
     Using dbcommand As SqlCommand = dbconnection.CreateCommand 
      With dbcommand 
       .CommandType = CommandType.StoredProcedure 
       .CommandText = "Stored_Proc_Name" 

       'Create parameter for AgendaID and output 
       Dim oParam As New SqlParameter 
       oParam.ParameterName = "@AgendaID" 
       oParam.Direction = ParameterDirection.Output 
       oParam.SqlDbType = SqlDbType.Int 

       'Create parameters for the remaining fields 
       .Parameters.Add(oParam) 
       .Parameters.AddWithValue("@Organiser", Agenda_Temp.Organiser) 
       .Parameters.AddWithValue("@Meeting", Agenda_Temp.Meeting) 
       .Parameters.AddWithValue("@AgendaDate", Agenda_Temp.AgendaDate.DBValue) 

       'Simply execute the query 
       dbcommand.ExecuteNonQuery() 

      End With 
     End Using 
    End Using 

    'Need to return the agendaID as an integer. 
    Return iAgendaID 

End Function 
End Class 

這裏是按鈕背後的代碼網頁。這是導致基於屬性/字段的錯誤的頁面。問題就出在這條線......

.AgendaDate = New SmartDate(txtAgendaDate.Text) 

該按鈕的整個代碼是在這裏...

Protected Sub btnAddAgendaTemplate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddAgendaTemplate.Click 

    'This works alongside the Class named Agenda_Temp which has the properties and DB connection assigned to it for each web form field. 
    Dim oAgenda_Temp As New Agenda_Temp 

    'Within the object Agenda_Temp Class use the properties defined. 
    'They are required to be defined in the Agenda_Temp/ app code so we can use them within here. 

    With oAgenda_Temp 
     .Organiser = txtOrganiser.Text 
     .Meeting = txtMeeting.Text 
     .AgendaDate = New SmartDate(txtAgendaDate.Text) 


     'Within the object Agenda_Temp class use the defined DAL which includes all the DC connect and stored procedures. 
     oAgenda_Temp.AgendaID = Agenda_TempDAL.AddAgenda_Temp(oAgenda_Temp) 
    End With 

End Sub 
End Class 

據我所知,它告訴我,該字符串值不能轉換爲日期但我不知道鋤頭解決這個問題,因爲我是.net 2010的新手?

任何幫助非常感謝。

+0

http://msdn.microsoft.com/en-us/library/cc165448.aspx – JonH 2012-04-16 14:49:18

+2

哪裏SmartDate'的'接收字符串的構造?你已經展示了很多代碼,爲什麼你忽略了相關的部分? – 2012-04-16 14:56:32

回答

0

將字符串到日期newing之前: 從MSDN:

string date = "01/08/2008"; 
DateTime dt = Convert.ToDateTime(date); 

你的將成爲

DateTime dt = Convert.ToDateTime(txtAgendaDate.Text) 

然後將日期傳遞到您的SmartDate構造:

oAgenda_Temp.AgendaDate = new SmartDate(dt) 

最終結果:

With oAgenda_Temp 
     .Organiser = txtOrganiser.Text 
     .Meeting = txtMeeting.Text 
     .AgendaDate = New SmartDate(Convert.ToDateTime(txtAgendaDate.Text)) 


     'Within the object Agenda_Temp class use the defined DAL which includes all the DC connect and stored procedures. 
     oAgenda_Temp.AgendaID = Agenda_TempDAL.AddAgenda_Temp(oAgenda_Temp) 
    End With 
+0

同樣在這裏:你怎麼知道SmartDate的構造函數需要DateTime作爲參數?那麼它甚至不會編譯。編輯:也許'選擇嚴格'是_off_什麼總是一個壞主意。 – 2012-04-16 15:10:53

+0

@ JonH。此轉換處理了一個款待並接受選定日期並將其填充到數據庫中。許多感謝所有。 – Betty 2012-04-16 15:21:25

+0

@Betty沒問題 - 只是將答案標記爲已接受,所以我們可以將此問題關閉。 – JonH 2012-04-16 15:22:06

0

正如其他人指出的那樣,您需要將輸入值轉換爲DateTime。我不知道SmartDate()函數在做什麼,但錯誤消息清楚地表明該值無法轉換爲日期。

其次,我想補充一些驗證,以確保輸入是有效的提交頁面之前。使用RequiredFieldValidatorCompareValidatorRegularExpressionValidator

<asp:TextBox ID="txtDate" runat="server" ... /> 
<asp:RequiredFieldValidator ID="reqDate" runat="server" ErrorMessage="Required" Display="Dynamic" ControlToValidate="txtDate"></asp:RequiredFieldValidator> 
<asp:RegularExpressionValidator ID="regDate" runat="server" ControlToValidate="txtDate" ErrorMessage="Please enter a valid date in the format (mm/dd/yyyy)" ValidationExpression="^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d$"></asp:RegularExpressionValidator>