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
mash alyyPosted Jul 26, 2012, 4:25 AM
The latest code you posted are working fine now with little adjustments.
But one thing I am not getting: Whenever record not found, a message is coming and after then it is closing the application, Why. I mean if no record found then it shud only pop-up a message and shud stay at the form only.
2nd thing regarding Modify the data, i am modifying the data in Datagridview control and pressing modify button, but no modification is happening in the db, here is my code:
'Modify Button code
Mycn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & System.Environment.CurrentDirectory & "\STPL.accdb")
Command = New OleDbCommand("SELECT * FROM Equipments", Mycn)
Mycn.Open()
Dim myDA As New OleDbDataAdapter
myDA = New OleDbDataAdapter(Command)
Dim myDataSet As DataSet
Dim builder As OleDbCommandBuilder = New OleDbCommandBuilder(myDA)
myDataSet = New DataSet()
myDA.Fill(myDataSet, "Equipments")
Mycn.Close()
DataGridView1.DataSource = myDataSet.Tables("Equipments").DefaultView
txtMan.Focus()
Mycn = Nothing
But when i press modify button, nothing is happening, its not modifying the data.
mash alyyPosted Jul 26, 2012, 3:58 AM
I tried your latest code but the same thing, it is not deleting the record from the access 2007 database. It's just saying record deleted but actually it is not deleting the record from the db.
One thing whenever record not found it is just simply closing the application, why it's doing this.
The code checking the record in the database, but not deleting the record from the database.
Please help me out......
Satyapriya NayakPosted Jul 26, 2012, 2:28 AM
Here it is working fine.
Try this...
Imports System.Data
Imports System.Data.OleDb
Public Class Form1
Dim ConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath & "\emp.mdb"
Dim oledbda As OleDbDataAdapter
Dim ds As DataSet
Dim com As OleDbCommand
Dim str As String
Private Sub btn_delete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_delete.Click
If TextBox1.Text = "" Then
MsgBox("Please enter Mandal name")
Else
namecheck()
Try
Dim con As New OleDbConnection(ConnectionString)
str = "Delete from stpl where Mandal='" & TextBox1.Text & "'"
com = New OleDbCommand(str, con)
con.Open()
com.ExecuteNonQuery()
con.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
MessageBox.Show("Would you like to delete?", "Deleting Employee Details", MessageBoxButtons.OK, MessageBoxIcon.Question)
TextBox1.Text = ""
binddatagridview()
End If
End Sub
Private Sub namecheck()
Dim con As New OleDbConnection(ConnectionString)
con.Open()
str = "Select count(*)from stpl where Mandal='" + TextBox1.Text & "'"
com = New OleDbCommand(str, con)
Dim count As Integer = Convert.ToInt32(com.ExecuteScalar())
If count > 0 Then
MessageBox.Show("Record exits")
Else
MessageBox.Show("Record does not exits")
End
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
binddatagridview()
End Sub
Private Sub binddatagridview()
Dim con As New OleDbConnection(ConnectionString)
con.Open()
str = "Select * from stpl"
com = New OleDbCommand(str, con)
oledbda = New OleDbDataAdapter(com)
ds = New DataSet
oledbda.Fill(ds, "stpl")
DataGridView1.DataSource = ds
DataGridView1.DataMember = "stpl"
con.Close()
End Sub
End Class
Thanks
If this post helps you mark it as answer
mash alyyPosted Jul 26, 2012, 1:41 AM
thank you very much Satyapriya nayak for ur kind response.
The code is working fine but it is not deleting any record from the access database. The code is just checking whether the record existing or not, if existing it's just saying "would you like to delete record" but when pressing ok button it is not deleting any record from the database.
If record not found, saying no record found, and closing the application, but problem with the deletion, it's not actually deleting any record.
Please look it once. Thank you.
Satyapriya NayakPosted Jul 25, 2012, 10:25 AM
Try this...
Imports System.Data
Imports System.Data.OleDb
Public Class Form1
Dim ConnectionString As String = System.Configuration.ConfigurationSettings.AppSettings("dsn")
Dim com As OleDbCommand
Dim str As String
Private Sub btn_delete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_delete.Click
If TextBox1.Text = "" Then
MsgBox("Please enter name")
Else
namecheck()
Try
Dim con As New OleDbConnection(ConnectionString)
str = "Delete from stpl where Mandal='" & TextBox1.Text & "'"
com = New OleDbCommand(str, con)
con.Open()
com.ExecuteNonQuery()
con.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
MessageBox.Show("Would you like to delete?", "Deleting Employee Details", MessageBoxButtons.OK, MessageBoxIcon.Question)
TextBox1.Text = ""
End If
End Sub
Private Sub namecheck()
Dim con As New OleDbConnection(ConnectionString)
con.Open()
str = "select count(*)from stpl where Mandal='" + TextBox1.Text & "'"
com = New OleDbCommand(str, con)
Dim count As Integer = Convert.ToInt32(com.ExecuteScalar())
If count > 0 Then
MessageBox.Show("Record exits")
Else
MessageBox.Show("Record does not exits")
End
End If
End Sub
End Class
Thanks
If this post helps you mark it as answer
VulpesPosted Jul 25, 2012, 9:50 AM
I can't see any obvious candidates unless there's something in your ClearTextBox method.