มันขึ้น Object reference not set to an instance of an object.
ที่ dt = DBObject.GetData(SQL) ในบรรทัดที่089ของโค้ดอะครับ รบกวนช่วยชี้ทางให้หน่อยครับ
อันนี้โค้ด Code (VB.NET)
Imports System.Data
Imports System.Data.OleDb
Imports System.IO
Public Class cusadd
#Region "Declaration"
Dim SQL As String = ""
#End Region
Dim Conn As New OleDbConnection
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.loaddata()
Me.SetEnableText(False)
End Sub
Private Sub cmdsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdadd.Click
If Me.txtidcus.Text.Length = 0 Then
MessageBox.Show("กรุณาป้อนรหัสบูกค้าก่อน!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
txtidcus.Focus()
End If
If txtname.Text.Length = 0 Then
MessageBox.Show("กรุณาป้อนชื่อลูกค้าก่อน!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
txtname.Focus()
End If
If txtidcard.Text.Length = 0 Then
MessageBox.Show("กรุณาป้อนรหัสบัตรประชาชนก่อน!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
txtidcard.Focus()
End If
If txtaddress.Text.Length = 0 Then
MessageBox.Show("กรุณาป้อนความสูงก่อน!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
txtaddress.Focus()
End If
If txtage.Text.Length = 0 Then
MessageBox.Show("กรุณาป้อนอายุก่อน!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
txtage.Focus()
End If
SQL = "SELECT * FROM cs WHERE id_cus ='" & Me.txtidcus.Text & "'"
If DBObject.GetDataScalar(SQL) > 0 Then
SQL = " UPDATE cs SET "
SQL &= " name = '" & Me.txtname.Text & "'"
SQL &= ",idcard ='" & Me.txtidcard.Text & "'"
SQL &= ",address ='" & Me.txtaddress.Text & "'"
SQL &= ",age ='" & Me.txtage.Text & "'"
SQL &= ",tel ='" & Me.txttel.Text & "'"
SQL &= " WHERE id_cus ='" & Me.txtidcus.Text & "'"
Else
SQL = "INSERT INTO cs (id_cus,name,idcard,address,age,tel) VALUES "
SQL &= " ('" & Me.txtidcus.Text & "'"
SQL &= " ,'" & Me.txtname.Text & "'"
SQL &= " ,'" & Me.txtidcard.Text & "'"
SQL &= " ,'" & Me.txtaddress.Text & "'"
SQL &= " ,'" & Me.txtage.Text & "'"
SQL &= " ,'" & Me.txttel.Text & "'"
End If
If DBObject.ExecuteNonQuery(SQL) > 0 Then
MessageBox.Show("บ้นทึกเรียบร้อย", "Save")
Me.loaddata()
End If
End Sub
#Region "Function"
Sub SetClearText()
Me.txtidcus.Text = ""
Me.txtname.Text = ""
Me.txtidcard.Text = ""
Me.txtaddress.Text = ""
Me.txtage.Text = ""
Me.txttel.Text = ""
End Sub
Sub SetEnableText(ByVal IsEnable As Boolean)
Me.txtidcus.Enabled = IsEnable
Me.txtname.Enabled = IsEnable
Me.txtidcard.Enabled = IsEnable
Me.txtaddress.Enabled = IsEnable
Me.txtage.Enabled = IsEnable
Me.txttel.Enabled = IsEnable
End Sub
Sub LoadData()
SQL = "SELECT * FROM cs"
Dim dt As DataSet
dt = DBObject.GetData(SQL)
If dt.Tables.Count > 0 Then
Me.dgvcustomerlist.DataSource = dt.Tables(0)
End If
End Sub
#End Region
Private Sub cmdedit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdedit.Click
If Me.txtidcus.Text = "" Then
MessageBox.Show("กรุณาเลือกลูกค้าที่ต้องการแก้ไข", "Edit")
Exit Sub
End If
Me.SetEnableText(True)
Me.txtidcus.Enabled = False
End Sub
Private Sub cmddelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmddelete.Click
If Me.txtidcus.Text = "" Then
MessageBox.Show("กรุณาเลือกลูกค้าที่ต้องการลบ", "Edit")
Exit Sub
End If
If MessageBox.Show("คุณแน่ใจหรือที่จะลบลูกค้า" & Me.txtidcus.Text, "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.Yes Then
SQL = "DELETE FROM cs WHERE id_cus = '" & Me.txtidcus.Text & "'"
If DBObject.ExecuteNonQuery(SQL) > 0 Then
MessageBox.Show("ลบข้อมูลเรียบร้อยแล้ว", "Delete")
Me.LoadData()
Me.SetClearText()
End If
End If
End Sub
End Class
อันนี้คลาสdb Code (VB.NET)
Namespace DATA
Public Class DBtc01
#Region "Declaration"
Private ConnectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=d:\Tc\TC01.accdb;"
Private ConnectionDBtc01 As System.Data.OleDb.OleDbConnection
#End Region
#Region "Functions"
Function Open() As Boolean
Try
ConnectionDBtc01 = New System.Data.OleDb.OleDbConnection
With ConnectionDBtc01
.ConnectionString = ConnectionString
.Open()
Return True
End With
Catch ex As Exception
MessageBox.Show(ex.Message, "Connection")
Return False
End Try
End Function
Function ExecuteNonQuery(ByVal SQL As String) As Integer
'insert ,update
Dim int As Integer
Try
Dim CMD As New System.Data.OleDb.OleDbCommand
CMD.Connection = ConnectionDBtc01
CMD.CommandType = CommandType.Text
CMD.CommandText = SQL
int = CMD.ExecuteNonQuery()
Return int
Catch ex As Exception
MessageBox.Show(ex.Message, "ExecuteNonQuery")
Return False
End Try
End Function
Function GetData(ByVal SQL As String) As DataSet
'select data
Try
Dim DT As New DataSet
Dim cmd As New System.Data.OleDb.OleDbCommand
cmd.Connection = ConnectionDBtc01
cmd.CommandType = CommandType.Text
cmd.CommandText = SQL
Dim DA As New System.Data.OleDb.OleDbDataAdapter(cmd)
DA.Fill(DT, "dd")
Return DT
Catch ex As Exception
MessageBox.Show(ex.Message)
Return Nothing
End Try
End Function
Function GetDataScalar(ByVal SQL As String) As Object
'select data one record and one field
Try
Dim re As Object
Dim CMD As New System.Data.OleDb.OleDbCommand
CMD.Connection = ConnectionDBtc01
CMD.CommandText = SQL
re = CMD.ExecuteScalar
Return re
Catch ex As Exception
MessageBox.Show(ex.Message)
Return Nothing
End Try
End Function
#End Region
End Class
End Namespace
อันนี้โมดูลครับ Code (VB.NET)
Module Module1
Public DBObject As DATA.DBtc01
Function StringConvert(ByVal txt As Object)
Dim re As String = ""
Try
If txt Is Nothing Then
re = ""
ElseIf IsDBNull(txt) Then
re = ""
Else
re = txt.ToString
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Return re
End Function
End Module