Problem with if/else statement when doing sql update/insert

xmlclmx

New member
Joined
Oct 12, 2006
Messages
2
Programming Experience
Beginner
Good day to everyone....im currently doing a small module on reading the contents of a text file and then updating/inserting into my oracle database...
i'm using vb 2005 express....
this is my code...
VB.NET:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
objSRFile = File.OpenText("C:\Documents and Settings\**.txt")
Dim objconn As New OleDb.OleDbConnection
objconn.ConnectionString = "Provider=msdaora;Data Source=**;User Id=**;Password=**;"
Dim objcmd1 As New OleDb.OleDbCommand
Dim objcmd2 As New OleDb.OleDbCommand
Dim objcmd3 As New OleDb.OleDbCommand
objcmd1 = New OleDbCommand(strSQL1, objconn)
objcmd2 = New OleDbCommand(strSQL2, objconn)
objcmd3 = New OleDbCommand(strSQL3, objconn)
 
objconn.Open()
 
While objSRFile.Peek <> -1
strInput = objSRFile.ReadLine
arrStrInput = Split(strInput, ",", , CompareMethod.Text)
For intCurrPos = 0 To arrStrInput.Length - 1
Select Case intCurrPos
Case 0
strAtitleno = arrStrInput(intCurrPos)
Case 1
strAyear1 = arrStrInput(intCurrPos)
Case 2
strArent1 = arrStrInput(intCurrPos)
End Select
Next
 
strSQL1 = "select count(*) from arrears_temp where atitleno='" & strAtitleno & "'"
 
If objcmd1.CommandText = strSQL1.ToString = 0 Then
strSQL2 = "insert into arrears_temp(atitleno, ayear1, arent1) values('" & strAtitleno & "', " & strAyear1 & ", " & strArent1 & ")"
objcmd2.CommandText = strSQL2.ToString()
objcmd2.ExecuteNonQuery() [COLOR=red][I][B]'ERROR OCCURED HERE[/B][/I][/COLOR]
Else
strSQL3 = "update arrears_temp SET ayear1=" & strAyear1 & ", arent1=" & strArent1 & ", ayear2=ayear1, arent2=arent1, ayear3=ayear2, arent3=arent2, ayear4=ayear3, arent4=arent3, ayear5=ayear4, arent5=arent4 + arent5 where atitleno='" & strAtitleno & "'"
objcmd3.CommandText = strSQL3.ToString()
objcmd3.ExecuteNonQuery()
End If
 
End While
objconn.Close()
objSRFile.Close()
End Sub
End Class
i'm having the error "ORA-00001: unique constraint (***.***_C001650) violated" when i execute it....

let me explain how it works....first i will do an sql count(*) statement...where it will check if the atitleno exist or not inside the database, if it returns 0...then i will do an insert statement...else...a record will be updated....

the if/else statement is giving me problems....if the record exists....it should go to the update statement...however it doesn't....

please give any hints regarding on how to solve this problem...

thanks in advance...:)
 
Last edited by a moderator:
I've figured it out...yay.....

all i need is a data adapter and a few changes here and there and the program's good to go...:D
 
Back
Top