2012-04-16 6 views
1

私はAjaxの日付カレンダーを使用するWebフォームを持っています。これは正常に動作します。私が持っている問題は、私が私のフォームを提出するとき、私は次のメッセージを得ることです。System.ArgumentException:文字列の値を日付に変換できません

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

は、ここで私はWebフォームのため、それに関連するプロパティを持つクラスを持っているカレンダーと関連付けられているテキストボックスを保持している私のWebフォーム...

<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> 

です。残りのフィールドは、ajaxカレンダーのテキストフィールド以外のデータをデータベースに送信して送信します。ここで

はここ

#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 

はDBにし、ストアドプロシージャでコネクトを見て、またパラメータを持っている私のコマンドです...クラスのコードとtxtAgendaDateコードのための私のストリップダウンバージョンです。 。

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 

ここには、ウェブページのボタンの背後にあるコードがあります。これは、プロパティ/フィールドに基づいてエラーを発生させるページです。問題は

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 

が、私はその文字列値を日付に変換できないことを私に言っていることを理解して...ボタンの

.AgendaDate = New SmartDate(txtAgendaDate.Text) 

全体のコードはここにある...このライン上にありしかし、私はそれを解決するために鍬を知らないのですが、私は.net 2010に新しいですか?

ご迷惑をおかけして申し訳ございません。

+0

http://msdn.microsoft.com/en-us/library/cc165448.aspx – JonH

+2

どこ文字列を受け取り 'SmartDate'のコンストラクタですか?たくさんのコードを示しましたが、なぜ関連する部分を省いたのですか? –

答えて

0

NEWING前の日付にそれを文字列に変換:MSDNから :

oAgenda_Temp.AgendaDate = new SmartDate(dt) 
を:

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

あなたのは、あなたのSmartDateコンストラクタに日付を渡すその後

DateTime dt = Convert.ToDateTime(txtAgendaDate.Text) 

なります

最終結果:

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_何かは常に悪い考えです。 –

+0

@ JonH。この変換では、処理が行われ、選択された日付が受け入れられ、これがデータベースに取り込まれます。多くのありがとうございます。 – Betty

+0

@Betty no problem - この問題を解消できるように、回答を受け入れたものとしてマークしてください。 – JonH

0

他の人が指摘しているように、入力値をDateTimeに変換する必要があります。私はSmartDate()機能が何をしているのかわかりませんが、エラーメッセージはその値が日付に変換できないことを明確に示しています。

第2に、ページを送信する前に入力が有効であることを確認するためにいくつかの検証を追加します。 RequiredFieldValidatorCompareValidatorまたはRegularExpressionValidatorを使用します。

<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> 
関連する問題