Dim fd As OpenFileDialog = New OpenFileDialog()
Dim strFileName As String
fd.Title = "Open File Dialog"
fd.InitialDirectory = "C:\"
fd.Filter = "All files (*.*)|*.*|All files (*.*)|*.*"
fd.FilterIndex = 2
fd.RestoreDirectory = True
If fd.ShowDialog() = DialogResult.OK Then
strFileName = fd.FileName
// Insert to Database
End If
If TextBox4.Text = "" Then
MessageBox.Show("กรุณาเลือกไฟล์เอกสารที่ต้องการบันทึก", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
Try
Select Case Action
Case "ADD" 'โหมดเพิ่มข้อมูล
sb.Append(" INSERT INTO pay(pa_id,pa_date,pa_sub,pa_num,pa_sum,pa_docname,pa_docfile)")
sb.Append(" VALUES (@pa_id,@pa_date,@pa_sub,@pa_num,@pa_sum,@pa_docname,@pa_docfile)")
Case "EDIT" 'โหมดแก้ไขข้อมูล
sb.Append(" UPDATE pay")
sb.Append(" SET pa_date=@pa_date,")
sb.Append(" pa_sub=@pa_sub,")
sb.Append(" pa_num=@pa_num,")
sb.Append(" pa_sum=@pa_sum,")
sb.Append(" pa_docname=@pa_docname,")
sb.Append(" pa_docfile=@pa_docfile")
sb.Append(" WHERE pa_id=@pa_id")
End Select
Dim sqlsave As String
sqlsave = sb.ToString
With com
.CommandType = CommandType.Text
.CommandText = sqlsave
.Transaction = st
.Parameters.Clear()
.Parameters.AddWithValue("@pa_id", SqlDbType.VarChar).Value = TextBox1.Text.Trim()
.Parameters.AddWithValue("@pa_date", SqlDbType.DateTime).Value = DateTimePicker1.Value
If RadioButton1.Checked = True Then
.Parameters.AddWithValue("@pa_sub", SqlDbType.NVarChar).Value = "ซื้อ"
Else
.Parameters.AddWithValue("@pa_sub", SqlDbType.NVarChar).Value = "จ้าง"
End If
.Parameters.AddWithValue("@pa_num", SqlDbType.VarChar).Value = TextBox3.Text.Trim()
.Parameters.AddWithValue("@pa_sum", SqlDbType.VarChar).Value = TextBox5.Text.Trim()
.Parameters.AddWithValue("@pa_docname", SqlDbType.NVarChar).Value = TextBox2.Text.Trim()
Dim fr As New FileStream(TextBox4.Text, FileMode.Open, FileAccess.Read)
Dim byteArray(CInt(fr .Length - 1)) As Byte
Dim binreader As New BinaryReader(fr)
Dim File() As Byte = binreader.ReadBytes(CInt(fr.Length))
.Parameters.AddWithValue("@pa_docfile", SqlDbType.VarBinary).Value = File
.ExecuteNonQuery()
MessageBox.Show("บันทึกข้อมูล " & TextBox1.Text & " เรียบร้อยแล้ว !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
End With
Catch ex As Exception
MessageBox.Show("เลขการขอเบิก " & TextBox1.Text & " ที่คุณป้อน ซ้ำกับเลขการขอเบิก เดิมที่มีอยู่" + ex.Message, "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Information)
TextBox1.Focus()
TextBox1.SelectAll()
End Try
End If
With OpenFileDialog1
.Filter = "PDF Files(*.pdf)|*.pdf"
.FileName = ""
If (.ShowDialog() = DialogResult.OK) Then
TextBox4.Text = .FileName
End If
Process.Start(.FileName)
End With