Results 1 to 11 of 11

Thread: access database

  1. #1

    Thread Starter
    Hyperactive Member pgrimes's Avatar
    Join Date
    Aug 2001
    Location
    sacramento
    Posts
    342

    access database

    I'm having trouble accessing data from an access database. I'm used to database interactions using asp 3.0. I'm using some code that was provided by "pvb" in this thread:
    http://vbforums.com/showthread.php?t...ccess+database

    But, none of the data is being displayed on the page. Here is the code as I have implemented it.

    I don't get any errors or anything, I just see a blank page when I load it. Shouldn't the data from my database appear in the datagrid when the page is loaded?

    Thanks for any help!
    VB Code:
    1. Imports System.Data
    2. Imports System.Data.OleDb
    3.  
    4. Public Class WebForm1
    5.     Inherits System.Web.UI.Page
    6.     Protected WithEvents gridAuthors As System.Web.UI.WebControls.DataGrid
    7. #Region " Web Form Designer Generated Code "
    8.  
    9.     'This call is required by the Web Form Designer.
    10.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    11.  
    12.     End Sub
    13.  
    14.     Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
    15.         'CODEGEN: This method call is required by the Web Form Designer
    16.         'Do not modify it using the code editor.
    17.         InitializeComponent()
    18.     End Sub
    19.  
    20. #End Region
    21.  
    22.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    23.         If Not Page.IsPostBack Then
    24.             bindAuthors()
    25.         End If
    26.     End Sub
    27.     Protected Sub bindAuthors()
    28.         Dim mdbPath As String = "G:\WebServer\cpthandler\database\cptDataInfo.mdb"
    29.         Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mdbPath
    30.         Dim cn As New OleDbConnection(connString)
    31.         Dim cmdText As String = "SELECT CONTACT_NAME FROM cptDataInfo"
    32.         Dim cmd As New OleDbCommand(cmdText, cn)
    33.         cmd.Connection.Open()
    34.  
    35.         gridAuthors.DataSource = cmd.ExecuteReader(CommandBehavior.CloseConnection)
    36.         gridAuthors.DataBind()
    37.  
    38.     End Sub
    39.  
    40.  
    41. End Class

    Here's the html source:
    Code:
    <%@ Page Language="vb" AutoEventWireup="false" Codebehind="createCPTxml.aspx.vb" Inherits="createCPTxml.WebForm1"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    	<HEAD>
    		<title>WebForm1</title>
    		<meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
    		<meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
    		<meta name="vs_defaultClientScript" content="JavaScript">
    		<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    	</HEAD>
    	<body MS_POSITIONING="GridLayout">
    		<form runat="server">
    			<asp:DataGrid Runat="server" AutoGenerateColumns="True" ID="gridAuthors" />
    		</form>
    	</body>
    </HTML>
    I don't get any errors or anything, I just see a blank page when I load it. Shouldn't the data from my database appear in the datagrid when the page is loaded?

  2. #2
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    This only works when you're not using codebehind:
    VB Code:
    1. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    2.     If Not Page.IsPostBack Then
    3.         bindAuthors()
    4.     End If
    5. End Sub
    You have to add Handles MyBase.Load if you use codebehind otherwise that event never fires(which results in a blank page with no errors):
    VB Code:
    1. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.     If Not Page.IsPostBack Then
    3.         bindAuthors()
    4.     End If
    5. End Sub

  3. #3

    Thread Starter
    Hyperactive Member pgrimes's Avatar
    Join Date
    Aug 2001
    Location
    sacramento
    Posts
    342
    Thanks for the reply pvb.

    It still does the same thing though. Nothing shows up. I don't have to iterate through the records or anything to make them appear? The database is populated.

    Thanks,

    Paul

  4. #4

    Thread Starter
    Hyperactive Member pgrimes's Avatar
    Join Date
    Aug 2001
    Location
    sacramento
    Posts
    342
    Hi, I got the code listed below to do what I'm shooting for. But, I'm having trouble getting it to work using "codebehind". Could anyone point me in the right direction? How would I modify this to work using codebehind. The things I've tried are unsuccessful.

    Thanks... Paul
    VB Code:
    1. <%@ Import Namespace="System.Data.OleDb" %><script runat="server">
    2. sub Page_Load
    3. dim dbconn,sql,dbcomm,dbread
    4. dbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data source=G:\WebServer\cpthandler\database\cptDataInfo.mdb")
    5. dbconn.Open()
    6. sql="SELECT * FROM cptDataInfo"
    7. dbcomm=New OleDbCommand(sql,dbconn)
    8. dbread=dbcomm.ExecuteReader()
    9. cptData.DataSource=dbread
    10. cptData.DataBind()
    11. dbread.Close()
    12. dbconn.Close()
    13. end sub
    14. </script><html>
    15. <body><form runat="server">
    16. <asp:Repeater id="cptData" runat="server"><HeaderTemplate>
    17. <table border="1" width="100%">
    18. <tr>
    19. <th>ID</th>
    20. <th>Cone Used</th>
    21. <th>Latitude</th>
    22. <th>Longitude</th>
    23. </tr>
    24. </HeaderTemplate><ItemTemplate>
    25. <tr>
    26. <td><%#Container.DataItem("ID")%></td>
    27. <td><%#Container.DataItem("CONE_USED")%></td>
    28. <td><%#Container.DataItem("LATITUDE")%></td>
    29. <td><%#Container.DataItem("LONGITUDE")%></td>
    30. </tr>
    31. </ItemTemplate><FooterTemplate>
    32. </table>
    33. </FooterTemplate></asp:Repeater>
    34. </form></body>
    35. </html>

  5. #5
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    Ok, in this example(almost the same as the other example) I'm using the biblio access database that ships with VS6.0. Here's the aspx....
    Code:
    <%@ Page Language="vb" AutoEventWireup="false" Codebehind="DataBoundGrid.aspx.vb" Inherits="localhost.DataBoundGrid"%>
    <html>
    	<body>
    		<form runat="server">
    			<asp:DataGrid ID="gridPublishers" Runat="server" AutoGenerateColumns="True"/>
    		</form>
    	</body>
    </html>
    and here's the codebehind:
    VB Code:
    1. Imports System
    2. Imports System.Data
    3. Imports System.Data.OleDb
    4.  
    5. Public Class DataBoundGrid : Inherits System.Web.UI.Page
    6.     Protected gridPublishers As DataGrid
    7.     Private Sub bindPublishers()
    8.         Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    9.                                     "data source=F:\msaccess_data\biblio2002.mdb"
    10.         Dim cn As New OleDbConnection(connString)
    11.         Dim cmdText As String = "Select * From Publishers"
    12.         Dim cmd As New OleDbCommand(cmdText, cn)
    13.         cmd.Connection.Open()
    14.         gridPublishers.DataSource = cmd.ExecuteReader(CommandBehavior.CloseConnection)
    15.         gridPublishers.DataBind()
    16.     End Sub
    17.     Protected Overrides Sub OnInit(ByVal e As EventArgs)
    18.         If Not Page.IsPostBack Then
    19.             bindPublishers()
    20.         End If
    21.         MyBase.OnInit(e)
    22.     End Sub
    23. End Class
    this is cut and pasted from a working example so i know it works. If you still get a blank screen, amongst other things, you can change bindPublishers to the following just for testing:
    VB Code:
    1. Private Sub bindPublishers()
    2.     Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    3.                                 "data source=F:\msaccess_data\biblio2002.mdb"
    4.     Dim cn As New OleDbConnection(connString)
    5.     Dim cmdText As String = "Select * From Publishers"
    6.     Dim cmd As New OleDbCommand(cmdText, cn)
    7.     Dim da As New OleDbDataAdapter(cmd)
    8.     Dim ds As New DataSet
    9.     da.Fill(ds)
    10.     If (ds.Tables.Count = 0) Then
    11.         Throw New Exception("No tables")
    12.     Else
    13.         If (ds.Tables(0).Rows.Count = 0) Then
    14.             Throw New Exception("No rows")
    15.         End If
    16.     End If
    17.     gridPublishers.DataSource = ds
    18.     gridPublishers.DataBind()
    19. End Sub

  6. #6

    Thread Starter
    Hyperactive Member pgrimes's Avatar
    Join Date
    Aug 2001
    Location
    sacramento
    Posts
    342
    Sweet! That worked fine. I should be able to manage what I need to do from here. Thanks a lot for the help!

    Paul

  7. #7

    Thread Starter
    Hyperactive Member pgrimes's Avatar
    Join Date
    Aug 2001
    Location
    sacramento
    Posts
    342
    Ok, so I have another question. How can I access each field from the database (one at a time). It seemed so much easier to me in ASP 3. All I had to do was get whatever field values I wanted while I looped through my recordset without reaching EOF. How can I do the same thing with asp.net?

    Example:
    VB Code:
    1. <%
    2. Set objRS = Server.CreateObject ("ADODB.Recordset")
    3. objRS.Open strSQL
    4.  
    5. While Not objRS.EOF
    6. %>
    7.     <td><%=objRS("lName")%></td>
    8.     <td><%=objRS("username")%></td>
    9.     <td><%=objRS("password")%></td>
    10. <%
    11. objRS.MoveNext
    12.  
    13. Wend
    14. %>
    Thanks,

    Paul

  8. #8
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    Here's one way you can do it with a repeater control and databinding. Using that biblio mdb again, here's the aspx page:
    Code:
    <%@ Page Language="vb" AutoEventWireup="false" Codebehind="DataBinding.aspx.vb" Inherits="localhost.DataBinding"%>
    <html>
    	<body>
    		<form runat="server">
    			<table>
    				<tr>
    					<td>PubID</td>
    					<td>Name</td>
    					<td>Company</td>
    				</tr>
    				<asp:Repeater ID="rptrPublishers" Runat="server">		
    					<ItemTemplate>
    						<tr>
    							<td><%# DataBinder.Eval ( Container.DataItem , "PubID" ) %></td>
    							<td><%# DataBinder.Eval ( Container.DataItem , "Name" ) %></td>
    							<td><%# DataBinder.Eval ( Container.DataItem , "CompanyName" ) %></td>
    						</tr>
    					</ItemTemplate>				
    				</asp:Repeater>
    			</table>
    		</form>
    	</body>
    </html>
    and here's the code behind:
    VB Code:
    1. Imports System
    2. Imports System.Data
    3. Imports System.Data.OleDb
    4. Public Class DataBinding : Inherits System.Web.UI.Page
    5.  
    6.     Protected rptrPublishers As Repeater
    7.  
    8.     Protected Overrides Sub OnInit(ByVal e As EventArgs)
    9.         If Not Page.IsPostBack Then
    10.             bindPublishers()
    11.         End If
    12.         MyBase.OnInit(e)
    13.     End Sub
    14.  
    15.     Private Sub bindPublishers()
    16.         rptrPublishers.DataSource = getData()
    17.         rptrPublishers.DataBind()
    18.     End Sub
    19.  
    20.     Private Function getData() As DataSet
    21.         Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    22.                                             "data source=F:\msaccess_data\biblio2002.mdb"
    23.         Dim cn As New OleDbConnection(connString)
    24.         Dim cmdText As String = "Select * From Publishers"
    25.         Dim cmd As New OleDbCommand(cmdText, cn)
    26.         Dim da As New OleDbDataAdapter(cmd)
    27.         Dim ds As New DataSet
    28.         da.Fill(ds)
    29.         Return ds
    30.     End Function
    31.  
    32. End Class

  9. #9

    Thread Starter
    Hyperactive Member pgrimes's Avatar
    Join Date
    Aug 2001
    Location
    sacramento
    Posts
    342
    Thanks again for the help pvb. One more question.

    How do I access each cell in my code segment? Like the part in comments below....

    example:
    VB Code:
    1. Dim cn As New OleDbConnection(connString)
    2. Dim cmdText As String = "Select * From Publishers"
    3. Dim cmd As New OleDbCommand(cmdText, cn)
    4. Dim da As New OleDbDataAdapter(cmd)
    5. Dim ds As New DataSet
    6.  
    7. Dim someVar As String
    8.  
    9. da.Fill(ds)
    10.  
    11. 'While Not da.EOF
    12. 'If da('aFieldName') = "desiredString" Then
    13. '  someVar = da('aFieldName')
    14. 'End If
    15.  
    16. 'da.MoveNext
    17. 'Wend

  10. #10
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fort Collins, CO
    Posts
    366
    Well, at that point you could do the following(i'm not really sure what you're tryin to do but here's how you'd iterate through the dataset):
    VB Code:
    1. For Each row As DataRow In ds.Tables(0).Rows
    2.     If row("Name").ToString() = "MyName" Then
    3.         'do something
    4.     End If
    5. Next
    You can also intercept the item when it's being created and modify it before it's output is rendered to the client(the following just makes the values in the Name column bold):
    VB Code:
    1. Imports System
    2. Imports System.Data
    3. Imports System.Data.OleDb
    4. Public Class DataBinding : Inherits System.Web.UI.Page
    5.  
    6.     Protected WithEvents rptrPublishers As Repeater
    7.  
    8.     Protected Overrides Sub OnInit(ByVal e As EventArgs)
    9.         If Not Page.IsPostBack Then
    10.             bindPublishers()
    11.         End If
    12.         MyBase.OnInit(e)
    13.     End Sub
    14.  
    15.     Private Sub bindPublishers()
    16.         rptrPublishers.DataSource = getData()
    17.         rptrPublishers.DataBind()
    18.     End Sub
    19.  
    20.     Private Function getData() As DataSet
    21.         Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    22.                                             "data source=F:\msaccess_data\biblio2002.mdb"
    23.         Dim cn As New OleDbConnection(connString)
    24.         Dim cmdText As String = "Select * From Publishers"
    25.         Dim cmd As New OleDbCommand(cmdText, cn)
    26.         Dim da As New OleDbDataAdapter(cmd)
    27.         Dim ds As New DataSet
    28.         da.Fill(ds)
    29.         Return ds
    30.     End Function
    31.  
    32.     Private Sub rptrPublishers_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptrPublishers.ItemCreated
    33.         If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Then
    34.             Dim dr As DataRowView = CType(e.Item.DataItem, DataRowView)
    35.             dr.Item("Name") = "<b>" & dr.Item("Name") & "</b>"
    36.         End If
    37.     End Sub
    38. End Class

  11. #11

    Thread Starter
    Hyperactive Member pgrimes's Avatar
    Join Date
    Aug 2001
    Location
    sacramento
    Posts
    342
    Thanks a lot! I'll give that a whirl.

    FYI, I'm going to be using the data from the database to show to the client (in the datagrid) and to write to dynamically generated xml files at the same time. When this page loads, it will take the desired data, display it and create an xml file containing it.

    Thanks again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width