Solved

Determine if a new row is needed in a DataSet/DataTable

Posted on 2004-10-18
2
241 Views
Last Modified: 2010-04-24
OK. I'm still trying to get my arms around many of the ADO.NET concepts.

Here's what I have in mind.

I have 2 tables -

1. Two fields
a. DispID - Integer - Identity field - Primary Key
b. DispNo - Char(12) - Unique

2. Two fields
a. DispID - Integer - Foreign Key pointing to above table. Unique
b. PoNo - Char(7) - Primary Key.

The data in table 1 is generally pre-loaded.

I want to develop a data-entry form to enter info into table 2. At any point in time there may or may not be an entry in table two corresponding with table 1's DispID. If it IS present then I want the user be able to EDIT this data. If not, then a new record should be added.

Now, I get the whole process of filling the dataset appropriately, etc, it's the editing portion I'm having difficulty with.

So, the form will have two controls. A ComboBox bound as follows to the appropriate dataset:

DataSource = Table1
DisplayMember = DispNo field in Table1
ValueMember = DispID field in Table1
SelectedValue = DispID field in Table2

A text box bound to the PoNo field in Table2.

When the user selects a DispNo from the ComboBox (i.e. SelectedIndexChanged event fires) then the logic should check if this record exists in Table2, if it does then go to that row in the datatable and allow the user to edit. If it doesn't exist, then it should add a new row allowing the user to enter the corresponding PoNo.

I'm trying to get my arms around the logic to do this last part, specifically checking for the row in table two and determining if there is one. If there is, go to that row to allow an edit. If not, then create a new one.

Am I making this more complicated than it really is?

Any help would be greatly appreciated.

Thanks in advance,

Steve
0
Comment
Question by:corwin_ranger
2 Comments
 
LVL 4

Accepted Solution

by:
gdexter earned 250 total points
ID: 12340435
This will help you determine if a record needs to added or edited

        Try
            Dim dr() As DataRow = ds.Tables("Table2").Select("DispID=" & ComboBox1.SelectedValue)
            If (dr.GetUpperBound(0) >= 0) Then
                'has a record do the edit
            Else
                'new record needs to be added
            End If

        Catch idx As IndexOutOfRangeException

        End Try
0
 

Author Comment

by:corwin_ranger
ID: 12341120
Thanks a great deal.  That makes a good deal of sense.  I've only started looking at ADO and haven't really worked directly with DataRows yet.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

816 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

10 Experts available now in Live!

Get 1:1 Help Now