Imports System.Data
Imports System.Data.OleDb
Imports System.IO
Public Class bntPrint
Dim conn As New OleDb.OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;data source=db_Village.mdb")
Dim ObjCurrencyMgr As CurrencyManager
Dim ds As New DataSet
Dim da As New OleDbDataAdapter
Dim dr As OleDbDataReader
Dim dt As DataTable
Dim command As OleDbCommand
Dim task As String = ""
Dim TEMP, TEXT_CLASS As String
Dim strFont1, strFont2 As String
Private Sub showData()
conn.Open()
Dim ds As New DataSet
Dim sqlCmd As String = "SELECT * From tb_Committee"
da = New OleDbDataAdapter(sqlCmd, conn)
da.Fill(ds, "Customer")
DGV1.DataSource = ds.Tables(0)
DGV1.Columns(0).HeaderText = "เลขที่สมาชิก"
DGV1.Columns(1).HeaderText = "ชื่อ"
DGV1.Columns(2).HeaderText = "นามสกุล"
DGV1.Columns(3).HeaderText = "ที่อยู่"
DGV1.Columns(4).HeaderText = "เบอร์โทรศัพท์"
DGV1.Columns(5).HeaderText = "วันที่"
DGV1.Columns(6).HeaderText = "หมายเลขบัตรประชาชน"
DGV1.Columns(0).Width = 70
DGV1.Columns(1).Width = 100
DGV1.Columns(2).Width = 100
DGV1.Columns(3).Width = 180
DGV1.Columns(4).Width = 100
DGV1.Columns(5).Width = 80
DGV1.Columns(6).Width = 80
conn.Close()
Binding()
setDisable()
End Sub
Private Sub Binding()
Dim ObjC As CurrencyManager
ObjC = Me.BindingContext(DGV1.DataSource, "")
txtCus_id.DataBindings.Clear()
txtName.DataBindings.Clear()
txtLastName.DataBindings.Clear()
txtAddr.DataBindings.Clear()
txtTel.DataBindings.Clear()
DateTimePicker1.DataBindings.Clear()
txtPreple.DataBindings.Clear()
txtCus_id.DataBindings.Add("text", DGV1.DataSource, "เลขที่สมาชิก")
txtName.DataBindings.Add("text", DGV1.DataSource, "ชื่อ")
txtLastName.DataBindings.Add("text", DGV1.DataSource, "นามสกุล")
txtAddr.DataBindings.Add("text", DGV1.DataSource, "ที่อยู่")
txtTel.DataBindings.Add("text", DGV1.DataSource, "เบอร์โทรศัพท์")
DateTimePicker1.DataBindings.Add("text", DGV1.DataSource, "วันที่")
txtPreple.DataBindings.Add("text", DGV1.DataSource, "หมายเลขบัตรประชาชน")
End Sub
Private Sub maxId()
Dim maxId As Integer = 0
conn.Open()
Dim sqlCmd As String = "Select Max(เลขที่สมาชิก) From tb_Committee"
command = New OleDbCommand(sqlCmd, conn)
Try
maxId = command.ExecuteScalar()
Catch ex As Exception
maxId = 0
End Try
txtCus_id.Text = maxId + 1
conn.Close()
End Sub
Private Sub setDisable()
txtName.Enabled = False
txtLastName.Enabled = False
txtAddr.Enabled = False
txtTel.Enabled = False
txtPreple.Enabled = False
End Sub
Private Sub setBtnDisable()
btnAdd.Enabled = False
btnEdit.Enabled = False
btnSave.Enabled = True
btnCancel.Enabled = True
End Sub
Private Sub setBtnEnable()
btnAdd.Enabled = True
btnEdit.Enabled = True
btnSave.Enabled = False
btnCancel.Enabled = False
End Sub
Private Sub setEnable()
txtName.Enabled = True
txtLastName.Enabled = True
txtAddr.Enabled = True
txtTel.Enabled = True
txtPreple.Enabled = True
End Sub
Private Sub clearText()
txtCus_id.Text = ""
txtName.Text = ""
txtLastName.Text = ""
DateTimePicker1.Text = ""
txtAddr.Text = ""
txtTel.Text = ""
txtPreple.Text = ""
End Sub
Private Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
task = "Add"
clearText()
setEnable()
maxId()
setBtnDisable()
txtName.Focus()
End Sub
Private Sub Customer_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
showData()
End Sub
Private Sub btnEdit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnEdit.Click
task = "Edit"
setEnable()
setBtnDisable()
txtName.Focus()
End Sub
Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim conn As New OleDb.OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;data source=db_Village.mdb")
conn.Close()
conn.Open()
Dim sql As String
sql = "SELECT ชื่อ,นามสกุล FROM tb_Committee WHERE ชื่อ ='" & txtName.Text & "' AND นามสกุล ='" & txtLastName.Text & "'"
Dim dr As OleDbDataReader
Dim com As OleDbCommand
com = New OleDbCommand()
With (com)
.CommandType = CommandType.Text
.CommandText = sql
.Connection = conn
.Parameters.Clear()
.Parameters.Add("ชื่อ", OleDbType.VarChar).Value = txtName.Text
dr = .ExecuteReader()
If dr.HasRows Then
MsgBox("มีข้อมูลซ้ำกรุณาตรวจสอบ")
Else
Dim sqlCmd As String = ""
If (task.Equals("Add")) Then
sqlCmd = "INSERT INTO tb_Committee(เลขที่สมาชิก,ชื่อ,นามสกุล,ที่อยู่,เบอร์โทรศัพท์,วันที่,หมายเลขบัตรประชาชน,รูปภาพ) VALUES(" & txtCus_id.Text & ", '" & txtName.Text & "','"
sqlCmd &= txtLastName.Text & "','" & txtAddr.Text & "','" & txtTel.Text & "','" & DateTimePicker1.Text & "','" & txtPreple.Text & "')"
ElseIf (task.Equals("Edit")) Then
sqlCmd = "Update tb_Committee SET "
sqlCmd &= " ชื่อ = '" & txtName.Text & "', นามสกุล= '" & txtLastName.Text & "', "
sqlCmd &= " ที่อยู่= '" & txtAddr.Text & "', เบอร์โทรศัพท์ = '" & txtTel.Text & "', วันที่ = '" & DateTimePicker1.Text & "', หมายเลขบัตรประชาชน = '" & txtPreple.Text & "'"
sqlCmd &= " Where เลขที่สมาชิก = " & txtCus_id.Text
End If
'MessageBox.Show(sqlCmd)
dbQuery(sqlCmd)
showData()
setBtnEnable()
End If
End With
End Sub
Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
clearText()
setDisable()
showData()
setBtnEnable()
End Sub
Private Sub txtTel_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtTel.KeyPress
If Not ((Char.IsDigit(e.KeyChar)) OrElse (Char.IsControl(e.KeyChar))) Then
e.Handled = True
MessageBox.Show("กรุณากรอกข้อมูลเป็นตัวเลข")
End If
End Sub
Private Sub btndelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndelete.Click
If MessageBox.Show("คุณต้องการที่จะลบข้อมูลนี้?", "คำเตือน", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) = Windows.Forms.DialogResult.Yes Then
Dim sqlCmd As String = "DELETE FROM tb_Committee WHERE เลขที่สมาชิก = " & txtCus_id.Text
dbQuery(sqlCmd)
Binding()
showData()
End If
End Sub
Public Sub getCusID(ByVal id As String)
txtCus_id.Text = id
End Sub
Private Sub txtCus_id_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtCus_id.KeyPress
If Not ((Char.IsDigit(e.KeyChar)) OrElse (Char.IsControl(e.KeyChar))) Then
e.Handled = True
MessageBox.Show("กรุณากรอกข้อมูลเป็นตัวเลข")
Else
If e.KeyChar = ControlChars.Cr Then
'MessageBox.Show(DateTimePicker1.Value.Month)
Dim sqlCmd As String = "SELECT * FROM tb_Committee WHERE เลขที่สมาชิก = " & txtCus_id.Text
conn.Open()
Dim command As New OleDbCommand(sqlCmd, conn)
dr = command.ExecuteReader()
If dr.HasRows Then
dt = New DataTable()
dt.Load(dr)
dr.Close()
For Each drw As DataRow In dt.Rows
txtName.Text = drw.Item("ชื่อ").ToString
txtPreple.Text = drw.Item("หมายเลขบัตรประชาชน").ToString
txtLastName.Text = drw.Item("นามสกุล").ToString
txtAddr.Text = drw.Item("ที่อยู่").ToString
txtTel.Text = drw.Item("เบอร์โทรศัพท์").ToString
DateTimePicker1.Text = drw.Item("วันที่").ToString
Next
conn.Close()
Else
MessageBox.Show("ไม่พบข้อมูลที่ค้นหา กรุณาลองใหม่อีกครั้ง", "ผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
conn.Close()
End If
End If
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SearchCommittee.Show()
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim strId As String
Dim strName As String
Dim strLastName As String
Dim strPreple As String
e.Graphics.DrawString("บัตรประจำตัวคณะกรรมการ", New Font(strFont1, 20, FontStyle.Bold), Brushes.Black, 300, 100)
e.Graphics.DrawString("เลขที่สมาชิก", New Font(strFont1, 10, FontStyle.Bold), Brushes.Black, 290, 200)
e.Graphics.DrawString("ชื่อ", New Font(strFont1, 10, FontStyle.Bold), Brushes.Black, 290, 240)
e.Graphics.DrawString("นามสกุล", New Font(strFont1, 10, FontStyle.Bold), Brushes.Black, 290, 280)
e.Graphics.DrawString("บัตรประจำตัวประชาชน", New Font(strFont1, 10, FontStyle.Bold), Brushes.Black, 250, 320)
strId = txtCus_id.Text
strName = txtName.Text
strLastName = txtLastName.Text
strPreple = txtPreple.Text
e.Graphics.DrawString(strId, New Font(strFont1, 10, FontStyle.Bold), Brushes.Black, 410, 200)
e.Graphics.DrawString(strName, New Font(strFont1, 10, FontStyle.Bold), Brushes.Black, 340, 240)
e.Graphics.DrawString(strLastName, New Font(strFont1, 10, FontStyle.Bold), Brushes.Black, 410, 280)
e.Graphics.DrawString(strPreple, New Font(strFont1, 10, FontStyle.Bold), Brushes.Black, 400, 320)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
PrintPreviewDialog1.ShowDialog()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Main.Show()
Me.Hide()
End Sub
Private Sub LnkPic_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LnkPic.LinkClicked
With OpenFileDialog1
.Filter = "Image Files(*.gif;*.jpg)|*.gif;*.jpg"
.FileName = ""
If (.ShowDialog() = DialogResult.OK) Then
PictureBox1.Image = Image.FromFile(.FileName)
End If
End With
End Sub
End Class
'*** Read Binary Data ***'
Dim imbByte(fUpload.PostedFile.InputStream.Length) As Byte
fUpload.PostedFile.InputStream.Read(imbByte, 0, imbByte.Length)
'*** MimeType ***'
Dim ExtType As String = System.IO.Path.GetExtension(fUpload.PostedFile.FileName).ToLower()
Dim strMIME As String = Nothing
Select Case ExtType
Case ".gif"
strMIME = "image/gif"
Case ".jpg", ".jpeg", ".jpe"
strMIME = "image/jpeg"
Case ".png"
strMIME = "image/png"
Case Else
Me.lblStatus.Text = "Invalid file type."
Exit Sub
End Select
'*** Insert to Database ***'
Dim objConn As New OleDbConnection
Dim strConnString, strSQL As String
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="& _
Server.MapPath("App_Data/mydatabase.mdb")&";Jet OLEDB:Database Password=;"
strSQL = "INSERT INTO files (Name,FilesName,FilesType) " & _
" VALUES " & _
" (@sName,@sFilesName,@sFilesType)"
objConn.ConnectionString = strConnString
objConn.Open()
Dim objCmd As New OleDbCommand(strSQL, objConn)
objCmd.Parameters.Add("@sName", OleDbType.VarChar).Value = Me.txtName.Text
objCmd.Parameters.Add("@sFilesName", OleDbType.Binary).Value = imbByte
objCmd.Parameters.Add("@sFilesType", OleDbType.VarChar).Value = strMIME
objCmd.ExecuteNonQuery()
ดูตรง
Code (VB.NET)
'*** Read Binary Data ***'
Dim imbByte(fUpload.PostedFile.InputStream.Length) As Byte
fUpload.PostedFile.InputStream.Read(imbByte, 0, imbByte.Length)