setting Withblock variable or object variable in VB 2015 - vb.net

Public Class Form1
Private ContactsTableBindingSource As Object
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'ContactsTableDataSet.Table' table. You can move, or remove it, as needed.
Me.TableTableAdapter.Fill(Me.ContactsTableDataSet.Table)
If ComboBox1.Text = Nothing Then
Try
ContactsTableBindingSource.AddNew()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If First_NameTextBox.Text = Nothing Then
First_NameTextBox.Text = "unknown"
End If
If Last_NameTextBox.Text = Nothing Then
Last_NameTextBox.Text = "unknown"
End If
If AddressTextBox.Text = Nothing Then
AddressTextBox.Text = "unknown"
End If
If Phone_NumberTextBox.Text = Nothing Then
Phone_NumberTextBox.Text = "unknown"
End If
If Email_AddressTextBox.Text = Nothing Then
Email_AddressTextBox.Text = "unknown"
End If
Try
Me.Validate()
Me.ContactsTableBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.ContactsTableDataSet)
MessageBox.Show("The data has been saved", "Information", MessageBoxButtons.OK)
ContactsTableBindingSource.AddNew()
First_NameTextBox.Select()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Please suggest a correction in the code as I have being showing the error "object variable or Withblock not set"

Related

How to delete a file used by another Process

Public Class frmMainCetegory
Private Sub frmMainCetegory_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'PosDatabaseDataSet.tblItemMainCategory' table. You can move, or remove it, as needed.
Me.TblItemMainCategoryTableAdapter.Fill(Me.PosDatabaseDataSet.tblItemMainCategory)
Me.CenterToParent()
Me.btnAddNew.Select()
End Sub
Private Sub btnAddNew_Click(sender As Object, e As EventArgs) Handles btnAddNew.Click
Try
Me.TblItemMainCategoryBindingSource.AddNew()
Me.txtMainCategory.Select()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Try
If Me.txtMainCategory.Text = "" Then
MsgBox("Please Enter Main Category Name!", vbInformation)
Exit Sub
End If
Me.imgMainCategoryImage.Image.Save(Application.StartupPath & "\Images\" & Me.txtMainCategory.Text & ".jpeg", System.Drawing.Imaging.ImageFormat.Jpeg)
Me.TblItemMainCategoryBindingSource.EndEdit()
Me.TblItemMainCategoryTableAdapter.Update(Me.PosDatabaseDataSet.tblItemMainCategory)
Me.TblItemMainCategoryTableAdapter.Fill(Me.PosDatabaseDataSet.tblItemMainCategory)
Me.btnAddNew.Select()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
Try
Dim MainCatName As String
MainCatName = Me.TblItemMainCategoryDataGridView.CurrentRow.Cells("MainCategoryName").Value
If Me.TblItemMainCategoryDataGridView.Rows.Count - 1 > 0 Then
'check before delting, if this main category is used in sub category then It can not be deleted.
Dim I As Integer
I = frmSubCategories.TblItemSubCategoryTableAdapter.qryCountMainCategoryUnderSubCategory(MainCatName)
If I > 0 Then
MsgBox(MainCatName & " is used under Sub Category(s), Please Delete Sub Category(s) First!", vbOK)
Exit Sub
End If
End If
Me.TblItemMainCategoryBindingSource.RemoveCurrent()
Me.TblItemMainCategoryTableAdapter.Update(Me.PosDatabaseDataSet.tblItemMainCategory)
Me.TblItemMainCategoryTableAdapter.Fill(Me.PosDatabaseDataSet.tblItemMainCategory)
'Delete the Main Category Image if this exists
If System.IO.File.Exists(Application.StartupPath & "\Images\" & MainCatName & ".JPEG") Then
System.IO.File.Delete(Application.StartupPath & "\Images\" & MainCatName & ".JPEG")
MsgBox("Image Deleted Successfull")
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub btnBrowse_Click(sender As Object, e As EventArgs) Handles btnBrowse.Click
Try
Me.OpenFileDialog1.Filter = "Bitmaps(*.JPG, *.PNG, *.JPEG, *.BMP, *.TIF, *.GIF, *.TIFF)|"
Me.OpenFileDialog1.FileName = ""
If OpenFileDialog1.ShowDialog(Me) = DialogResult.OK Then
Dim img As String = Me.OpenFileDialog1.FileName
Me.imgMainCategoryImage.Image = System.Drawing.Bitmap.FromFile(img)
Me.btnSave.Select()
Else
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub TblItemMainCategoryDataGridView_SelectionChanged(sender As Object, e As EventArgs) Handles TblItemMainCategoryDataGridView.SelectionChanged
Try
Me.imgMainCategoryImage.Image = DirectCast(Image.FromFile(Application.StartupPath & "\Images\" & Me.TblItemMainCategoryDataGridView.CurrentRow.Cells("MainCategoryName").Value & ".JPEG").Clone(), Image)
' Me.imgMainCategoryImage.Image = System.Drawing.Bitmap.FromFile(Application.StartupPath & "\Images\" & Me.TblItemMainCategoryDataGridView.CurrentRow.Cells("MainCategoryName").Value & ".JPEG")
Catch ex As Exception
Me.imgMainCategoryImage.Image = My.Resources.imgEmpty 'if image path not found then select empty image
End Try
End Sub
Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
Me.Close()
End Sub
End Class
Above code give me an error message:
image is used in another process, can't be deleted
when I delete the image file. The image is assigned to a PictureBox with selection event of the DataGridView. Since I have deleted the DataGridView row, as shown in the above code, it changes the image in the PictureBox.
Here I am unable to delete the image file related to the removed row.
Please guide and let me know if you need any further details.

Deletion of image from folder that is referenced to picture box

sorry i have to rewrite this question as i did not get any update on my same question posted previously.
Below is the code where I'm inserting some data into Ms Access database in visual studio using vb. Net.
When i insert the data i also browse the image for that item and save into the folder that is located under project directory for universal access.
There is a datagridview which displays the data which was inserted to access databas alog with the image in picture box. Whenever the the datagrid view selection is change it display the picure into picture box.
I am having a trouble when i want to delete the data along with image located in project folder as it show the error that 'file used by another process, cant be deleted'
Since i have referenced that image to my picture box, it can not be deleted directly. So here I'm looking for a solution which can display the clone of image in picture box instead of real object.
Below is my code
Public Class frmMainCetegory
Private Sub frmMainCetegory_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'PosDatabaseDataSet.tblItemMainCategory' table. You can move, or remove it, as needed.
Me.TblItemMainCategoryTableAdapter.Fill(Me.PosDatabaseDataSet.tblItemMainCategory)
Me.CenterToParent()
Me.btnAddNew.Select()
End Sub
Private Sub btnAddNew_Click(sender As Object, e As EventArgs) Handles btnAddNew.Click
Try
Me.TblItemMainCategoryBindingSource.AddNew()
Me.txtMainCategory.Select()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Try
If Me.txtMainCategory.Text = "" Then
MsgBox("Please Enter Main Category Name!", vbInformation)
Exit Sub
End If
Me.imgMainCategoryImage.Image.Save(Application.StartupPath & "\Images\" & Me.txtMainCategory.Text & ".jpeg", System.Drawing.Imaging.ImageFormat.Jpeg)
Me.TblItemMainCategoryBindingSource.EndEdit()
Me.TblItemMainCategoryTableAdapter.Update(Me.PosDatabaseDataSet.tblItemMainCategory)
Me.TblItemMainCategoryTableAdapter.Fill(Me.PosDatabaseDataSet.tblItemMainCategory)
Me.btnAddNew.Select()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
Try
Dim MainCatName As String
MainCatName = Me.TblItemMainCategoryDataGridView.CurrentRow.Cells("MainCategoryName").Value
If Me.TblItemMainCategoryDataGridView.Rows.Count - 1 > 0 Then
'check before delting, if this main category is used in sub category then It can not be deleted.
Dim I As Integer
I = frmSubCategories.TblItemSubCategoryTableAdapter.qryCountMainCategoryUnderSubCategory(MainCatName)
If I > 0 Then
MsgBox(MainCatName & " is used under Sub Category(s), Please Delete Sub Category(s) First!", vbOK)
Exit Sub
End If
End If
Me.TblItemMainCategoryBindingSource.RemoveCurrent()
Me.TblItemMainCategoryTableAdapter.Update(Me.PosDatabaseDataSet.tblItemMainCategory)
Me.TblItemMainCategoryTableAdapter.Fill(Me.PosDatabaseDataSet.tblItemMainCategory)
'Delete the Main Category Image if this exists
If System.IO.File.Exists(Application.StartupPath & "\Images\" & MainCatName & ".JPEG") Then
System.IO.File.Delete(Application.StartupPath & "\Images\" & MainCatName & ".JPEG")
MsgBox("Image Deleted Successfull")
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub btnBrowse_Click(sender As Object, e As EventArgs) Handles btnBrowse.Click
Try
Me.OpenFileDialog1.Filter = "Bitmaps(*.JPG, *.PNG, *.JPEG, *.BMP, *.TIF, *.GIF, *.TIFF)|"
Me.OpenFileDialog1.FileName = ""
If OpenFileDialog1.ShowDialog(Me) = DialogResult.OK Then
Dim img As String = Me.OpenFileDialog1.FileName
Me.imgMainCategoryImage.Image = System.Drawing.Bitmap.FromFile(img)
Me.btnSave.Select()
Else
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub TblItemMainCategoryDataGridView_SelectionChanged(sender As Object, e As EventArgs) Handles TblItemMainCategoryDataGridView.SelectionChanged
Try
Me.imgMainCategoryImage.Image = DirectCast(Image.FromFile(Application.StartupPath & "\Images\" & Me.TblItemMainCategoryDataGridView.CurrentRow.Cells("MainCategoryName").Value & ".JPEG").Clone(), Image)
' Me.imgMainCategoryImage.Image = System.Drawing.Bitmap.FromFile(Application.StartupPath & "\Images\" & Me.TblItemMainCategoryDataGridView.CurrentRow.Cells("MainCategoryName").Value & ".JPEG")
Catch ex As Exception
Me.imgMainCategoryImage.Image = My.Resources.imgEmpty 'if image path not found then select empty image
End Try
End Sub
Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
Me.Close()
End Sub
Private Sub Label6_Click(sender As Object, e As EventArgs) Handles Label6.Click
frmTable.Show()
End Sub
End Class

Get Combobox Value in VS

I am making a simple pricing calculator by using a Combobox and Label Text.
My Combobox has 2 items: Weekend (30.000) and Weekday (20.000). While the multiplier is quantity.
So if I choose "Weekend (30.000)" in the combobox and input "2" to qty, the result would be 30.000 * 2 = 60.000.
I tried this code but could not work.
I wonder how do I get the value of the strings from "Weekend (30.000)" to 30000?
Private Sub txtQty_TextChanged(sender As Object, e As EventArgs) Handles txtQty.TextChanged
Try
lblFinal.Text = cboPrice.SelectedItem * txtQty.Text
Catch ex As Exception
End Try
End Sub
Private Sub cboPrice_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboPrice.SelectedIndexChanged
Try
If cboPrice.SelectedIndex = 1 Then
cboPrice.SelectedItem = "30000"
ElseIf cboPrice.SelectedIndex = 2 Then
cboPrice.SelectedText = "40000"
End If
Catch ex As Exception
End Try
End Sub
I tried again using this code and it worked. . .
Dim price as integer
Private Sub txtQty_TextChanged(sender As Object, e As EventArgs) Handles txtQty.TextChanged
Try
lblFinal.Text = Price * CInt(txtQty.Text)
Catch ex As Exception
End Try
End Sub
Private Sub cboPrice_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboPrice.SelectedIndexChanged
Try
If cboPrice.SelectedIndex = 0 Then
Price = 30000
Else
Price = 40000
End If
Catch ex As Exception
End Try
End Sub

Drag and Drop in datagridview in vb.net

I am using two Datagridview in my code, i drag content from Me.datagridview2 and drop it on Me.datagridview1.This process is successful. But as soon as i click the cell other than the dropped content cell, the dropped content disappears. Here's my code
Private Sub DataGridView2_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles DataGridView2.MouseDown
Try
If Me.DataGridView2.SelectedRows.Count = 0 Then
Exit Sub
End If
Me.DataGridView2.DoDragDrop(Me.DataGridView2.SelectedRows(0), DragDropEffects.All)
Catch ex As Exception
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub DataGridView1_DragDrop(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles DataGridView1.DragDrop
Try
Dim r As DataGridViewRow = e.Data.GetData(GetType(DataGridViewRow))
If Me.DataGridView1.SelectedRows.Count = 0 Then
Exit Sub
End If
Dim i As Integer = Me.DataGridView1.SelectedRows(0).Index
Me.DataGridView1.Rows(i).Cells(1).Value = r.Cells(0).Value
Me.DataGridView1.Rows(i).Cells(2).Value = r.Cells(1).Value
Me.DataGridView1.Rows(i).Cells(3).Value = r.Cells(2).Value
Me.DataGridView1.Rows(i).Cells(4).Value = r.Cells(3).Value
Catch ex As Exception
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub DataGridView1_DragEnter(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles DataGridView1.DragEnter
Try
e.Effect = DragDropEffects.Copy
Catch ex As Exception
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Have following doubts
1. why does the dropped content disappears.
2. whenever we begin edit on datagridview, a row in automatically added below. Why it doesn't gets added when i drop content in datagridview.?
Please help me.
Actually, i just got an alternative to my own question. Here's it.
Private Sub DataGridView1_DragDrop(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles DataGridView1.DragDrop
Try
Dim r As DataGridViewRow = e.Data.GetData(GetType(DataGridViewRow))
If Me.DataGridView1.SelectedRows.Count = 0 Then
Exit Sub
End If
Dim i As Integer = Me.DataGridView1.SelectedRows(0).Index
dragseldet.Tables(0).Rows.Add("", r.Cells(0).Value, r.Cells(1).Value, r.Cells(2).Value, r.Cells(3).Value, 0, 0)
dragseldet.AcceptChanges()
'Me.DataGridView1.Rows(i).Cells(1).Value = r.Cells(0).Value
'Me.DataGridView1.Rows(i).Cells(2).Value = r.Cells(1).Value
'Me.DataGridView1.Rows(i).Cells(3).Value = r.Cells(2).Value
'Me.DataGridView1.Rows(i).Cells(4).Value = r.Cells(3).Value
Catch ex As Exception
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Rather than copying the contents from r row to Me.DataGridview. all cells , i am directly adding r row to my datasource named dragsaldet . And that did the trick for me.

Why isn't my vb.net code saving a row to my Access database? (Error)

I'm trying to add a 'coupling' to my database with the following code. It give me the error "Update requires a valid InsertCommand when passed DataRow collection with new rows."
Private Sub AddCoupling_Load(sender As Object, e As EventArgs) Handles MyBase.Load
CouplingsTableAdapter.Fill(TestDatabaseDataSet.Couplings)
End Sub
Private Sub ButtonCancel_Click(sender As Object, e As EventArgs) Handles ButtonClose.Click
Close()
End Sub
Private Sub ButtonSave_Click(sender As Object, e As EventArgs) Handles ButtonSave.Click
Dim newCoup As DataRow = TestDatabaseDataSet.Tables("Couplings").NewRow
newCoup.Item("Type") = ComboBoxType.Text
newCoup.Item("Part Number") = TextPartNumber.Text
newCoup.Item("Description") = TextDescription.Text
newCoup.Item("Rubber Type") = ComboBoxRubberType.Text
newCoup.Item("TKN") = Integer.Parse(TextTKN.Text)
newCoup.Item("TKmax") = Integer.Parse(TextTKmax.Text)
newCoup.Item("TKW") = Integer.Parse(TextTKW.Text)
newCoup.Item("Ambient Temp") = Integer.Parse(TextTemp.Text)
newCoup.Item("PKW") = Integer.Parse(TextPKW.Text)
newCoup.Item("Max Speed") = Integer.Parse(TextSpeed.Text)
Try
TestDatabaseDataSet.Tables("Couplings").Rows.Add(newCoup)
CouplingsTableAdapter.Adapter.Update(TestDatabaseDataSet) ' ERROR HERE '
MessageBox.Show("Saved!")
Close()
Catch ex As Exception
MessageBox.Show("Failed to save to database. E:" & ex.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub