473,806 Members | 2,707 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

error on adding record... Try/Catch

I am looping through a bunch of records in 1 database and putting them into
an ado.net datatable

I get a key violation when i try to add a record... so what i'm trying to do
if that happens, is skip that record and move on to the next record... I
haven't quite figured out the Try / Catch scenario. Here is my code:

Try
Me.DataSet11.tb lContact.Rows.A dd(myNewRow)

Catch ex As Exception

Me.DataSet11.tb lContact.Rows.R emove(myNewRow)

GoTo NextRecord

End Try

This code does not work. Any thoughts would be greatly appreciated!

Chris Thunell

ct******@pierce associates.com


Nov 20 '05 #1
5 1249
Well if the tryed call dont work the execution jump to catch... just let the
loop to run on next record, no need to control it from catch...I think

--
Ceers,
Crirus

------------------------------
If work were a good thing, the boss would take it all from you

------------------------------

"Chris Thunell" <ct******@pierc eassociates.com > wrote in message
news:Os******** *****@TK2MSFTNG P10.phx.gbl...
I am looping through a bunch of records in 1 database and putting them into an ado.net datatable

I get a key violation when i try to add a record... so what i'm trying to do if that happens, is skip that record and move on to the next record... I
haven't quite figured out the Try / Catch scenario. Here is my code:

Try
Me.DataSet11.tb lContact.Rows.A dd(myNewRow)

Catch ex As Exception

Me.DataSet11.tb lContact.Rows.R emove(myNewRow)

GoTo NextRecord

End Try

This code does not work. Any thoughts would be greatly appreciated!

Chris Thunell

ct******@pierce associates.com

Nov 20 '05 #2
Hi Chris,

A couple of thoughts..

When you trap Exceptions, it's usually good to be as specific as possible.
The Catch code may try an recover from the wrong error,
NullReferenceEx ception, for example.

If an Exception occurs, the current operation usually hasn't been
completed, so is that Remove necessary there?

It might help if you show the full loop that you're talking about.

Regards,
Fergus
Nov 20 '05 #3
The problem is that there are 2 identical records that are trying to be
added... so on this error, i would like to just ignore the add of the second
record and cycle to the next.
Thanks for everyone's help!

Full Code:

Dim myNewRow As DataRow

Me.daContact.Fi ll(Me.DataSet11 .tblContact)

For Each objItem2 In colItems

myNewRow = Me.DataSet11.tb lContact.NewRow

'get the company name

If IsNothing(objIt em2.CompanyName ) Then

myNewRow("Compa ny") = ""

Else

myNewRow("Compa ny") = NullStrCheck(ob jItem2.CompanyN ame.ToString)

End If

'get the firstname

If IsNothing(objIt em2.FirstName) Then

myNewRow("First Name") = ""

Else

myNewRow("First Name") = NullStrCheck(ob jItem2.FirstNam e.ToString)

End If

'get the last name

If IsNothing(objIt em2.LastName) Then

myNewRow("LastN ame") = ""

Else

myNewRow("LastN ame") = NullStrCheck(ob jItem2.LastName .ToString)

End If

'get the phone number

If IsNothing(objIt em2.CompanyMain TelephoneNumber ) Then

myNewRow("Phone No") = ""

Else

myNewRow("Phone No") =
NullStrCheck(ob jItem2.CompanyM ainTelephoneNum ber.ToString)

End If

'get the faxno

If IsNothing(objIt em2.BusinessFax Number) Then

myNewRow("FaxNo ") = ""

Else

myNewRow("FaxNo ") = NullStrCheck(ob jItem2.Business FaxNumber.ToStr ing)

End If

'get the email

If IsNothing(objIt em2.Email1Addre ss) Then

myNewRow("Email Address") = ""

Else

myNewRow("Email Address") = NullStrCheck(ob jItem2.Email1Ad dress.ToString)

End If

'add record

Try

Me.DataSet11.tb lContact.Rows.A dd(myNewRow)

Catch ex As Exception

MessageBox.Show (Err.Number & " - " & Err.Description & " - " & Err.Source)

GoTo NextRecord

End Try

myCurrentRow = myCurrentRow + 1

System.Windows. Forms.Applicati on.DoEvents()

Me.lblImportSta tus.Text = "Importing Record " & myCurrentRow & " of " &
myCount & " records."

NextRecord:

Next

Me.lblImportSta tus.Text = "Saving Information into Program"

System.Windows. Forms.Applicati on.DoEvents()

'update the dataset

Me.daContact.Up date(Me.DataSet 11.tblContact)

"Fergus Cooney" <fi****@post.co m> wrote in message
news:u5******** ******@TK2MSFTN GP09.phx.gbl...
Hi Chris,

A couple of thoughts..

When you trap Exceptions, it's usually good to be as specific as possible. The Catch code may try an recover from the wrong error,
NullReferenceEx ception, for example.

If an Exception occurs, the current operation usually hasn't been
completed, so is that Remove necessary there?

It might help if you show the full loop that you're talking about.

Regards,
Fergus

Nov 20 '05 #4
Hi Chris,

I've rearranged the code a bit and added a subroutine to tidy up all those
messy If Then Elses. I'm not sure I quite understand yet, though. - What
actually goes wrong? (But don't answer that until you've tried the code
below).

Because there's more code within the Try block, it's now more important
that you are specific about which Exception you are trapping. When it occurs,
it will tell you what Exception type it is. Use that name instead of just
Exception.

As a side-issue, can I ask what IsNothing and NullStrCheck do?

Regards,
Fergus

<code>
For Each objItem2 In colItems
myNewRow = Me.DataSet11.tb lContact.NewRow

SetField (myNewRow, "Company", objItem2.Compan yName)
SetField (myNewRow, "FirstName" , objItem2.FirstN ame)
SetField (myNewRow, "LastName", objItem2.LastNa me)
SetField (myNewRow, "PhoneNo",
objItem2.Compan yMainTelephoneN umber)
SetField (myNewRow, "FaxNo", objItem2.Busine ssFaxNumber)
SetField (myNewRow, "EmailAddre ss", objItem2.Email1 Address)

'add record
Try
Me.DataSet11.tb lContact.Rows.A dd(myNewRow)
myCurrentRow = myCurrentRow + 1
Me.lblImportSta tus.Text = "Importing Record " & myCurrentRow & _
" of " & myCount & " records."
Application.DoE vents()

Catch ex As Exception
MessageBox.Show (ex.Message)
End Try
Next

Sub SetField (dr As DataRow, sFieldName As String, oValue As Object)
If oValue Is Nothing Then
dr (sFieldName) = ""
Else
dr (sFieldName) = NullStrCheck (oValue.ToStrin g)
End If
End Sub
</code>
Nov 20 '05 #5
The key violation will prevent the row from being added. ignore the
exception, move on.

I guess my question would be why do you have a key constraint when you know
there will be duplicates?
"Chris Thunell" <ct******@pierc eassociates.com > wrote in message
news:Os******** *****@TK2MSFTNG P10.phx.gbl...
I am looping through a bunch of records in 1 database and putting them into an ado.net datatable

I get a key violation when i try to add a record... so what i'm trying to do if that happens, is skip that record and move on to the next record... I
haven't quite figured out the Try / Catch scenario. Here is my code:

Try
Me.DataSet11.tb lContact.Rows.A dd(myNewRow)

Catch ex As Exception

Me.DataSet11.tb lContact.Rows.R emove(myNewRow)

GoTo NextRecord

End Try

This code does not work. Any thoughts would be greatly appreciated!

Chris Thunell

ct******@pierce associates.com

Nov 20 '05 #6

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

Similar topics

6
2554
by: wk6pack | last post by:
Hi, I have a question about my coding practise. I have a class method to return a value from a database. I open the connection do my search and dispose the reader. Open the reader with a new recordset and then close the reader and close the connection. I do this for every new record I am adding. But I seem to get an error around the 770 record to add. The error is and unspecified error at the connection.open() for that record.
2
5790
by: brenda.stow | last post by:
error msg " An error occured while referencing the object. You tried to run a visual basic procedure that improperly references a property or method of an object" This msg occurs everytime I add a new record to a subform. If I close msgbox I can then successfully add new record. Have looked on MS Technet and tried their 2 suggestions:- 1)LinkChildFields property contains the name of a form property - this isn't the case linkChildFields...
4
3106
by: Robert Schuldenfrei | last post by:
Dear NG, I was about to "improve" concurrency checking with a Timestamp when I discovered that my current code is not working. After about a day of beating my head against the wall, I am turning to the NG in hopes that someone can spot what I am doing wrong. Key to this technique working is the SQL UPDATE statement. It is designed to fail
0
2134
by: Roman | last post by:
I'm trying to create the form which would allow data entry to the Client table, as well as modification and deletion of existing data rows. For some reason the DataGrid part of functionality stops working when I include data entry fields to the form: I click on Delete or Edit inside of DataGrid and get this error: "Error: Object doesn't support this property or method" If I remove data entry fields from the form - DataGrid allows to...
11
2704
by: Steve Hoyer | last post by:
I am trying to deploy my first asp.net app to our webserver (2K server, IIS 5) My start page comes up and you can get to the subsequent pages that are tied into our sql server (2K). Each page has a datagrid that loads up just fine, but when I click on any link that updates the page, i.e. a column header to sort the database, it returns a 404 error. Any ideas why it works the for the inital load and not on a postback?
4
7622
by: James Radke | last post by:
Hello, I am looking for guidance on best practices to incorporate effective and complete error handling in an application written in VB.NET. If I have the following function in a class module (note that this class module represents the business layer of code NOT the gui layer): Public Function Test(ByVal Parm1 As Integer, ByVal Parm2 As Integer) As SqlDataReader ' Declare the SQL data layer class Dim oSQL As New...
9
2232
by: Gustaf | last post by:
I'm confused about structured error handling. The following piece of code is a simplification of a class library I'm working on. It works, and it does what I want, but I'm still not convinced that I have been doing it right. I think I overdo it. Please have a look: -- using System; using System.IO;
0
3245
by: zfraile | last post by:
I'm getting this error from the JIT compiler at runtime, but only on my boss' machine, not my development machine. I believe the error is generated from a call to an Excel object, but I can't tell why or what specific action on the object is causing it. We both are running version 2.0 of the .net framework with Office 2003 installed, though I am running XP and she has Win2000. Also, I do not get this error when running another sub in...
6
2353
by: redashley40 | last post by:
This is my first attempt in SQL and PreparedStatement I have add the PreparedStatement and I'm not to sure if I'm doing it correctly. When I do a test run on Choose 1 ,or 2 I get this error. Error - com.mysql.jdbc.Statement here is my code below public class DBAssign { /**
0
9719
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10624
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10374
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10111
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9193
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7650
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4330
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3010
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.