1

我有 2 个日历控件“cldStartDate”和“cldEndDate”,它们链接到 2 个文本框“txtStartDate”和“txtEndDate”。选择日期后,这些文本框将保存日期。我正在尝试让我的代码将日期添加到我的数据库中。它具有以下字段:

-ProjectName -Description -StartDate -EndDate

'StartDate' 和 'EndDate' 在我的数据库中都是 DateTime 类型。有人可以指出我的代码哪里出错了。我想我需要更改代码以将日期值作为 DateTime 传递。有没有人知道我可以如何做到这一点。代码如下。谢谢!

Protected Sub btnAddProject_Click(ByVal sender As Object, ByVal e As System.EventArgs) 处理 btnAddProject.Click

    Dim projectName As String
    Dim projectDescription As String
    Dim startdate As String
    Dim enddate As String

    projectName = txtProjectName.Text
    projectDescription = txtProjectName.Text
    startdate = txtStartDate.Text
    enddate = txtEndDate.Text

    ' check the validity of information entered
    Dim isValid As Boolean
    isValid = True

    If String.IsNullOrEmpty(projectName) Or String.IsNullOrEmpty(projectDescription)     Then
        isValid = False
    End If

    If isValid Then
        'add everything to the database
        Dim db As New AgileClassesDataContext()

        Dim startDate As String
        startDate = DateTime.Parse(startDate)

        Dim endDate As String
        endDate = DateTime.Parse(endDate)

        ' create a project to populate a row in the PROJECT table
        Dim project As New Project With _
        {.ProjectName = projectName, _
         .Description = projectDescription _
         .StartDate = startdate _
         .EndDate = enddate}

        ' add the new project to the PROJECT table
        db.Projects.InsertOnSubmit(project)

        ' submit the changes to the database
        Try
            db.SubmitChanges()
        Catch ex As Exception
            Console.WriteLine(ex)
            db.SubmitChanges()
        End Try'
4

1 回答 1

1

怀疑直接的问题是您的对象初始化程序的语法。而不是这个:

Dim project As New Project With _
    {.ProjectName = projectName, _
     .Description = projectDescription _
     .StartDate = startdate _
     .EndDate = enddate}

...每个属性后应该有一个逗号:

Dim project As New Project With { _
    .ProjectName = projectName, _
    .Description = projectDescription, _
    .StartDate = startdate, _
    .EndDate = enddate }
于 2012-11-22T17:44:09.150 回答