473,495 Members | 2,021 Online
Bytes | Software Development & Data Engineering Community
Create 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.tblContact.Rows.Add(myNewRow)

Catch ex As Exception

Me.DataSet11.tblContact.Rows.Remove(myNewRow)

GoTo NextRecord

End Try

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

Chris Thunell

ct******@pierceassociates.com


Nov 20 '05 #1
5 1237
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******@pierceassociates.com> wrote in message
news:Os*************@TK2MSFTNGP10.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.tblContact.Rows.Add(myNewRow)

Catch ex As Exception

Me.DataSet11.tblContact.Rows.Remove(myNewRow)

GoTo NextRecord

End Try

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

Chris Thunell

ct******@pierceassociates.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,
NullReferenceException, 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.Fill(Me.DataSet11.tblContact)

For Each objItem2 In colItems

myNewRow = Me.DataSet11.tblContact.NewRow

'get the company name

If IsNothing(objItem2.CompanyName) Then

myNewRow("Company") = ""

Else

myNewRow("Company") = NullStrCheck(objItem2.CompanyName.ToString)

End If

'get the firstname

If IsNothing(objItem2.FirstName) Then

myNewRow("FirstName") = ""

Else

myNewRow("FirstName") = NullStrCheck(objItem2.FirstName.ToString)

End If

'get the last name

If IsNothing(objItem2.LastName) Then

myNewRow("LastName") = ""

Else

myNewRow("LastName") = NullStrCheck(objItem2.LastName.ToString)

End If

'get the phone number

If IsNothing(objItem2.CompanyMainTelephoneNumber) Then

myNewRow("PhoneNo") = ""

Else

myNewRow("PhoneNo") =
NullStrCheck(objItem2.CompanyMainTelephoneNumber.T oString)

End If

'get the faxno

If IsNothing(objItem2.BusinessFaxNumber) Then

myNewRow("FaxNo") = ""

Else

myNewRow("FaxNo") = NullStrCheck(objItem2.BusinessFaxNumber.ToString)

End If

'get the email

If IsNothing(objItem2.Email1Address) Then

myNewRow("EmailAddress") = ""

Else

myNewRow("EmailAddress") = NullStrCheck(objItem2.Email1Address.ToString)

End If

'add record

Try

Me.DataSet11.tblContact.Rows.Add(myNewRow)

Catch ex As Exception

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

GoTo NextRecord

End Try

myCurrentRow = myCurrentRow + 1

System.Windows.Forms.Application.DoEvents()

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

NextRecord:

Next

Me.lblImportStatus.Text = "Saving Information into Program"

System.Windows.Forms.Application.DoEvents()

'update the dataset

Me.daContact.Update(Me.DataSet11.tblContact)

"Fergus Cooney" <fi****@post.com> wrote in message
news:u5**************@TK2MSFTNGP09.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,
NullReferenceException, 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.tblContact.NewRow

SetField (myNewRow, "Company", objItem2.CompanyName)
SetField (myNewRow, "FirstName", objItem2.FirstName)
SetField (myNewRow, "LastName", objItem2.LastName)
SetField (myNewRow, "PhoneNo",
objItem2.CompanyMainTelephoneNumber)
SetField (myNewRow, "FaxNo", objItem2.BusinessFaxNumber)
SetField (myNewRow, "EmailAddress", objItem2.Email1Address)

'add record
Try
Me.DataSet11.tblContact.Rows.Add(myNewRow)
myCurrentRow = myCurrentRow + 1
Me.lblImportStatus.Text = "Importing Record " & myCurrentRow & _
" of " & myCount & " records."
Application.DoEvents()

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.ToString)
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******@pierceassociates.com> wrote in message
news:Os*************@TK2MSFTNGP10.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.tblContact.Rows.Add(myNewRow)

Catch ex As Exception

Me.DataSet11.tblContact.Rows.Remove(myNewRow)

GoTo NextRecord

End Try

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

Chris Thunell

ct******@pierceassociates.com

Nov 20 '05 #6

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

Similar topics

6
2537
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...
2
5760
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...
4
3086
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...
0
2115
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...
11
2666
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...
4
7598
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...
9
2211
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...
0
3218
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...
6
2341
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...
0
7120
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,...
0
6991
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7160
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,...
0
7196
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
7373
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
5456
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,...
0
3088
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...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
286
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...

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.