error on adding record... Try/Catch

  • Thread starter Thread starter Chris Thunell
  • Start date Start date
C

Chris Thunell

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

(e-mail address removed)
 
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
 
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
 
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.ToString)

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)
 
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>
 
Thanks all for your repoonse.

Hi Chris

When the Rows.Add() mehod violates the unique constraint in the table, the
Rows is actually not added to the table. So you needn't remove it from the
table. As Fergus said, just let the loop go on.

If anything is unclear, please feel free to reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

--------------------
| From: "Chris Thunell" <[email protected]>
| Subject: error on adding record... Try/Catch
| Date: Thu, 6 Nov 2003 08:18:39 -0500
| Lines: 27
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Newsgroups:
microsoft.public.dotnet.framework.adonet,microsoft.public.dotnet.languages.v
b
| NNTP-Posting-Host: 24-249-184-208.dsl.lan2wan.com 208.184.249.24
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vb:154259
microsoft.public.dotnet.framework.adonet:65626
| X-Tomcat-NG: microsoft.public.dotnet.framework.adonet
|
| 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
|
| (e-mail address removed)
|
|
|
|
|
 
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?
 
Hi Chris,

I'd like to know if this issue has been resolved yet. Is there anything
that I can help on this? I'm still monitoring on it.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

--------------------
| From: "Chris Thunell" <[email protected]>
| Subject: error on adding record... Try/Catch
| Date: Thu, 6 Nov 2003 08:18:39 -0500
| Lines: 27
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <[email protected]>
| Newsgroups:
microsoft.public.dotnet.framework.adonet,microsoft.public.dotnet.languages.v
b
| NNTP-Posting-Host: 24-249-184-208.dsl.lan2wan.com 208.184.249.24
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vb:154259
microsoft.public.dotnet.framework.adonet:65626
| X-Tomcat-NG: microsoft.public.dotnet.framework.adonet
|
| 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
|
| (e-mail address removed)
|
|
|
|
|
 
Back
Top