Home > Ask the Microsoft .Net Development Experts > Visual Studio .NET Questions & Answers > What could be causing this problem with my add New and delete methods?
Ask The Win Development Expert: Questions & Answers
EMAIL THIS

What could be causing this problem with my add New and delete methods?

John Robbins EXPERT RESPONSE FROM: John Robbins

Pose a Question
Other Win Development Categories
Meet all Win Development Experts
Become an Expert for this site


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


>
QUESTION POSED ON: 24 June 2003
I am facing a problem in my add New and delete methods. My code is attached to show you the way I am doing it. Can you help me please on this?
Private Sub cmdView_Click(ByVal sender As System.Object, ByVal e As 
System.EventArgs) Handles cmdView.Click
        Dim NR As DataRow = DS.Tables("accounts").NewRow()
        NR("ID") = txtID.Text
        NR("Account_Name") = txtName.Text
        NR("Account_Reference") = txtReference.Text
        NR("SubID") = txtSub.Text
        DS.Tables("accounts").Rows.Add(NR)
        DA.Update(DS, "Accounts")
    End Sub

    Private Sub cmdDelete_Click(ByVal sender As System.Object, ByVal e As 
System.EventArgs) Handles cmdDelete.Click
        Dim NR As DataRow = DS.Tables("accounts").Select("ID = '" &
txtID.Text & "'")(0)
        NR.Delete()
        DA.Update(DS, "accounts")
    End Sub


>

The Add button code looks like it should work, but the delete button needs some work. Before you can use the dataset, you must fill it with the data from the data adaptor. Try adding something like:

   Dim DS As New DataSet()
   DA.Fill(DS)

before calling the first line in the cmdDelete_Click method. That should work. The first one works because you "fill" the dataset with the new row.

As an aside, where is DS declared? Is there a reason it has larger scope than these button events? Always try to limit the scope of an object to the narrowest possible context. If you don't need DS to hang around and be used after the method exits, declare it in the individual procedures.


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
Visual Studio .NET
Load a DSL domain model instance file programmatically
Calling via command prompt from ASP.NET
On line-by-line analysis in VS .NET
Best approach for merging text files using arrays
Descriptions of symbols in the class view of a .NET solution
What's the best approach for enumerating network resources?
How to enable a line-by-line code walkthrough
Changing a C++ Makefile project to a Utility project
How can I configure Visual Studio.NET 2002 to use the .NET Framework 1.1?
Changing a datagrid column to read-only=false based on variable

Visual Studio .NET (Archive)
Is there a .NET equivalent to netsend that I can use in VB.NET?
I'm getting a lot of flickering on my application
How can I make a VB6 network app available on the Internet using .NET?
How can I implement an FTP upload using VB.NET?
VB.NET app works on my local drive but not on the network drive
Is developing add-ons for Windows Explorer supported using managed code in .NET?
In C#, how can I change the properties of controls on another form?
How do I add images in listview by using VB.NET?
What is the best technique for connecting to a database from VB.NET?
How do you use control collection in VB.NET?

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice



Windows Development - White Papers, News and Expert Advice
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2000 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts