我试图以下拉列表的形式从用户那里获取日期,如下面的ASP代码:
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代码。
谢谢
答案 0 :(得分: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
答案 1 :(得分:0)
您必须使用DateTime
或DateTime.ParseExact
方法连接日,月和年值并将字符串日期转换为DateTime.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
答案 2 :(得分: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()