I put together a simple vb program, or so I thought. I am accessing DB2 tables and trying to pull the data in using a DB2 reader. What I'd like is to have the columns be set to width=auto. The number of columns depends on the DB2 table. I'm getting an error "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index".
Where am I going wrong?
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="VB_test_code._Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>SQL results from DB2</title> </head> <body> <form id="form1" runat="server"> <div> <asp:TextBox ID="tbxSQL" runat="server" Columns="80" Rows="9" TextMode="MultiLine"></asp:TextBox> <br /> </div> <div> <asp:Label ID="lblError" runat="server"></asp:Label> <br /> </div> <div> <asp:Button ID="btnRunSQL" runat="server" Text="Run SQL Script" /> <br /> </div> <div> <asp:GridView ID="dgResults" AutoGenerateColumns="true" runat="server"> </asp:GridView> </div> </form> </body> </html> Imports System Imports System.Data Imports System.IO Imports IBM.Data.DB2.iSeries Partial Public Class _Default Inherits System.Web.UI.Page Public mySQL As String = " " Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not Page.IsPostBack Then lblError.Visible = False End If End Sub Private Sub btnRunSQL_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRunSQL.Click Dim mySQL As String = Request.Form("tbxSQL") Dim strConnection As String = "DataSource=10.10.10.11;userid=LIVEXML;password=LIVE1;DefaultCollection=MYDATA031;" Dim myConnection As New iDB2Connection(strConnection) Dim myCommand As New iDB2Command(mySQL, myConnection) myCommand.Connection.Open() Dim myReader As iDB2DataReader Try myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection) If myReader.HasRows = False Then lblError.Text = "No rows returned for SQL statement" lblError.Visible = True Else Dim fields As Integer = myReader.FieldCount dgResults.DataSource = myReader dgResults.DataBind() ''''''Fix columns For colnum As Integer = 0 To fields - 1 dgResults.Columns(colnum).ItemStyle.Width = "auto" Next '''''' end fix myReader.Close() myConnection.Close() lblError.Text = " " lblError.Visible = False End If Catch ex As Exception lblError.Text = ex.Message lblError.Visible = True End Try End Sub End Class
Discuss This Question: