chiarimento su datareader

martedì 18 luglio 2006 - 10.31

bluland Profilo | Guru

salve, ho letto su msdn che e' sempre consigliabile chiudere un oggetto datareader altrimenti non si puo' utilizzare la connessione per altre cose infatti sono incorso verso il seguente errore:

The connection is already Open (state=Open).

ecco le domande:

1. ecco il mio codice: read = cmd.ExecuteReader(CommandBehavior.CloseConnection)

ma in questo caso non chiudo cmq la connessione?

2. il problema si risolve sia mettendo myconnection.close() che read.close() l'uno o l'altro vanno bene.
allora non ho capito una cosa, in realta' tale errore viene generato perche' il datareader e' aperto??
e allora perche' si fissa l'errore anche se faccio myconnection.close()?? che poi la connessione non viene chiusa gia' ?? vedi domanda 1..

saluti

ENzo

bluland Profilo | Guru

beh nel seguente codice ho un errore che non riesco a capire::

Private Function ruolo() As String
Dim e As String = Request.ServerVariables("REMOTE_USER")
Dim b As String = e.Substring(8)

Dim queryUser As String = "select AO from OSM_User where [user] = '" & b & "' "

Me.connloc.Open()

'Dim da As New SqlDataAdapter(queryUser, Me.Connord)

Dim cmd As New SqlCommand(queryUser, Me.connloc)
Dim dr As SqlDataReader


dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)

'verifo CHE dr = null
If dr.HasRows Then

Do While dr.Read()

If IsDBNull(dr.GetString(0)) Then

Return (dr.GetString(0))
'dr.Close()

Else

Dim s As String = dr.GetString(0).Trim
'
dr.Close()
Me.connloc.Close()
Return s

End If
Loop

End If

End Function

Private Sub dagrid_itembound(ByVal sender As Object, ByVal e As DataGridCommandEventArgs) Handles DgItem.ItemCommand

'prova con stringbuilder
Dim sb As New StringBuilder

sb.Append("Update ord_web")
sb.Append(" set Accepting_Date = CURRENT_TIMESTAMP")
sb.Append(" WHERE ID= @elemento")


Dim queryModifica As String = sb.ToString

Dim queryOrdspadett As String = "select * " & _
" from Ord_web " & _
"where SpA_Order= @elemento " & _
"order by Shipping_Date"

Dim queryOrdsuddett As String = "select * " & _
"from Ord_web " & _
"where Sud_Order= @elemento " & _
"order by Shipping_Date"

Dim queryNRpldett As String = "select * " & _
"from Ord_web where " & _
"[NRPL-ShopOrder]= @elemento " & _
"order by Shipping_Date"

'preleva ruolo dalla funzione ruolo
Dim s As String = ruolo()

'per user che possono accettare gli ordini
If s = "admin" Then

Try

Me.connloc.Open()

Dim cmd As New SqlCommand(queryModifica, Me.connloc)

cmd.Parameters.Add("@elemento", e.Item.Cells(1).Text)


Dim read As SqlDataReader

read = cmd.ExecuteReader(CommandBehavior.CloseConnection)
Me.connloc.Close()
'read.Close()

Catch ex As Exception

Finally

'refresh della griglia dopo l'accettazione di un determinato record
If Me.LstRic.SelectedItem.Value = "SpA_Order" Then

Me.connloc.Open()


Dim cmd As New SqlCommand(queryOrdspadett, Me.connloc)
cmd.Parameters.Add("@elemento", Me.Lstcampi.SelectedItem.Value)

Dim read As SqlDataReader

read = cmd.ExecuteReader(CommandBehavior.CloseConnection)

Me.DgItem.DataSource = read
Me.DgItem.DataBind()
Me.DgItem.Visible = True
Me.Lblitem.Visible = True

ElseIf Me.LstRic.SelectedItem.Value = "Shipping_Date" Then

Me.txtcerca.Text = Me.Calendar1.SelectedDate.ToShortDateString
Dim start As String
start = Year(Me.txtcerca.Text) & " / " & Month(Me.txtcerca.Text) & " / " & Day(Me.txtcerca.Text)
Dim ends As String
ends = Year(Me.txtcerca.Text) & "/" & Month(Me.txtcerca.Text) & "/" & Day(Me.txtcerca.Text) & " " & "23:59:59.999"



Dim queryData As String = "select * " & _
"from Ord_web where " & _
"(Shipping_Date between '" & start & "' and '" & ends & "') " & _
"and (id <> 1) " & _
"order by Shipping_Date"

Me.connloc.Open()

Dim cmd As New SqlCommand(queryData, Me.connloc)

Dim reads As SqlDataReader

reads = cmd.ExecuteReader(CommandBehavior.CloseConnection)

Me.DgItem.DataSource = reads
----> Me.DgItem.DataBind() <-----ERRORE

Me.DgItem.Visible = True
Me.Lblitem.Visible = True
End If

End Try
End If
End Sub

SE SI VERIFICA LA CONDIZIONE Me.LstRic.SelectedItem.Value = "SpA_Order" tutto funziona cioe' mi trovo il datagrid aggiornato
invece SE SI VERIFICA LA CONDIZIONE "Shipping_Date" mi da errore:
Invalid attempt to Read when reader is closed.


non ho capito!!!
mi puoi dire qualcosa in merito??

bluland Profilo | Guru

scusami Marco ma l'ho ripostata perche'pensavo di dover aprire un altro thread per questa domanda, la formattazione per renderizzare il codice non ho capito bene come si fa!!

bluland Profilo | Guru

grazie
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?

Dopo esserti registrato potrai chiedere
aiuto sul nostro Forum oppure aiutare gli altri

Consulta le Stanze disponibili.

Registrati ora !
Copyright © dotNetHell.it 2002-2025
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5