Hi everyone,
I am still new in visual basic; so, please go slow on me.
I am actually having problem with the mentioned subject.
I wanted to run a form that has a flowerID(text), FlowerName(text), FlowerMeaning(text), and FlowerPicture(PictureBox) that can be viewed, update, and delete.
My problem is that I can't display the database out and have the correct syntax for the picturebox. By the way, I am not using any datasets,dataAdapter, and datagrid because I want to make it simple by using a direct connection.
Here is my current code:
The attached file is my database that may probably need some adjustment.
Please enlighten me.
- Imports System.Data.OleDb
- Public Class FlowerData
- Public connString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=FlowerDB.accdb"
- Dim conn As New OleDbConnection
- Dim myqry As String = Nothing
- Dim mycmd As New OleDbCommand
- Dim mydr As OleDbDataReader
- Dim searching As Boolean
- Dim str As String = ""
- Private Sub FlowerData_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
- Call ConnToDB()
- Call FillData()
- Call Set1()
- End Sub
- #Region "Connection"
- Sub ConnToDB()
- Try
- With conn
- .ConnectionString = connString
- .Open()
- End With
- Catch ex As Exception
- MessageBox.Show("Unable to connect", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
- Application.Exit()
- End Try
- End Sub
- #End Region
- #Region "object Settings "
- Sub Set1()
- btnAdd.Enabled = True
- btnEdit.Enabled = True
- btnDelete.Enabled = True
- btnExit.Enabled = True
- btnsearch.Enabled = True
- btnSave.Enabled = False
- btnCancel.Enabled = False
- txtFlowerID.Enabled = False
- txtFlowerName.Enabled = False
- txtFlowerMeaning.Enabled = False
- picBox.Enabled = False
- txtSearch.Enabled = True
- End Sub
- Sub Set2()
- btnAdd.Enabled = False
- btnEdit.Enabled = False
- btnDelete.Enabled = False
- btnExit.Enabled = False
- btnsearch.Enabled = False
- btnSave.Enabled = True
- btnCancel.Enabled = True
- txtFlowerID.Enabled = True
- txtFlowerName.Enabled = True
- txtFlowerMeaning.Enabled = True
- picBox.Enabled = True
- txtSearch.Enabled = False
- End Sub
- #End Region
- #Region "CLEAR TEXTBOX"
- Sub ClearAlltextBox()
- txtFlowerName.Text = ""
- txtFlowerMeaning.Text = ""
- picBox.Image = Nothing
- End Sub
- #End Region
- #Region "CLEAR TEXTBOX"
- Sub FillData()
- Dim LoadQry As String = "Select * from tblFlower Where FlowerID = " "'1001'"
- mycmd = New OleDbCommand
- mycmd = New OleDbCommand
- With mycmd
- .CommandText = LoadQry
- .Connection = conn
- .ExecuteNonQuery()
- End With
- mydr.Read()
- txtFlowerName.Text = mydr(1)
- txtFlowerMeaning.Text = mydr(2)
- picBox.Image = mydr(3)
- End Sub
- #End Region
- Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
- Me.Hide()
- End Sub
- Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
- If txtFlowerID.Text = "" Then
- MessageBox.Show("Please Select Record to Update", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
- Else
- str = "edit"
- Call Set2()
- txtFlowerID.Enabled = False
- picBox.Image = Nothing
- End If
- End Sub
- Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
- If txtFlowerID.Text = "" Then
- MessageBox.Show("Please Select Record to Update", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
- Else
- str = "edit"
- Call Set2()
- txtFlowerID.Enabled = False
- End If
- End Sub
- Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
- Call Set1()
- Call ClearAlltextBox()
- End Sub
- Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
- If str = "add" Then
- ''''''ADD NEW RECORD'''''''
- If txtFlowerName.Text = "" Or txtFlowerMeaning.Text = "" Or picBox.Visible = Nothing Then
- MessageBox.Show("All fields Are Required", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
- Else
- myqry = "INSERT INTO TblFlower(Flower Name,Flower Meaning,Flower image) "
- myqry = myqry + "VALUES('" & txtFlowerName.Text & "','" & txtFlowerMeaning.Text & "','" & picBox.Visible & "')"
- mycmd = New OleDbCommand
- With mycmd
- .CommandText = myqry
- .Connection = conn
- .ExecuteNonQuery()
- End With
- Call Set1()
- End If
- Else
- ''''''''''UPDATE RECORD'''''''
- If txtFlowerName.Text = "" Or txtFlowerMeaning.Text = "" Or picBox.Visible = Nothing Then
- MessageBox.Show("All fields Are Required", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
- Else
- myqry = "UPDATE TblFlower SET "
- myqry = myqry + " Flower Name = '" & txtFlowerName.Text & "',"
- myqry = myqry + " Flower Meaning = '" & txtFlowerMeaning.Text & "',"
- myqry = myqry + " Flower image = '" & picBox.Visible & "''"
- myqry = myqry + " WHERE "
- myqry = myqry + " FlowerID = " & txtFlowerID.Text
- mycmd = New OleDbCommand(myqry, conn)
- mycmd.ExecuteNonQuery()
- Call Set1()
- End If
- End If
- Call ClearAlltextBox()
- End Sub
- End Class
- 23 views