4 Replies - 1011 Views - Last Post: 25 July 2012 - 10:54 PM Rate Topic: -----

#1 mashalyy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 25-July 12

deleting a record based on name from aceess db

Posted 25 July 2012 - 05:59 AM

Hi.
I want to delete a particular record based on name from access database in vb.net

db name: stpl
table name: equipments
field: Mandal, sal, Date
eg: Pune, 2000, 23/7/12
chennai, 1200, 11/5/12


If i want to delete pune from my record, I just want to press delete button, it should pop-up msgbox asking me to enter Mandal name, And when i enter mandal name: pune, then it shud check with the db whether the record exists, if yes then shud delete it otherwise it shud say:"no record exists".

My code is: here I used textbox, actually i want to use pop-up box.
Iam getting error: object ref set not to instance of an object.


Try
            Mycn.Open()
            Dim command As String
            command = "delete from Equipments where Mandal='" & txtMan.Text & "';"
            myDA.DeleteCommand.CommandText = command
            myDA.DeleteCommand.ExecuteNonQuery()
            MessageBox.Show("Record deleted")
        Catch ex As Exception
            MessageBox.Show("no record")
        Finally
            Mycn.Close()
        End Try


I tried this code also:

Try
            Mycn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & System.Environment.CurrentDirectory & "\STPL.accdb")
            Dim sql As String
            sql = "Delete from Equipments where Mandal=" & Convert.ToString(txtMan.Text) & ""
            Mycn.Open()
            command = New OleDbCommand(sql, Mycn)
            command.ExecuteNonQuery()
            MessageBox.Show("Record Deleted Successfully", "Deleting Employee Details", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Mycn.Close()
            command.Dispose()
            ClearTextBox(Me)
            txtMan.Focus()
        Catch ex As OleDbException
            MessageBox.Show("Please enter Name only", "Deleting Employee Details", MessageBoxButtons.OK, MessageBoxIcon.Error)
            txtMan.Focus()
        Catch ex As Exception
            MessageBox.Show("Please enter Name only", "Deleting Employee Details", MessageBoxButtons.OK, MessageBoxIcon.Error)
           
 txtMan.Focus()
        End Try


But no use.........
help me out

This post has been edited by Atli: 25 July 2012 - 06:52 AM
Reason for edit:: Please use [code] tags when posting code.


Is This A Good Question/Topic? 0
  • +

Replies To: deleting a record based on name from aceess db

#2 _HAWK_   User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1162
  • View blog
  • Posts: 4,444
  • Joined: 02-July 08

Re: deleting a record based on name from aceess db

Posted 25 July 2012 - 07:21 AM

It is great to use Try Catch blocks and you need to make use of there exception messages. See if it shows you any errors.

Catch ex As Exception
Debug.Writeline(ex.ToString)
...

Was This Post Helpful? 0
  • +
  • -

#3 S-John   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 51
  • Joined: 13-July 12

Re: deleting a record based on name from aceess db

Posted 25 July 2012 - 11:42 AM

Here is an example of DB editing code from my current project. I'm using a SQL server not Access but the ideas are the same.
Private Sub btnRemoveAdmin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRemoveAdmin.Click
        Dim dialog As DialogResult = MessageBox.Show("Are you sure you want to remove" & cboAdminUsers.SelectedText & " as an administrator?", "Delete confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If dialog = DialogResult.Yes Then
            Dim udtConn As New SqlConnection(sBoMConnectionString)
            'Deletes the selected user from the administrator list
            Using udtConn
                udtConn.Open()
                Dim udtSQLCommand As New SqlCommand("Delete from Admin_Users WHERE Username = @Username", udtConn)
                udtSQLCommand.Parameters.AddWithValue("@Username", cboAdminUsers.Text)

                udtSQLCommand.ExecuteNonQuery()
                udtConn.Close()
            End Using
            'Removed the person who is no longer an admin from the combobox
            AdminUsersBindingSource.Remove(cboAdminUsers.SelectedItem)
        End If
    End Sub


Was This Post Helpful? 0
  • +
  • -

#4 mashalyy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 25-July 12

Re: deleting a record based on name from aceess db

Posted 25 July 2012 - 10:44 PM

Thank you for the kind reply.

But the code isn't working.
Was This Post Helpful? 0
  • +
  • -

#5 smohd   User is offline

  • Critical Section
  • member icon


Reputation: 1824
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: deleting a record based on name from aceess db

Posted 25 July 2012 - 10:54 PM

Quote

But the code isn't working.

That wont help, tell what is not working and what error message(s) are you getting....
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1