HELP!

G

Guest

I am having a problem with a datagrid I am trying to use. I have an Access
DB with two records I want to display. All that is displayed is the heading
for the datagrid. I can't seem to display the actual data.

Below is the Code Behind Portion:

Imports System.Data.OleDb

Public Class Units
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents UnitList As System.Web.UI.WebControls.DataGrid
Protected WithEvents UnitLists As System.Web.UI.WebControls.DataGrid

'NOTE: The following placeholder declaration is required by the Web Form
Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
BindDataGrid()
End Sub

Sub BindDataGrid()
Dim strSQL As String = "SELECT * FROM Unit_General"
Dim dbconn As New
OleDb.OleDbConnection(System.Configuration.ConfigurationSettings.AppSettings.Get("dsn"))
Dim dt As DataTable
Dim dr As DataRow
Dim dret As OleDb.OleDbDataReader
Dim i As Integer

dbconn.Open()
Dim Command As New OleDb.OleDbCommand(strSQL, dbconn)
dret = Command.ExecuteReader(CommandBehavior.CloseConnection)

Try
'create a DataTable
dt = New DataTable
dt.Columns.Add(New DataColumn("Unit Number", GetType(Integer)))
dt.Columns.Add(New DataColumn("Price", GetType(Double)))
dt.Columns.Add(New DataColumn("SqFt", GetType(String)))
dt.Columns.Add(New DataColumn("Floor Plan", GetType(String)))
dt.Columns.Add(New DataColumn("Tenant Status", GetType(String)))
dt.Columns.Add(New DataColumn("Beds", GetType(Integer)))
dt.Columns.Add(New DataColumn("Baths", GetType(Integer)))

Do While dret.Read()
dr = dt.NewRow()
dr(0) = dret(13)
dr(1) = dret(2)
dr(2) = dret(3)
dr(3) = dret(4)
dr(4) = dret(9)
dr(5) = dret(6)
dr(6) = dret(7)

'add the row to the datatable
dt.Rows.Add(dr)
Loop

Catch
End Try

dbconn.Close()
Command.Connection.Close()

'return a DataView to the DataTable
UnitLists.DataSource = New DataView(dt)
UnitLists.DataBind()

End Sub
End Class

THE ASPX REFERENCE TO THE DATAGRID IS AS FOLLOWS:

<asp:datagrid id="UnitLists" runat="server" ForeColor="Black"
GridLines="Horizontal" CellPadding="4"
BackColor="White" BorderWidth="1px" BorderStyle="None"
BorderColor="#CCCCCC" Width="696px"
DataKeyField="Unit Number" AutoGenerateColumns="False"
EditItemStyle-BackColor="yellow" PageSize="50"
AllowSorting="True">
<SelectedItemStyle Font-Bold="True" ForeColor="White"
BackColor="#CC3333"></SelectedItemStyle>
<EditItemStyle BackColor="Yellow"></EditItemStyle>
<AlternatingItemStyle BackColor="#E0E0E0"></AlternatingItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="White"
BackColor="#333333"></HeaderStyle>
<FooterStyle ForeColor="Black" BackColor="#CCCC99"></FooterStyle>
<Columns>
<asp:BoundColumn DataField="Unit Number" ReadOnly="True"
HeaderText="Unit Number"></asp:BoundColumn>
<asp:BoundColumn DataField="Price" ReadOnly="True"
HeaderText="Price"></asp:BoundColumn>
<asp:BoundColumn DataField="SqFt" ReadOnly="True"
HeaderText="SqFt"></asp:BoundColumn>
<asp:BoundColumn DataField="Floor Plan" ReadOnly="True"
HeaderText="Floor Plan"></asp:BoundColumn>
<asp:BoundColumn DataField="Tenant Status" ReadOnly="True"
HeaderText="Tenant Status"></asp:BoundColumn>
<asp:BoundColumn DataField="Beds" ReadOnly="True"
HeaderText="Beds"></asp:BoundColumn>
<asp:BoundColumn DataField="Baths" ReadOnly="True"
HeaderText="Baths"></asp:BoundColumn>
</Columns>
<PagerStyle HorizontalAlign="Right" ForeColor="Black"
BackColor="White"></PagerStyle>
</asp:datagrid>


Any help is greatly appreciated!

Sincerely,
ryan
 
P

Patrick Olurotimi Ige

Having a quick glance at ur code i guess maybe its becos u are closing
the ExecuteReader connection before adding/creating the Datatabe

I was also thinking u didn't set AutoGenerateColumns = true in ur
DataGrid but u don't need it as u explicitly specify what columns should
appear in the DataGrid via BoundColumns.

Patrick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,930
Messages
2,570,072
Members
46,522
Latest member
Mad-Ram

Latest Threads

Top