An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in system.data.dll

R

Rampa

hello! the problem is always the same but i can't understand why it
happens!! Someone can help me? FIRST QUERY GOES WELL, SECOND BREAK WITH
ERROR why?

Dim connessione As New
SqlClient.SqlConnection("Server=SOSQLSRV01;Initial
Catalog=SO_Bolle;Uid=SO_CattolicaRE;PWD=sogeda")
Dim iddestinatario As Integer

Dim TestoQueryCodicecodicedestinario As String = "SELECT
max(id_destinatario) FROM destinatario "
Dim QueryCodicecodicedestinario As New
SqlClient.SqlCommand(TestoQueryCodicecodicedestinario, Connessione)
Dim LettoreCodicecodicedestinario As SqlClient.SqlDataReader
If Connessione.State = ConnectionState.Closed Then
Connessione.Open()
End If
LettoreCodicecodicedestinario =
QueryCodicecodicedestinario.ExecuteReader()
LettoreCodicecodicedestinario.Read()
Dim Codcodicedestinario As String =
LettoreCodicecodicedestinario.GetInt32(0)
LettoreCodicecodicedestinario.Close()
Connessione.Close()


Dim QuerySalvadestinatario As String = "INSERT INTO
Destinatario (Intestazione) VALUES ('ciao')"
Dim Inseriscidestinatario As New
SqlClient.SqlCommand(QuerySalvadestinatario, connessione)
If connessione.State = ConnectionState.Closed Then
connessione.Open()
End If
Inseriscidestinatario.ExecuteNonQuery()
connessione.Close()
 
C

Cor Ligthert [MVP]

Rampa,

Be aware that this kind of code can be very problemfull in a multiuser
environment where more users can add rows.

I would use the new Guid as the uniqueidentifier.

As well, as you want to get one value than the command.executescalar is much
better.

http://msdn2.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar.aspx

And when you want to see what was your error than embed your commands inside
a Try Catch block

Try
result = cmd.executescalar
Catch sqlex as sqlexception
messagebox.show(sqlex.tostring)
Catch ex as exception
messagebox.show(ex.toString
End Try

I hope this helps,

Cor
 
R

Rampa

Thank you! Really!! I solved the problem!

Be aware that this kind of code can be very problemfull in a multiuser
environment where more users can add rows.

Can you explain me why you say it?

Thank you again!



Cor Ligthert [MVP] ha scritto:
 
C

Cor Ligthert [MVP]

Rampa,
Be aware that this kind of code can be very problemfull in a multiuser
environment where more users can add rows.

Can you explain me why you say it?

Thank you again!
Think that both users want to add a row, the both ask for the last and get
that in almost the same time. Than they are both inserting in my idea (id I
did understand your code well), the same id.

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top