VWD2010 VB网站发布至IIS报错:The IListSource does not contain any data sources
Hey there, let's work through this error step by step. The System.Web.HttpException: The IListSource does not contain any data sources usually means your GridView is being bound to an empty DataSet when running on IIS—even though it works locally. Here are the key fixes and checks to resolve this:
1. Fix the Typo in Your GetData Function
First, there's a small but critical typo in your data retrieval code that's probably causing the DataSet to never get populated:
' Original line (incorrect capitalization of the connection variable) Dim adapter As New SqlDataAdapter(queryString, Connection) ' Corrected line (matches your declared variable name) Dim adapter As New SqlDataAdapter(queryString, connection)
This typo means the SQL adapter isn't using your active database connection, so no data gets pulled into the DataSet.
2. Verify Database Permissions (Beyond Login Access)
You've granted login and database access to IIS APPPOOL\DefaultAppPool, but you also need to give the account SELECT permissions on the Logins table itself. Run this SQL on your server:
USE AuditLogons GO GRANT SELECT ON [dbo].[Logins] TO [DefaultAppPool]
Without this, the app pool account can connect to the database but can't read the table data, resulting in an empty DataSet.
3. Check Your web.config Connection String
Local and IIS environments often use different connection strings. Make sure your published web.config has the correct settings:
- If using Windows Authentication (which you are, via the app pool identity), ensure the string includes
Integrated Security=TrueorTrusted_Connection=True. - Avoid using local-only SQL instance names like
.\SQLEXPRESSunless the IIS server has that exact instance. Use the server's SQL instance name or(local)for a default instance.
4. Improve Error Handling to Diagnose Exact Issues
Your current catch block only shows a generic "Unable to connect" message, which hides real problems like permission errors or SQL syntax issues. Update the GetData function's catch block to show detailed errors:
Catch ex As Exception Message.Text = "Error Details: " & ex.Message & "<br>SQL Query: " & queryString End Try
This will let you see exactly what's failing when running on IIS—whether it's a permission denied error, a bad SQL query, or something else.
5. Fix SQL Injection Vulnerabilities (Bonus)
Your code directly concatenates user input into SQL queries, which is a huge security risk. Switch to parameterized queries to prevent SQL injection and avoid date/string format issues across environments. For example, update your BindDataAllUsers method:
Private Sub BindDataAllUsers() UserNM.Text = "" Message.Text = "" Dim startDate As DateTime = DateTime.Parse(StartDT.Text) Dim endDate As DateTime = DateTime.Parse(EndDT.Text).AddDays(1) ' Covers up to 23:59:59 cleanly Dim queryString As String = "SELECT * FROM [dbo].[Logins] WHERE Timestamp >= @StartDate AND Timestamp < @EndDate" Dim ds As DataSet = GetDataWithParams(queryString, { New SqlParameter("@StartDate", startDate), New SqlParameter("@EndDate", endDate) }) GridView1.DataSource = ds GridView1.DataBind() GridView1.Visible = True End Sub ' Add a parameter-friendly version of GetData Function GetDataWithParams(ByVal queryString As String, ByVal parameters() As SqlParameter) As DataSet Dim connectionString As String = ConfigurationManager.ConnectionStrings("AuditLogonsConnectionString").ConnectionString Dim ds As New DataSet() Try Using connection As New SqlConnection(connectionString) Using command As New SqlCommand(queryString, connection) command.Parameters.AddRange(parameters) Dim adapter As New SqlDataAdapter(command) adapter.Fill(ds) End Using End Using Catch ex As Exception Message.Text = "Error Details: " & ex.Message & "<br>SQL Query: " & queryString End Try Return ds End Function
Start with fixing the typo in GetData—that's the most likely immediate fix. Then use the improved error handling to confirm if permissions or connection strings are still causing issues.
内容的提问来源于stack exchange,提问作者Alex

