Home All Groups Group Topic Archive Search About

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

Author
17 Jul 2006 8:57 AM
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()

Author
17 Jul 2006 9:18 AM
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

Show quoteHide quote
"Rampa" <on***@libero.it> schreef in bericht
news:1153126676.767257.268520@m79g2000cwm.googlegroups.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(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()
>
Author
17 Jul 2006 9:29 AM
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:

Show quoteHide quote
> 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
>
> "Rampa" <on***@libero.it> schreef in bericht
> news:1153126676.767257.268520@m79g2000cwm.googlegroups.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(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()
> >
Author
17 Jul 2006 10:12 AM
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