Solved

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

Posted on 2011-03-15
10
597 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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 tutorial demonstrates a quick way of adding group price to multiple Magento products.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 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

12 Experts available now in Live!

Get 1:1 Help Now