0

我正在尝试以下拉列表的形式从用户那里获取日期,如下所示:

ASP 代码

 <div class="content-topwidth">
                    <asp:Label ID="lblDOB" runat="server" Text="Label">Enter DOB</asp:Label>
                    <div id="three-cases-cal">
                    <asp:DropDownList ID="ddlDay" runat="server">
                    </asp:DropDownList>
                    <asp:DropDownList ID="ddlMonth" runat="server">
                    </asp:DropDownList>
                    <asp:DropDownList ID="ddlYear" runat="server">
                    </asp:DropDownList>
                    </div>
                </div>

像这样的东西:

出生日期

我必须在 VB 中编写代码并将这些组合框中的值保存在数据库中。

例如:我有一个USERINFO表,并且有一列作为DOB,因此从这些组合框中获取的日期以 dd/mm/yyyy 形式存储在该列中。

任何人都可以帮助我使用VB代码。

谢谢

4

3 回答 3

0

您必须连接日、月和年值并将字符串日期转换为DateTime使用DateTime.ParseExactDateTime.TryParseExact方法。

Dim str=ddlDay.SelectedValue & "-" & ddlMonth.SelectedValue & "-" & ddlYear.SelectedValue
Dim myDate as DateTime
IF DateTime.TryParseExact(str,"dd-MM-yyyy",
        System.Globalization.CultureInfo.InvariantCulture,
          System.Globalization.DateTimeStyles.None,myDate) Then
   //valid date
End IF
于 2012-08-08T12:00:46.953 回答
0

我已经在 Page 的 Page_Load 事件中编写了这段代码,希望对你有帮助

Protected Sub Page_Load(sender As Object, e As EventArgs)

Dim mdate As String = ddlDay.ToString() & "/" & ddlMonth.ToString() & "/" & ddlYear.ToString()

Dim connection As String = "Data Source=.;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;" 'Change datasource, username and password Datasource may be . or if you have sqlexpress then it be .\SqlExpress

    Dim query As String = "Insret into USERINFO(DOB) VALUES('"&mdate&"');"
    Dim con As New SqlConnection(connection)
    Dim cmd As New SqlCommand()
    cmd.CommandText = query
    cmd.CommandType = CommandType.Text
    cmd.Connection = con
    con.Open()
    cmd.ExecuteNonQuery()
    con.Close()
End Sub
于 2012-08-08T11:01:43.930 回答
0

使用它来收集值并保存到 DateTime

Dim year as integer = CInt(ddlYear.SelectedValue)
Dim month as integer = CInt(ddlMonth.SelectedValue)
Dim day as integer = CInt(ddlDay.SelectedValue)

Dim DOB as DateTime  = New DateTime(year, month, day)

所以你可以在数据库中保存DOB

在编辑页面时设置值

用这个

ddlYear.SelectedValue = DOB.Year.ToString()
ddlMonth.SelectedValue = DOB.Month.ToString()
ddlDay.SelectedValue = DOB.Day.ToString()
于 2012-08-09T16:42:27.027 回答