By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,573 Members | 3,008 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,573 IT Pros & Developers. It's quick & easy.

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

P: n/a
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(TestoQueryCodicecodicedestina rio, 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()

Jul 17 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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/lib...utescalar.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

"Rampa" <on***@libero.itschreef in bericht
news:11**********************@m79g2000cwm.googlegr oups.com...
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(TestoQueryCodicecodicedestina rio, 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()

Jul 17 '06 #2

P: n/a
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:
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/lib...utescalar.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

"Rampa" <on***@libero.itschreef in bericht
news:11**********************@m79g2000cwm.googlegr oups.com...
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(TestoQueryCodicecodicedestina rio, 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()
Jul 17 '06 #3

P: n/a
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
Jul 17 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.