Solved

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

Posted on 2011-03-15
10
602 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
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: 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 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

810 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