Imports System.Data
Imports System.Data.SqlClient
Public Class frmJew
Private conection As SqlConnection
Private command As SqlCommand
Private dataSt As DataSet
Private adapter As SqlDataAdapter
Private bindingSrc As BindingSource
Private sql As String
Private Sub UpdateBinding()
ComboBox11.DataBindings.Clear()
ComboBox21.DataBindings.Clear()
PictureBox11.DataBindings.Clear()
sql = "SELECT* FROM Jewelry"
command = New SqlCommand(sql, conection)
adapter = New SqlDataAdapter(command)
dataSt = New DataSet()
adapter.Fill(dataSt, "Jewelry")
bindingSrc = New BindingSource()
bindingSrc.DataSource = dataSt.Tables("Jewelry")
ComboBox11.DataBindings.Add("Text", bindingSrc, "Jew_ID")
ComboBox21.DataBindings.Add("Text", bindingSrc, "TypeJew_ID")
PictureBox11.DataBindings.Add("Image", bindingSrc, "Picture", True)
End Sub
Private Sub frmJew_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim conStr As String = "Data Source=MINI-PC;Initial Catalog=MEO;Integrated Security=True"
conection = New SqlConnection(conStr)
conection.Open()
ButtonSave.Enabled = False
ComboBox11.Items.AddRange(jew)
ComboBox11.SelectedIndex = 0
End Sub
Public Sub frmjew_FormClosing() Handles MyBase.FormClosing
conection.Close()
End Sub
Private jew() As String = {"ต่างหู", "จี่", "แหวน"}
Private ry()() As String = {New String() {"ER"},
New String() {"P"},
New String() {"R"}}
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox11.SelectedIndexChanged
Dim idy As Integer = ComboBox11.SelectedIndex
ComboBox21.Items.Clear()
ComboBox21.Items.AddRange(ry(idy))
ComboBox21.SelectedIndex = 0
End Sub
Private Sub ButtonAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonAdd.Click
ComboBox11.Text = ""
ComboBox21.Text = ""
'TextMatID.Text = ""
'TextJewID.Text = ""
PictureBox11.Image = Nothing
If ButtonAdd.Text = "เพิ่มข้อมูล" Then
ButtonAdd.Text = "ยกเลิก"
ButtonSave.Enabled = True
ElseIf ButtonAdd.Text = "ยกเลิก" Then
ButtonAdd.Text = "เพิ่มข้อมูล"
ButtonSave.Enabled = False
UpdateBinding()
End If
End Sub
Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
OpenFileDialog1.Filter = "Image File(*.jpg,*.png,*.gif,*.bmp)|*.jpg;*.png;*.gif;*.bmp"
If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
PictureBox11.Image = Bitmap.FromFile(OpenFileDialog1.FileName)
End If
End Sub
Private Sub ButtonSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSave.Click
If IsDataComplete() = False Then
Exit Sub
End If
sql = "INSERT INTO Jewelry "
If Not (PictureBox11.Image Is Nothing) Then
sql &= "(Jew_ID,TypeJew_ID,Picture)" &
"VALUES(@jew,@type,@pic)"
Else
sql &= "(Jew_ID,TypeJew_ID)" &
"VALUES(@jew,@type)"
End If
command.Parameters.Clear()
command.CommandText = sql
command.Parameters.AddWithValue("@jew", ComboBox11.Items)
command.Parameters.AddWithValue("@type", ComboBox21.Items)
If Not (PictureBox11.Image Is Nothing) Then
Dim pic() As Byte = ReadImage()
command.Parameters.AddWithValue("pic", pic)
End If
Dim result As Integer = command.ExecuteNonQuery()
If result = -1 Then
MessageBox.Show("เกิดข้อผิดพลาดไม่สามารถเพิ่มข้อมูลได้")
Else
MessageBox.Show("บันทึกข้อมูลแล้ว")
ButtonAdd.PerformClick()
End If
End Sub
Private Function ReadImage() As Byte()
Dim memStream As New IO.MemoryStream()
PictureBox11.Image.Save(memStream, Drawing.Imaging.ImageFormat.Bmp)
Return memStream.ToArray()
End Function
Private Function IsDataComplete()
ComboBox11.Text.Trim()
ComboBox21.Text.Trim()
If ComboBox11.Text = "" OrElse ComboBox21.Text = "" Then
MessageBox.Show("กรุณาใส่ข้อมูลให้ครบ")
Return False
Else
Return True
End If
End Function
Private Sub Btnupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btnupdate.Click
If ComboBox11.Text = "" Or IsDataComplete() = False Then
Exit Sub
End If
Dim s As String
If Not (PictureBox11.Image Is Nothing) Then
s = ",Picture = @pic "
Else
s = " "
End If
sql = "UPDATE Jewelry SET Jew_ID = @jew, TypeJew_ID = @type " & s &
" WHERE Jew_ID = @jew "
command.Parameters.Clear()
command.CommandText = sql
command.Parameters.AddWithValue("@Type", ComboBox11.Items)
command.Parameters.AddWithValue("@Jew", ComboBox21.Items)
command.Parameters.AddWithValue("@Type", ComboBox11.Items)
If Not (PictureBox11.Image Is Nothing) Then
Dim byteArr() As Byte = ReadImage()
command.Parameters.AddWithValue("@pic", byteArr)
End If
If command.ExecuteNonQuery() = 1 Then
MessageBox.Show("บันทึกการเปลี่ยนแปลงแล้ว")
Else
MessageBox.Show("เกิดข้อผิดพลาด")
End If
End Sub
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
If ComboBox11.Text = "" Then
Exit Sub
End If
Dim result As DialogResult =
MessageBox.Show("ท่านต้องการลบข้อมูลแถวนี้จริงหรือไม่", "ยืนยัน",
MessageBoxButtons.OKCancel)
If result = DialogResult.Cancel Then
Exit Sub
End If
sql = "DELETE FROM Jewelry WHERE TypeJew_ID =" & ComboBox11.Text
command.Parameters.Clear()
command.CommandText = sql
command.ExecuteNonQuery()
End Sub
End Class
'คือผมต้องการที่จะบันทึกข้อมูลในComboboxในฐานข้อมูล แต่มันขึ้นObject reference not set to an instance of an object.
ไม่รู้ว่าผิดพลาดตรงไหนรบกวนผู้รู้ช่วยทีคับ
Private Sub ButtonSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSave.Click
If IsDataComplete() = False Then
Exit Sub
End If
sql = "INSERT INTO Jewelry "
If Not (PictureBox11.Image Is Nothing) Then
sql &= "(Jew_ID,TypeJew_ID,Picture)" &
"VALUES(@jew,@type,@pic)"
Else
sql &= "(Jew_ID,TypeJew_ID)" &
"VALUES(@jew,@type)"
End If
command.Parameters.Clear()
command.CommandText = sql
command.Parameters.AddWithValue("@jew", ComboBox11.Items.ToString())
command.Parameters.AddWithValue("@Type", ComboBox21.Items.ToString())
'ผมแก้ตามที่บอกแล้วนะครับแต่ก็ยังเหมือนเดิมคับ ช่วยดูอีกทีนะคับ
If Not (PictureBox11.Image Is Nothing) Then
Dim pic() As Byte = ReadImage()
command.Parameters.AddWithValue("pic", pic)
End If
Dim result As Integer = command.ExecuteNonQuery()
If result = -1 Then
MessageBox.Show("เกิดข้อผิดพลาดไม่สามารถเพิ่มข้อมูลได้")
Else
MessageBox.Show("บันทึกข้อมูลแล้ว")
ButtonAdd.PerformClick()
End If
End Sub