'*** 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 SqlConnection
Dim strConnString, strSQL As String
strConnString = "Server=localhost;UID=sa;PASSWORD=;database=mydatabase;Max Pool Size=400;Connect Timeout=600;"
strSQL = "INSERT INTO files (Name,FilesName,FilesType) " & _
" VALUES " & _
" (@sName,@sFilesName,@sFilesType)"
objConn.ConnectionString = strConnString
objConn.Open()
Dim objCmd As New SqlCommand(strSQL, objConn)
objCmd.Parameters.Add("@sName", SqlDbType.VarChar).Value = Me.txtName.Text
objCmd.Parameters.Add("@sFilesName", SqlDbType.Binary).Value = imbByte
objCmd.Parameters.Add("@sFilesType", SqlDbType.VarChar).Value = strMIME
objCmd.ExecuteNonQuery()
objConn.Close()
objConn = Nothing
Imports System.Data.SqlClient
Public Class Organization
Dim Conn As SqlConnection = New SqlConnection
Dim da As SqlDataAdapter
Dim dt As New DataTable
Dim ds As New DataSet
Dim IsFind As Boolean = False
Dim add_state As Boolean = False
Dim addNewMode As Boolean
Dim land As SqlCommand
Dim user As SqlCommand
Dim tmpFileName As String = ""
Dim CurrentRecord As Integer
Dim ImageFileName As String = ""
Dim organization As SqlCommand
Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
Private Sub Organization_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim sqlorganization As String = ""
sqlorganization = " select * from organization"
sqlorganization &= " ORDER BY ORGID"
With Conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strConnstring
.Open()
End With
If IsFind = True Then
ds.Tables("organization").Clear()
End If
da = New SqlDataAdapter(sqlorganization, Conn)
da.Fill(ds, "organization")
da.SelectCommand.CommandText = sqlorganization
da.Fill(ds, "organization")
If ds.Tables("organization").Rows.Count <> 0 Then
IsFind = True
txtORGID.DataBindings.Add("Text", ds, "organization.ORGID")
txtOg.DataBindings.Add("Text", ds, "organization.ORGNAME")
txtHOUSENO.DataBindings.Add("Text", ds, "organization.HOUSE_NO")
txtMOO.DataBindings.Add("Text", ds, "organization.MOO")
txtSOINAME.DataBindings.Add("Text", ds, "organization.SOINAME")
txtZIPCODE.DataBindings.Add("Text", ds, "organization.ZIPCODE")
txtEMAIL.DataBindings.Add("Text", ds, "organization.EMAIL")
txtFAX.DataBindings.Add("Text", ds, "organization.FAX")
txtPHONE.DataBindings.Add("Text", ds, "organization.TEL")
txtWEB.DataBindings.Add("Text", ds, "organization.WEB")
dt = ds.Tables("organization")
ShowPosition()
End If
Call_province()
call_amphur()
call_tambon()
End Sub
Private Sub ToolStripLabel1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtExit.Click
Me.Close()
End Sub
Private Sub Call_province()
Try
Dim sqlprovince As String = ""
sqlprovince = "SELECT * FROM province"
sqlprovince &= " ORDER BY PROVINCEID"
da = New SqlDataAdapter(sqlprovince, Conn)
da.Fill(ds, "province")
If ds.Tables("province").Rows.Count <> 0 Then
With cboPROVINCE_TH
.DataSource = ds.Tables("province")
.DisplayMember = "PROVINCE_TH"
.ValueMember = "PROVINCEID"
.DataBindings.Clear()
.DataBindings.Add("SelectedValue", ds, "organization.PROVINCEID")
End With
End If
Catch ex As Exception
cboPROVINCE_TH.Text = Nothing
End Try
End Sub
Private Sub call_amphur()
ds.Tables.Clear()
Try
Dim sqldistrict As String = ""
sqldistrict = "SELECT * FROM district"
If cboPROVINCE_TH.ValueMember = "" Then
Exit Sub
Else
sqldistrict &= " where PROVINCEID = '" & CStr(cboPROVINCE_TH.SelectedValue) & "'"
sqldistrict &= " ORDER BY DISTRICTID"
da = New SqlDataAdapter(sqldistrict, Conn)
da.Fill(ds, "district")
If ds.Tables("district").Rows.Count <> 0 Then
With cboDISTRICT_TH
.DataSource = ds.Tables("district")
.DisplayMember = "DISTRICT_TH"
.ValueMember = "DISTRICTID"
.DataBindings.Clear()
.DataBindings.Add("SelectedValue", ds, "organization.DISTRICTID")
End With
End If
End If
Catch ex As Exception
cboDISTRICT_TH.Text = Nothing
End Try
End Sub
Private Sub call_tambon()
ds.Tables.Clear()
Try
Dim sqltambon As String = ""
sqltambon = "SELECT * FROM tambon"
If cboDISTRICT_TH.ValueMember = "" Then
Exit Sub
Else
sqltambon &= " where DISTRICTID = '" & CStr(cboDISTRICT_TH.SelectedValue) & "'"
sqltambon &= " ORDER BY TAMBONID"
da = New SqlDataAdapter(sqltambon, Conn)
da.Fill(ds, "tambon")
If ds.Tables("tambon").Rows.Count <> 0 Then
With cboTAMBON_TH
.DataSource = ds.Tables("tambon")
.DisplayMember = "TAMBON_TH"
.ValueMember = "TAMBONID"
.DataBindings.Clear()
.DataBindings.Add("SelectedValue", ds, "organization.TAMBONID")
End With
End If
End If
Catch ex As Exception
cboTAMBON_TH.Text = Nothing
End Try
End Sub
Private Sub cboPROVINCE_TH_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboPROVINCE_TH.SelectedValueChanged
call_amphur()
End Sub
Private Sub cboDISTRICT_TH_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboDISTRICT_TH.SelectedValueChanged
call_tambon()
End Sub
Private Sub txtSave_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSave.Click
add_state = True
Dim sqlupdate As String
Dim sqladd As String
sqlupdate = "update organization "
sqlupdate &= " set ORGNAME = '" & txtOg.Text & "',"
sqlupdate &= " HOUSE_NO= '" & txtHOUSENO.Text & "',"
sqlupdate &= " MOO= '" & txtMOO.Text & "',"
sqlupdate &= " SOINAME= '" & txtSOINAME.Text & "',"
sqlupdate &= " EMAIL= '" & txtEMAIL.Text & "',"
sqlupdate &= " ZIPCODE= '" & txtZIPCODE.Text & "',"
sqlupdate &= " FAX= '" & txtFAX.Text & "',"
sqlupdate &= " TEL= '" & txtPHONE.Text & "',"
sqlupdate &= " ROAD_NAME= '" & txtROAD.Text & "',"
sqlupdate &= " PICTURE_ADMIN3= '" & tmpFileName & "',"
sqlupdate &= " WEB= '" & txtWEB.Text & "',"
sqlupdate &= " TAMBONID='" & CStr(cboTAMBON_TH.SelectedValue) & "',"
sqlupdate &= " DISTRICTID='" & CStr(cboDISTRICT_TH.SelectedValue) & "',"
sqlupdate &= " PROVINCEID='" & CStr(cboPROVINCE_TH.SelectedValue) & "'"
sqlupdate &= " where ORGID = '" & txtORGID.Text & "'"
sqladd = "insert into organization(ORGNAME,HOUSE_NO,MOO,SOINAME,EMAIL,ZIPCODE,FAX,TEL,ROAD_NAME,PICTURE_ADMIN3,TAMBONID,DISTRICTID,PROVINCEID)"
sqladd &= " values ('" & txtOg.Text & "',"
sqladd &= "'" & txtHOUSENO.Text & "',"
sqladd &= "'" & txtMOO.Text & "',"
sqladd &= "'" & txtSOINAME.Text & "',"
sqladd &= "'" & txtEMAIL.Text & "',"
sqladd &= "'" & txtZIPCODE.Text & "',"
sqladd &= "'" & txtFAX.Text & "',"
sqladd &= "'" & txtPHONE.Text & "',"
sqladd &= "'" & txtROAD.Text & "',"
sqladd &= "'" & tmpFileName & "',"
sqladd &= "'" & CStr(cboTAMBON_TH.SelectedValue) & "',"
sqladd &= "'" & CStr(cboDISTRICT_TH.SelectedValue) & "',"
sqladd &= "'" & CStr(cboPROVINCE_TH.SelectedValue) & "')"
Try
With Conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strConnstring
.Open()
End With
user = New SqlCommand
With user
.CommandType = CommandType.Text
If add_state = True Then
.CommandText = sqladd
Else
.CommandText = sqlupdate
End If
.Connection = Conn
.ExecuteNonQuery()
End With
Catch ErrorToEdit As Exception
MessageBox.Show(ErrorToEdit.Message & sqladd, "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End Try
add_state = False
MessageBox.Show("บันทึกข้อมูลเสร็จแล้ว ", "แจ้งให้ทราบ", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
Private Sub txtadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtadd.Click
End Sub
Private Sub txtBRO_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBRO.Click
Dim ofDlg As New OpenFileDialog()
ofDlg.Title = "เลือกไฟล์รูปภาพ"
ofDlg.Filter = "Bitmap Images(.bmp)|*.bmp|Gif Images(.gif)|*.gif|Jpeg Images(.jpg)|*.jpg| Png Image(*.png)|*.png|All Files (*.*)|*.*"
If ofDlg.ShowDialog = DialogResult.OK Then
tmpFileName = ofDlg.FileName
PictureBox1.Image = Image.FromFile(tmpFileName)
End If
Refresh()
End Sub
Private Sub ShowPosition()
Try
CurrentRecord = Me.BindingContext(ds, "organization").Position + 1
'lblRecord.Text = "เร็คคอร์ด : " & CurrentRecord & " / " & RecordCount
If CurrentRecord > 0 Then
If Convert.ToString(dt.Rows(CurrentRecord - 1).Item("PICTURE_ADMIN3")) <> "" Then
PictureBox1.Image = Image.FromFile(Convert.ToString(dt.Rows(CurrentRecord - 1).Item("PICTURE_ADMIN3")))
Else
PictureBox1.Image = Nothing
End If
End If
Catch
PictureBox1.Image = Nothing
End Try
End Sub
End Class