Welcome to Dream.In.Code
Getting VB.NET Help is Easy!

Join 136,538 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,763 people online right now. Registration is fast and FREE... Join Now!




Removing record from database using VB 2008 & LINQ

 
Reply to this topicStart new topic

Removing record from database using VB 2008 & LINQ

eowend
17 Aug, 2008 - 08:22 AM
Post #1

New D.I.C Head
*

Joined: 26 Apr, 2008
Posts: 4


My Contributions
Hi there,

I am currently writing a code that queries data from a MySQL database. Once I have that data, I want to let the user delete the current record he is looking at. So what I did was to place a textbox that will let the user input any search keyword and then select any search criteria he wants from a drop down box. When the user click on the find button, this code is executed"

CODE
  

Dim SearchBy As String = cmbSearchBy.SelectedItem
Dim searchKeyWord As String = txtSearch.Text
lblResults.Text = "Searching..."

Select Case SearchBy
    Case "EmployeeName"
        ' records is a private object defined
        ' previously in the code
        records = From employee In Me.Vendor_dbDataSet.employee _
        Where employee.EmployeeName Like searchKeyWord & "*" _
        Select employee

    Case "EmployeeID"
        records = From employee In Me.Vendor_dbDataSet.employee _
        Where employee.EmployeeID Like searchKeyWord & "*" _
        Select employee
End Select

Try
    Me.EmployeeBindingSource.DataSource = records
    lblCurrentPosition.Text = Me.EmployeeBindingSource.Position + 1 & _
                              " of " & Me.EmployeeBindingSource.Count.ToString
    lblResults.Text = Me.EmployeeBindingSource.Count.ToString & " records found."
Catch ex As Exception
    lblResults.Text = "No records found."
    MessageBox.Show("No match found.", "Sorry", MessageBoxButtons.OK, _
                    MessageBoxIcon.Information)
End Try



This gives me the matches I want. They are shown one by one in the textbox, and I made a small navigation system for them. When the user finds the value he/she is looking for, he/she will have to press the delete button, and this code will be executed:

CODE
  

Try
    Dim result As MsgBoxResult
    Dim Count As String = Me.EmployeeBindingSource.Count.ToString

    result = MessageBox.Show("Are you sure you want to delete " & _
                    Count & " record(s)? This cannot be undone.", _
                    "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning)
    If result = MsgBoxResult.Yes Then
        ' This actually removes the value from the list, but apparently it
        ' does not save it to the database permanently.
        Me.Validate()
        Me.EmployeeBindingSource.RemoveCurrent()
        Me.EmployeeBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.AVendor_dbDataSet)
    End If

    MessageBox.Show("Record(s) deleted.", "Success", _
                    MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ex As Exception
    MessageBox.Show(ex.Message)
End Try



After I click on delete I get a confirmation asking if I want to do it, I go on and say yes, and the values are removed. If I search again for the same keywords, I will get the same record I just deleted. I am suspecting I am just taking some temporary value and deleting it, but I do not know how to deal with this case. I will greatly appreciate any help concerning this topic.

Thank you smile.gif
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Removing Record From Database Using VB 2008 & LINQ
17 Aug, 2008 - 01:02 PM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,947



Thanked: 42 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
Moved to VB.NET.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 10:26PM

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month