Hi haider6.9,
As per your query i have create the sample. If you find any doubt please revert me back. Refer the below sample.
HTML
<div>
<asp:DropDownList ID="ddl_Years" runat="server">
<asp:ListItem Text="---Select The Year---" Value="-1"> </asp:ListItem>
<asp:ListItem Text="2017" Value="2017"></asp:ListItem>
<asp:ListItem Text="2016" Value="2016"></asp:ListItem>
<asp:ListItem Text="2015" Value="2015"></asp:ListItem>
<asp:ListItem Text="2014" Value="2014"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddl_Months" runat="server">
<asp:ListItem Text="--Select The Month---" Value="-1"></asp:ListItem>
<asp:ListItem Text="January" Value="01"></asp:ListItem>
<asp:ListItem Text="Febrauary" Value="02"></asp:ListItem>
<asp:ListItem Text="March" Value="03"></asp:ListItem>
<asp:ListItem Text="April" Value="04"></asp:ListItem>
<asp:ListItem Text="May" Value="05"></asp:ListItem>
<asp:ListItem Text="June" Value="06"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddl_Days" runat="server">
<asp:ListItem Text="--Select The Day---" Value="-1"></asp:ListItem>
<asp:ListItem Text="Monday" Value="01"></asp:ListItem>
<asp:ListItem Text="Tuesday" Value="02"></asp:ListItem>
<asp:ListItem Text="Wednesday" Value="03"></asp:ListItem>
<asp:ListItem Text="Thursday" Value="04"></asp:ListItem>
<asp:ListItem Text="Friday" Value="05"></asp:ListItem>
<asp:ListItem Text="Saturday" Value="06"></asp:ListItem>
<asp:ListItem Text="Sunday" Value="07"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddl_Hours" runat="server" AppendDataBoundItems="true">
</asp:DropDownList>
<asp:DropDownList ID="ddl_Minutes" runat="server" AppendDataBoundItems="true">
</asp:DropDownList>
<asp:DropDownList ID="ddl_TimeZones" runat="server">
<asp:ListItem Text="AM" Value="AM"></asp:ListItem>
<asp:ListItem Text="PM" Value="PM"></asp:ListItem>
</asp:DropDownList>
<br />
<asp:Button ID="Submit" Text="Submit" runat="server" OnClick="Submit_Click" />
</div>
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindHours();
BindMinutes();
}
}
private void BindHours()
{
ddl_Hours.Items.Clear();
ddl_Hours.Items.Add(new ListItem("Select Hour", "-1"));
for (int i = 1; i <= 12; i++)
{
ddl_Hours.Items.Add(new ListItem(i.ToString(), i.ToString().PadLeft(2, '0')));
}
}
private void BindMinutes()
{
ddl_Minutes.Items.Clear();
ddl_Minutes.Items.Add(new ListItem("Select Minute", "-1"));
for (int i = 1; i <= 60; i++)
{
ddl_Minutes.Items.Add(new ListItem(i.ToString(), i.ToString().PadLeft(2, '0')));
}
}
protected void Submit_Click(object sender, EventArgs e)
{
string startDate = ddl_Years.SelectedValue + '-' + ddl_Months.SelectedItem.Value + '-' + ddl_Days.SelectedItem.Value + '-' + ddl_Hours.SelectedValue + '-' + ddl_Minutes.SelectedValue + '-' + ddl_TimeZones.SelectedValue;
string cns = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
using (SqlConnection con = new SqlConnection(cns))
{
SqlCommand cmd = new SqlCommand("INSERT INTO DATETIMES(StartDate)VALUES(@StartDate)");
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@StartDate", startDate);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
VB
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
BindHours()
BindMinutes()
End If
End Sub
Private Sub BindHours()
ddl_Hours.Items.Clear()
ddl_Hours.Items.Add(New ListItem("Select Hour", "-1"))
For i As Integer = 1 To 12
ddl_Hours.Items.Add(New ListItem(i.ToString(), i.ToString().PadLeft(2, "0"c)))
Next
End Sub
Private Sub BindMinutes()
ddl_Minutes.Items.Clear()
ddl_Minutes.Items.Add(New ListItem("Select Minute", "-1"))
For i As Integer = 1 To 60
ddl_Minutes.Items.Add(New ListItem(i.ToString(), i.ToString().PadLeft(2, "0"c)))
Next
End Sub
Protected Sub Submit_Click(sender As Object, e As EventArgs)
Dim startDate As String = ddl_Years.SelectedValue + "-"c + ddl_Months.SelectedItem.Value + "-"c + ddl_Days.SelectedItem.Value + "-"c + ddl_Hours.SelectedValue + "-"c + ddl_Minutes.SelectedValue + "-"c + ddl_TimeZones.SelectedValue
Dim cns As String = ConfigurationManager.ConnectionStrings("ConString").ConnectionString
Using con As New SqlConnection(cns)
Dim cmd As New SqlCommand("INSERT INTO DATETIMES(StartDate)VALUES(@StartDate)")
cmd.Connection = con
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("@StartDate", startDate)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Sub
Output
StartDate |
EndDate |
2017-01-01-01-01-PM |
NULL |