Imports System.data
Imports System.IO
Imports System.Data.SqlClient
Partial Class Default4
Inherits System.Web.UI.Page
Public Sub GenerateYear(ByVal NextYear As Integer)
Dim ThisYear As Integer = Date.Now().Year
Dim dt As DataTable = New DataTable("GenerateYear")
Dim dtc As DataColumn
Dim dtr As DataRow
dtc = New DataColumn()
dtc.DataType = System.Type.GetType("System.Int64")
dtc.ColumnName = "GenYear"
dt.Columns.Add(dtc)
For i As Integer = ThisYear To NextYear
dtr = dt.NewRow()
dtr(0) = i
dt.Rows.Add(dtr)
Next
Me.ddlYear.DataSource = dt
Me.ddlYear.DataBind()
End Sub
Public Sub GenerateMonth()
Dim dt As DataTable = New DataTable("GenerateMonth")
Dim dtc As DataColumn
Dim dtr As DataRow
dtc = New DataColumn()
dtc.DataType = System.Type.GetType("System.Int64")
dtc.ColumnName = "GenMonth"
dt.Columns.Add(dtc)
For i As Integer = 1 To 12
dtr = dt.NewRow()
dtr(0) = i
dt.Rows.Add(dtr)
Next
Me.ddlMonth.DataSource = dt
Me.ddlMonth.DataBind()
End Sub
Public Sub GenerateDay(ByVal TotalDay As Integer)
Dim dt As DataTable = New DataTable("GenerateDay")
Dim dtc As DataColumn
Dim dtr As DataRow
dtc = New DataColumn()
dtc.DataType = System.Type.GetType("System.Int64")
dtc.ColumnName = "GenDay"
dt.Columns.Add(dtc)
For i As Integer = 1 To TotalDay
dtr = dt.NewRow()
dtr(0) = i
dt.Rows.Add(dtr)
Next
Me.ddlDay.DataSource = dt
Me.ddlDay.DataBind()
End Sub
Protected Sub ddlMonth_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlMonth.SelectedIndexChanged
GenerateDay(Date.DaysInMonth(Me.ddlYear.SelectedValue, Me.ddlMonth.SelectedValue))
End Sub
Protected Sub ddlYear_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlYear.SelectedIndexChanged
GenerateDay(Date.DaysInMonth(Me.ddlYear.SelectedValue, Me.ddlMonth.SelectedValue))
End Sub
Protected Sub ddlDay_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlDay.SelectedIndexChanged
Me.lblSeleceDate.Text = _
Me.ddlDay.SelectedItem.Text & "/" _
& Me.ddlMonth.SelectedItem.Text & "/" _
& Me.ddlYear.SelectedItem.Text
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Me.Page.IsPostBack Then
Dim DateNow As Date
DateNow = Date.Now()
GenerateDay(Date.DaysInMonth(DateNow.Year, DateNow.Month))
GenerateMonth()
GenerateYear(2020)
Me.ddlDay.SelectedValue = DateNow.Day
Me.ddlMonth.SelectedValue = DateNow.Month
Me.ddlYear.SelectedValue = DateNow.Year
End If
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Label1.Text = ""
Dim connection As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True"
Dim conn As New SqlConnection(connection)
Try
conn.Open()
Dim comm As String = "INSERT INTO CH (CH_IN) "
comm &= " VALUES ('" & lblSeleceDate.Text & "')"
Dim command As New SqlCommand(comm, conn)
command.ExecuteNonQuery()
conn.Close()
Response.Redirect("Default2.aspx")
Catch ex As Exception
Label1.Text = "พบปัญหาเกี่ยวกับข้อมูลโปรดตรวจสอบ"
End Try
End Sub
End Class
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Label1.Text = ""
Dim connection As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True"
Dim conn As New SqlConnection(connection)
If DateTime.Parse(ddlDay.SelectedItem.Value & "/" & ddlMonth.SelectedItem.Value & "/" & ddlYear.SelectedItem.Value).CompareTo(DateTime.Now) > 0 Then
Try
conn.Open()
Dim comm As String = "INSERT INTO CH (CH_IN) "
comm &= " VALUES ('" & lblSeleceDate.Text & "')"
Dim command As New SqlCommand(comm, conn)
command.ExecuteNonQuery()
conn.Close()
Response.Redirect("Default2.aspx")
Catch ex As Exception
Label1.Text = "พบปัญหาเกี่ยวกับข้อมูลโปรดตรวจสอบ"
End Try
End If
End Sub