Torna al Thread
private sub button_click()
Dim Where As String = String.Empty
Using MyConnection As SqlConnection = New SqlConnection()
MyConnection.ConnectionString = ConfigurationManager.ConnectionStrings("MyConnectionString").ConnectionString
Using MyCommand As SqlCommand = New SqlCommand()
Dim sqlAdapter As New SqlDataAdapter()
' creo nuova tabella
Dim Table As New DataTable()
MyCommand.CommandType = CommandType.Text
MyCommand.Connection = MyConnection
MyCommand.Parameters.Clear()
'inizio i parametri per i filtri
If Not String.IsNullOrEmpty(ddlanno.SelectedValue.ToString) Then
MyCommand.Parameters.AddWithValue("@anno", ddlanno.SelectedValue.ToString)
Where = Where & " WHERE Anno = @anno "
End If
If Not String.IsNullOrEmpty(ddlcentro.SelectedValue.ToString) Then
MyCommand.Parameters.AddWithValue("@centro", ddlcentro.SelectedValue.ToString)
If Not String.IsNullOrEmpty(Where) Then
Where = Where & " AND Codcen = @centro "
Else
Where = Where & " WHERE Codcen = @centro"
End If
End If
' ....altri parametri
If MyCommand.Parameters.Count = 0 Then
lblfiltri.Visible = True
lblfiltri.Text = "nessun filtro impostato"
Exit Sub
Else
lblfiltri.Visible = False
End If
MyCommand.CommandText = " SELECT ..." & _
" FROM (SELECT ..." & _
" ..." & Where & ") AS Tab1" & _
" GROUP BY ..."
Try
MyCommand.Connection.Open()
sqlAdapter = New SqlDataAdapter(MyCommand.CommandText, "Data Source=xx;Initial Catalog=xx;User ID=xx;Password=xx")
sqlAdapter.SelectCommand = MyCommand
sqlAdapter.Fill(Table)
Session("Table") = Table
GridView1.DataSource = Session("Table")
GridView1.DataBind()
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
If GridView1.Rows.Count = 0 Then
lblrighe.Visible = True
lblrighe.Text = "nessun risultato per i criteri impostati"
Else
lblrighe.Visible = False
End If
End Using
End Using
End Sub