Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

working with a try statement

Status
Not open for further replies.

sthmpsn1

MIS
Sep 26, 2001
456
US
I have the following try statement which isn't working the way I would like. The way I want this to work is if no record is found on my innital search I want one created. It doesn't do the catch. Any good suggestions on a work around?

Try
selectCMD1 = New SqlCommand()
selectCMD1.Connection = dbconn
selectCMD1.CommandTimeout = 30
selectCMD1.CommandText = "Select * from yeareval1 where loginID = '" & Session("employee") & "'"
empDA.SelectCommand = selectCMD1
dbconn.Open()
empDA.Fill(empDS, "yeareval1")
dbconn.Close()


Catch

Dim myConnection As SqlConnection
Dim myCommand As SqlCommand
Dim strInsert As String
Dim cmdInsert As SqlCommand

strInsert = "Insert yeareval1 ( loginID, year ) values ( @loginID, @year)"
cmdInsert = New SqlCommand(strInsert, myConnection)
cmdInsert.Parameters.Add("@loginID", Session("employee"))
cmdInsert.Parameters.Add("@year", Date.Today.Year - 1)
myConnection.Open()
cmdInsert.ExecuteNonQuery()
myConnection.Close()
selectCMD1 = New SqlCommand()
selectCMD1.Connection = dbconn
selectCMD1.CommandTimeout = 30
selectCMD1.CommandText = "Select * from yeareval1 where loginID = '" & Session("employee") & "'"
empDA.SelectCommand = selectCMD1
dbconn.Open()
empDA.Fill(empDS, "yeareval1")
dbconn.Close()

End Try
 
well, unless you raise an exception, your catch won't have anything to catch.

Instead of doing it that way, the quick and dirty way would be to check if empDS had anything returned in it, and if not you do the insert.

Its also not a good idea to throw exceptions around like this either, as it can affect performance.

D
 
I believe this is the format I use.

Try
' establish DB connection
oldbConnection.open()

Catch exception as System.Data.OleDB.OleDBexception
'print execption message

Finally
' optionalally run cleanup
oldbConnection.Close()
End Try
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top