2012-04-16 2 views
1

Ajax 날짜 ​​달력을 사용하는 웹 양식이 있습니다. 이것은 잘 작동합니다. 문제는 내가 내 양식을 제출할 때 다음과 같은 메시지가 나타납니다.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> 

입니다. 나머지 필드는 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에 처음 왔을 때?

도움을 주시면 감사하겠습니다. MSDN에서 :

+0

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

+2

경우 문자열을 사용합니다 SmartDate''의 생성자입니까? 많은 코드를 보여 줬는데 왜 관련 부분을 빠뜨린거야? –

답변

0

는 newing 그 이전의 날짜로 문자열을 변환

oAgenda_Temp.AgendaDate = new SmartDate(dt) 
:

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

귀하의가

DateTime dt = Convert.ToDateTime(txtAgendaDate.Text) 

이 그런 다음 SmartDate 생성자에 날짜를 통과 될 것

최종 결과 :

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을 매개 변수로 사용한다는 것을 어떻게 알 수 있습니까? 그렇다면 컴파일되지 않습니다.편집 : 어쩌면'option strict'는 항상 잘못된 생각 인 _off_입니다. –

+0

@ JonH. 이 변환은 치료를 처리하고 선택된 날짜를 승인하고이를 데이터베이스에 채 웁니다. 많은 분들께 감사드립니다. – Betty

+0

@Betty 문제 없음 -이 문제를 해결할 수 있도록 답변을 승인으로 표시하십시오. – JonH

0

다른 사람들도 지적했듯이 입력 값을 DateTime으로 변환해야합니다. SmartDate() 기능이 무엇을하는지는 모르지만 오류 메시지는 값을 날짜로 변환 할 수 없음을 명확하게 나타냅니다.

둘째, 페이지를 제출하기 전에 입력 내용이 유효한지 확인하기 위해 몇 가지 유효성 검사를 추가 할 것입니다. 사용하여 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> 
관련 문제