Duplicate identity code in SQL for vb.net winforms - sql-server

Good day,
I am not too sure about the reason this can happen.
I have a large code in which I insert some values to some SQL tables. The program is working in 4-5 separate machines that access the same SQL data base that is held in another server.
Try : codfact = CInt(Me.CFTableAdapter.codigoQuery) + 1 : Catch ex As Exception : codfact = 1 : End Try
There I get my new id of the table and save it in codfact.
CFTableAdapter.codigoQuery = It is a SELECT that gets the MAX(id) of my table.
And then I do the insert:
While Not exit4
Try
Me.CFTableAdapter.InsertQuery(codfact, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 1, -1, -1, Me.codcom, 0, False, -1, -1, -1, -1, 0, selprop)
exit4 = True
Catch ex As Exception
If ex.Message.Contains("PRIMARY KEY") Then
Try : codfact = CInt(Me.CF.codigoQuery) + 1 : Catch : codfact = 1 : End Try
Else
MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error)
End
End If
End Try
End While
That is the code.
What happens is that sometimes both programs keep working with the same codfact that should be the Unique identity ID in the SQL table.
When I try to reproduce this error I always get the "PRIMARY KEY" error and it gets the next codfact to work with(the correct way).
That codfact is used in other tables as Foreing Key. That way I get a single codfact line instead of 2 or 3.

Related

Sqlite3 insert data in database does not work in c

sqlite3_stmt *stmt;
sqlite3_prepare_v2(db, "INSERT INTO links(NAME, LINK, SIZE, STARRED) VALUES ('?' , '?', ? , 0);", 41, &stmt, NULL);
if(stmt != NULL) {
sqlite3_bind_text(stmt, 1, name, 0, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 2, link, 0, SQLITE_TRANSIENT);
sqlite3_bind_int(stmt, 3, size);
sqlite3_step(stmt);
sqlite3_finalize(stmt);
}else{
printf("%sError during insertion in the database%s\n", RED, RST);
}
sqlite3_close(db);
I always get on the output Error during insertion in the database but can't understand why, maybe something related to sqlite3_prepare_v2 but doesn't know what, I tried to execute the query 'manually' with random data and it works.
The size of the zSql statement is 69 characters. It is is better to us -1 for length as by default it will be null-terminated string and size is automatically computed :
sqlite3_prepare_v2(db, "INSERT INTO links(NAME, LINK, SIZE, STARRED) VALUES (?, ?, ? , 0);", -1, &stmt, NULL);
Do not forget the 3rd '?' in the statement for the 3rd argument.
Edit : write it as ? in the statement
You bind twice item 2 of the statement and ? is missing for item 3.
Just solved by changing the third parameter of sqlite3_prepare_v2() that is the maximum length of zSql in bytes, and 47 is to small.

Error: Operation is not allowed when the object is closed

I have the following code:
set ors1 = server.CreateObject("ADODB.recordset")
sqlString = "USE PoSystem OPEN SYMMETRIC KEY WebUsersPasswordEncryption DECRYPTION BY CERTIFICATE EncryptUsersPasswords "&_
"SELECT UserName,JobTitle,UserRoleID,FullName,EntityID,StatusID,CONVERT(nvarchar(50),DECRYPTBYKEY(Password)) AS PASSWORD,Email,BeneficiaryID,DATEADD(dd, 0, DATEDIFF(dd, 0, LastLoginDate)) AS LastLoginDate "&_
"FROM TblWebUsers WHERE UserID="&UserID&" CLOSE SYMMETRIC KEY WebUsersPasswordEncryption"
ors1.Open sqlString,conn,3,1
UserName = ors1("UserName")
Remark = ors1("JobTitle")
PassWord = ors1("PassWord")
Email = ors1("Email")
UserRoleID = ors1("UserRoleID")
StatusID = ors1("StatusID")
Entity = ors1("EntityID")
userFullName= ors1("FullName")
BeneficiaryID = ors1("BeneficiaryID")
LastLoginDate = ors1("LastLoginDate")
ors1.Close
But I'm getting the following message:
Operation is not allowed when the object closed
on ors1("UserName")
How can I solve it? I tried to add SET NOCOUNT ON but it's not working.
As #Paul mentioned in the comments, if you query doesn't return any records, you're going to get an error when you try to access the row values.
Guard against this error by testing to see if the Recordset is at the end (EOF). For example:
ors1.Open sqlString,conn,3,1
' Make sure recordset isn't empty...
If Not ors1.EOF Then
UserName = ors1("UserName")
Remark = ors1("JobTitle")
PassWord = ors1("PassWord")
...
Else
' Query returned no records
End If
If you need to process multiple records, use a loop:
ors1.Open sqlString,conn,3,1
Do Until ors1.EOF
UserName = ors1("UserName")
Remark = ors1("JobTitle")
PassWord = ors1("PassWord")
...
ors1.MoveNext
Loop
I found my problem
needed to change connection string
from:
ConnStr = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source='10.1.1.1';Initial Catalog='***';Uid=***;Pwd=*****;"
I change it to:
ConnStr="Driver={SQL Server};Server=10.1.1.1;Database=****;Uid=**;Pwd=*****;"

BatchUpdateException updateCounts return inconsistent results

I am trying to find the way to recognise the item that failed in a batch insert using SQL Server (driver: sqljdbc4).
I created a tiny test case that sends a varchar out of range so I got
"java.sql.BatchUpdateException: String or binary data would be truncated."
I cannot understand the value of updateCounts, which is as follows:
When the first batch insert causes error :
=> updateCounts is -3
When the 2nd batch insert causes error :
=> Update counts: -3, 1, 1, 1
When the 3rd batch insert causes error :
=> Update counts: 1, -3, 1, 1
When the 4rd batch insert causes error :
=> Update counts: 1, 1, -3, 1
When the 5th batch insert causes error :
=> Update counts: 1, 1, 1, -3
I am sending 5 items all the time, so I was expecting 5 items in updateCounts.
Does anyone see a pattern here so I can identify the wrong item?

Inserting values using Application Engine

I am trying to insert the values from my text file to a record using app engine.
I know this could be done using file layout but my requirement suggests not to use file layout but needs to insert into record using app engine people code.
I am writing the following PeopleCode in my appengine
(here i am trying to add only one field value to my record in this example)
Local File &MYFILE;
Local Record &REC_ERN;
&MYFILE = GetFile("c:\sample.csv", "R", %FilePath_Absolute);
If &MYFILE.IsOpen Then
While &MYFILE.ReadLine(&STRING);
MessageBox(0, "", 0, 0, &STRING);
&source_type_id = Substring(&STRING, 1, 3);
&Stmt= "INSERT INTO KGS_TEST (ID) VALUES ( &source_type_id)";
SQLExec(&Stmt);
REM MessageBox(0, "", 0, 0, &source_type_id);
End-While;
End-If;
&MYFILE.Close();
The problem I am facing is &source_type_id = Substring(&STRING, 1, 3);
the variable &source_type_id has the value but i need to insert this into a record which I have created which has fields (ID,NAME,AGE,department).
The issue you are having is that your variable &source_type_id is inside a string and therefore is interpreted literally as part of the string instead of the value of the variable as you want.
What you need to do is put a bind variable in the string for the value and then pass the value as a parameter to SqlExec.
Local File &MYFILE;
&MYFILE = GetFile("c:\sample.csv", "R", %FilePath_Absolute);
If &MYFILE.IsOpen Then
While &MYFILE.ReadLine(&STRING);
MessageBox(0, "", 0, 0, &STRING);
&source_type_id = Substring(&STRING, 1, 3);
&Stmt= "INSERT INTO KGS_TEST (ID) VALUES ( :1)";
SQLExec(&Stmt,&source_type_id);
REM MessageBox(0, "", 0, 0, &source_type_id);
End-While;
End-If;
&MYFILE.Close();

EXEC %%System().FireTrigger

I was just wondering what is this?
EXEC %%System().FireTrigger(ID = 225, ID = 102, ID = 0, ID = 0, Value = #server,
ID = -1, ID = 0, ID = 0, Value = NULL,
ID = 7, Value = #server, Value = #srvproduct2, Value = #provider, Value = #datasrc, Value = #location, Value = NULL, Value = #catalog)
I have been trying to look for it in mssql but I cant find it, what does it mean? I know EXEC is used to execute stored procedure, but what is %%System()?
and what is FireTrigger? is it an embedded command in the system? external dll? been trying to Google it but it seems to be vague at the moment.
This is an internal mechanism that is sometimes exposed but you can't call it yourself and it is undocumented.

Resources