Click here to Skip to main content
15,909,656 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All:)

I just started learning ASP.NET using VB.NET and I could use all the guidance there is so thanks in advance for any help you can offer :)

I am creating a web application using VB.NET as the language. I am using Visual Web Developer as the IDE and Microsoft Access as the database. The web application allows users to select a row and delete it. I have added the Select Row feature from the gridview "Autogenerateselect" property and it works fine. However when I tried to set the "Autogeneratedelete" property to true, it wouldn't delete the row. So, I thought I would leave the select as it is and program a button to delete the selected row. I have done that, but the code isn't working; it's not giving me any errors, but it just refreshes the page

Here is the code:
VB
Protected Sub Button1_Click(sender As Object, e As System.EventArgs)
          Dim cn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\Users\toshiba\Documents\Employees.mdb;User Id=admin;Password=;")
          Dim dt As New DataTable
          Dim ds As New DataSet


          ds.Tables.Add(dt)
          Dim da As New OleDbDataAdapter
          cn.Open()
          da = New OleDbDataAdapter("select * from Employees", cn)
          da.Fill(dt)

          dt.Rows(0).BeginEdit()
          dt.Rows(0).Delete()
          dt.Rows(0).EndEdit()

          Dim cs As New OleDbCommandBuilder(da)

          GridViewAdmin.DataSource = dt.DefaultView

          da.Dispose()

          cn.Close()


      End Sub


I really can't figure out what the problem is. I have searched around but none of the solutions match what I'm looking for.

Kindly would anyone please assist me in this and point me to the right direction?

Thank you :)
Posted

You're deleting the row from the datatable, but not actually from the database. Perhaps the simplest way is to use OleDbDataAdapter[^].

Another option is to use a single OleDbCommand [^] to execute the DELETE statement against the Access database.
 
Share this answer
 
Comments
Espen Harlinn 22-May-12 18:09pm    
Good points :-D
Wendelius 23-May-12 0:59am    
THank you :)
Prasad_Kulkarni 23-May-12 0:24am    
My 5!
Wendelius 23-May-12 0:59am    
Thanks :)
Maciej Los 23-May-12 12:05pm    
Good answer, my 5!
C#
private void button1_Click(object sender, EventArgs e)
       {
           int index = 0;//dataGridView1中ID列的索引
           string temp = string.Format("delete 表名 where ID={0}",
               dataGridView1[index, dataGridView1.SelectedCells[0].RowIndex].Value);
           dataGridView1.Rows.RemoveAt(dataGridView1.SelectedCells[0].RowIndex);
       }
 
Share this answer
 
Comments
AmeeR-89 22-May-12 5:25am    
Hello :)
Thank you for your solution, but it didn't work. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900