473,396 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Testing if value exists in datacolumn

Hi,

I am trying to build a function that returns a dataset.When populating the
dataset, is it possible to test if a value already exists in a column and
only add it if it is not already present ? I tried adding setting
mycolumn.unique=true, but this generates an exception when the first
duplicate occur.

Any suggestions would be appreciated

Niclas
Nov 20 '05 #1
2 5573
Niclas,
I would still consider setting "mycolumn.unique=true", as it sounds like
that is a requirement of your dataset.

I would use DataView.Find to verify that the value does not exist.
Alternatively you could use DataTable.Select, however DataView.Find is
'cleaner' & probably faster.

If DataView.Find returns -1 the value was not found, if it returns 0 or
greater the value was found.

Something like:

Dim table As DataTable
Dim view As New DataView(table)
view.Sort = "mycolumn"

If view.Find(myValue) = -1 Then
Dim row As DataRow = table.NewRow()
...
table.Add(row)
End If

Note: DataView.Find returns -1 for not found, not null as the documentation
suggests, the return value is an Integer, so null is not really allowed for
a return value.
but this generates an exception when the first
duplicate occur. You could always trap for this exception and ignore it...

Dim row As DataRow = table.NewRow()
...
Try
table.Add(row)
Catch
' this space intentionally left blank
End Try

However you may be 'hiding' exceptions then.

If you don't have it, David Sceppa's book "Microsoft ADO.NET - Core
Reference" from MS Press covers ADO.NET in great details, including a
chapters on DataSets, DataTables, and DataViews.

Hope this helps
Jay

"MS Newsgroups" <no****@nospam.com> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl... Hi,

I am trying to build a function that returns a dataset.When populating the
dataset, is it possible to test if a value already exists in a column and
only add it if it is not already present ? I tried adding setting
mycolumn.unique=true, but this generates an exception when the first
duplicate occur.

Any suggestions would be appreciated

Niclas

Nov 20 '05 #2
Many thanks !

Niclas
"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in message
news:eg**************@tk2msftngp13.phx.gbl...
Niclas,
I would still consider setting "mycolumn.unique=true", as it sounds like
that is a requirement of your dataset.

I would use DataView.Find to verify that the value does not exist.
Alternatively you could use DataTable.Select, however DataView.Find is
'cleaner' & probably faster.

If DataView.Find returns -1 the value was not found, if it returns 0 or
greater the value was found.

Something like:

Dim table As DataTable
Dim view As New DataView(table)
view.Sort = "mycolumn"

If view.Find(myValue) = -1 Then
Dim row As DataRow = table.NewRow()
...
table.Add(row)
End If

Note: DataView.Find returns -1 for not found, not null as the documentation suggests, the return value is an Integer, so null is not really allowed for a return value.
but this generates an exception when the first
duplicate occur.

You could always trap for this exception and ignore it...

Dim row As DataRow = table.NewRow()
...
Try
table.Add(row)
Catch
' this space intentionally left blank
End Try

However you may be 'hiding' exceptions then.

If you don't have it, David Sceppa's book "Microsoft ADO.NET - Core
Reference" from MS Press covers ADO.NET in great details, including a
chapters on DataSets, DataTables, and DataViews.

Hope this helps
Jay

"MS Newsgroups" <no****@nospam.com> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
Hi,

I am trying to build a function that returns a dataset.When populating the dataset, is it possible to test if a value already exists in a column and only add it if it is not already present ? I tried adding setting
mycolumn.unique=true, but this generates an exception when the first
duplicate occur.

Any suggestions would be appreciated

Niclas


Nov 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Andy Levy | last post by:
Hi Im trying to create a function that tests if a 'Username' exists in a particular table. If the value is found then the code generates 'Username1' as the username, and check if that username...
6
by: Shabam | last post by:
A web application of mine developed using C# + MS SQL runs fine normally. However when I stress test it with a load testing software (using about 60 simultaneous users) some instances start...
0
by: rodrigo | last post by:
How to get get the last inserted key (value NOT column) in a specific dataTable dt right after dt.Rows.InsertAt according to code and schema below. The key is a autoincrement column. foreach...
3
by: Rob Rogers | last post by:
I have an ASP.NET datagrid that loads an image. I would like to load one image if the value in my SQL table is true and another if it is false. Here is my code. <%@ Page Language="vb"...
5
by: JC Voon | last post by:
Hi: How to reset the autoincrement value generated by DataTable ? I've master and detail table, the detail table has a autoincrement column, each time i add a new master record, i need to...
13
by: dbuchanan | last post by:
Hello, Here is the error message; ---------------------------- Exception Message: ForeignKeyConstraint Lkp_tbl040Cmpt_lkp302SensorType requires the child key values (5) to exist in the...
4
by: simon | last post by:
hello, i am displaying a dataset in a datagrid, for one of the values being displayed it either comes back as a 1 or a 0, which is currently bound to a column in the datagrid what i'd like to do...
2
by: ismaelf | last post by:
i have a textbox in a template column in a datagrid, it displays the initial value of the row, but when i change it remains with the original value (it doesnt get the new value..) i fill the...
3
by: mnishani | last post by:
Is there any way to add a default value to a datagrid column(for the whole column - eg : system date) . I'm using vs2003 and datagrid and using dataadapter to update the grid. Thanks in advance
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.