Solved

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

Posted on 2011-03-15
10
599 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
  • 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
 
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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 500 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Stream.BeginRead and Stream.EndRead in .NET Core 5 33
Problem of RegEx to match the first occurence of 10 35
Error in page 3 44
Entity Framework 7 27
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

929 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now