?
Solved

How do I delete a row from table and database (Windows Forms .Net)?

Posted on 2011-03-15
10
Medium Priority
?
614 Views
Last Modified: 2012-05-11
Hi,

I'm new to Windows Forms and .Net programming with database access.

I'm trying just a single form with some textboxes on it and a simple table.
There's a table opened with several rows.

In my form I have a bindingSource, a tableAdapter and a DataSet.

After reading here and there, I found that I need this 5 lines in order to delete a row!!
But.... really!?

            DataRowView drv = (DataRowView)bindingSource1.Current;
            MyDataSet.TestRow row = (MyDataSet.TestRow)drv.Row;
            this.testTableAdapter.Delete(row.Col1, row.Col2);
            this.bindingSource1.EndEdit();
            this.bindingSource1.RemoveCurrent();

Is there any simpler approach? I mean, less lines.
0
Comment
Question by:fischermx
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 12

Expert Comment

by:jandromeda
ID: 35143901
0
 
LVL 10

Expert Comment

by:Nash2334
ID: 35144053
You TableAdapter has an update method that will insert, update or delete a record based on it's current rowstate, calling the appropriate stored procedure for you.  That is the big plus with using TableAdapters, you don't have to specify insert, deletes or updates (although you can).

Your BindingSource will bind the DataTable of the DataSet to the UI component, perhaps a DataGridView in your case.  This will act as the "traffic cop" for you, committing changes to the database when the TableAdapter's Update method is called.  In other words, if you have a DataSet/DataTable bound to a BindingSource bound to a DataGridView (as your UI component), all you need to do is call the Update method of your TableAdapter to delete a row after it's been deleted in the UI.

MyTableAdapter ta = new MyTableAdapter();
ta.Update(myDataSet.MyDataTable);
0
 
LVL 1

Author Comment

by:fischermx
ID: 35161398
Nash:

I don't get it when you say "all you need to do is call the Update method of your TableAdapter to delete a row after it's been deleted in the UI."

I mean, well, that's my question, I want to disappear a record, kill it, of course from the cached data in the UI and in the table.

So, according to your explanation, what are the steps to delete record? (all the steps)

0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
LVL 10

Expert Comment

by:Nash2334
ID: 35161986
Are you bound to a DataGridView?
0
 
LVL 1

Author Comment

by:fischermx
ID: 35162742

No, the data is bound to textboxes.

0
 
LVL 10

Expert Comment

by:Nash2334
ID: 35165691
OK, so if I am reading this correctly you've got a bunch of text elements on a form, and they are bound to a single record in your database.  It sounds like you are using a BindingNavigator.  You click the BindingNavigator delete button to delete the current record.

If this is correct, the BindingNavigator has it's own delete method wired to the button that will perform all actions for you as long as you have a valid delete command and the primary key of the record is available.  You only need to write all that code if you are manually handling the delete yourself, e.g. in the case where you want to prompt for delete confirmation.

If you are not using a BindingNavigator, and are wired directly to a button or linkbutton click event, some more code would be helpful to see where there are opportunities for efficiencies, including where you are doing your DataBinding.

0
 
LVL 1

Author Comment

by:fischermx
ID: 35168609
I showed my code in the original post:

 DataRowView drv = (DataRowView)bindingSource1.Current;
MyDataSet.TestRow row = (MyDataSet.TestRow)drv.Row;
this.testTableAdapter.Delete(row.Col1, row.Col2);
this.bindingSource1.EndEdit();
this.bindingSource1.RemoveCurrent();

My questions
Is this the correct way? (Yes/No)
Is this the only way? (Yes/No)
Is there a way with fewer lines? (Yes/No)
0
 
LVL 10

Accepted Solution

by:
Nash2334 earned 2000 total points
ID: 35168781
1. Yes, this is ONE correct way.
2. No
3. Depends, which is why I was asking if you were using a BindingNavigator.  Your code does not tell me how you are binding to your UI controls or loading your data into the BindingSource, hence the request for more information.  If that's all you can provide, then I'd say run with what you have.
0
 
LVL 1

Author Comment

by:fischermx
ID: 35202219
I don't have a binding navigator.
But if that will help, I can put that, and hide it to the user. (I don't want that ugly thing in my UI).

So, let's suppose I have a binding navigator. How can that simplify things?

0
 
LVL 1

Author Closing Comment

by:fischermx
ID: 35233532
Thank you!
0

Featured Post

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question