Imports System.Data
Imports System.Data.SqlClient
Public Class frmDevice
Private Mycommand As SqlCommand
Private dataSt As DataSet
Private adapter As SqlDataAdapter
Private bindingSrc As BindingSource
Private sql As String
Private Sub frmDevice_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim conStr As String = "Data Source=.\SQLEXPRESS;" &
"AttachDbFilename=|DataDirectory|\iConWorkDATA.mdf" &
"Integrated Security=True;User Instance=True"
Dim connection As SqlConnection
connection = New SqlConnection(conStr)
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub btnSaveAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveAdd.Click
If IsDataComplete() = False Then
Exit Sub
End If
sql = "INSERT INTO Device"
sql = "(DeID, DeAsset, DeDv, Brand, Model, SnNo, WarY, EndWar, WarSub, Detail)"
sql = "VALUES(@devid, @devdv, @devbrand, @devmodel, @devsn, @devwary, @devendW, @devwars, @devdet"
AddParameters(sql)
Dim r As Integer = Mycommand.ExecuteNonQuery()
If r > 0 Then
MessageBox.Show("บันทึกข้อมูลเรียบร้อยแล้ว")
btnSaveAdd.PerformClick()
Else
MessageBox.Show("เกิดข้อผิดพลาดในการบันทึกข้อมูล")
End If
End Sub
Private Sub AddParameters(ByVal sql As String)
Mycommand.Parameters.Clear() <<<<<<<<<<<<<<<<<<<<<<< ติดบรรทัดนี้
Mycommand.CommandText = sql
Mycommand.Parameters.AddWithValue("DeID", txtDevID.Text)
Mycommand.Parameters.AddWithValue("DeAsset", txtDevAsset.Text)
Mycommand.Parameters.AddWithValue("DeDv", txtDev.Text)
Mycommand.Parameters.AddWithValue("Brand", txtDevBrand.Text)
Mycommand.Parameters.AddWithValue("Model", txtDevModel.Text)
Mycommand.Parameters.AddWithValue("SnNo", txtDevSn.Text)
Mycommand.Parameters.AddWithValue("WarY", txtWarr.Text)
Dim dateStr As String = dtpDevWarDate.Value.Year
dateStr &= "/" & dtpDevWarDate.Value.Month
dateStr &= "/" & dtpDevWarDate.Value.Day
Mycommand.Parameters.AddWithValue("EndWar", dateStr)
Mycommand.Parameters.AddWithValue("WarSub", txtDevSub.Text)
Mycommand.Parameters.AddWithValue("Detail", txtDevDetail.Text)
End Sub
Private Function IsDataComplete() As Boolean
txtDevID.Text.Trim()
txtDev.Text.Trim()
txtDevBrand.Text.Trim()
txtDevModel.Text.Trim()
Dim errMsg As String = ""
If txtDevID.Text = "" Then
errMsg = "ยังไม่ได้ระบุรหัสเครื่อง"
ElseIf txtDev.Text = "" Then
errMsg = "ยังไม่ได้ระบุประเภทอุปกรณ์"
ElseIf txtDevBrand.Text = "" Then
errMsg = "ยังไม่ได้ระบุยี่ห้ออุปกรณ์"
ElseIf txtDevModel.Text = "" Then
errMsg = "ยังไม่ได้รุ่นของอุปกรณ์"
End If
If errMsg <> "" Then
MessageBox.Show(errMsg)
Return False
Else
Return True
End If
End Function
End Class