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
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