locked
Ways to troubleshoot a data connection? RRS feed

  • Question

  • User-2033772850 posted

    Hi, I am trying to teach myself ASP.net.  I'm working on this example: http://www.w3schools.com/aspnet/aspnet_dbconnection.asp

    Below is the code I'm using.  When I view in browser, I just get a blank page.  I'm not getting any errors... I'm thinking that if the repeater is empty, maybe the connection to the database is no good?  But I'm not sure how to troubleshoot with no error messages.  I've verified the database is in the location specified and that the tables and and field names are correct.  Any tips on how to troubleshoot this issue?

    Thanks in advance!

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
    
    <%@ Import Namespace="System.Data.OleDb" %>
    
    <script  runat="server">
       
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
            Dim dbconn, sql, dbcomm, dbread
            dbconn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\myname\Desktop\myfolder\mydb.accdb")
            dbconn.Open()
            sql = "SELECT DISTINCT myid FROM mytable"
            dbcomm = New OleDbCommand(sql, dbconn)
            dbread = dbcomm.ExecuteReader()
            customers.DataSource = dbread
            customers.DataBind()
            dbread.Close()
            dbconn.Close()
        End Sub
    </script>
    <!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></title>
    
    </head>
    <body>
         <form id="Form2" runat="server">
         
    <asp:Repeater id="customers" runat="server">
    
    <HeaderTemplate>
    <table border="1" width="100%">
    <tr bgcolor="#b0c4de">
    <th>myid</th>
    </tr>
    </HeaderTemplate>
    
    <ItemTemplate>
    <tr bgcolor="#f0f0f0">
    <td><%# Container.DataItem("myid")%> </td>
    </tr>
    </ItemTemplate>
    
    <FooterTemplate>
    </table>
    </FooterTemplate>
    
    </asp:Repeater>
    
    </form>
    </body>
    </html>



    Tuesday, March 19, 2013 2:12 PM

Answers

  • User-2033772850 posted

    I got it to work!  Indeed, the problem was that the page_load wasn't working.  Instead of what I had, I needed:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 19, 2013 4:37 PM

All replies

  • User281315223 posted

    Have you tried placing a breakpoint within your code and stepping through it as it is progressing to check the value of each of your variables?

    You'll also want to verify (as I am sure you have) that your table actually contains all of the necessary fields that you are querying. Have you also tried using the Eval method within your ItemTemplate as well : 

    <td><%# Eval("myid") %></td>

    Check the source of your rendered page to check if actual rows are being rendered properly and it is just an issue of the data not properly being displayed.

    Tuesday, March 19, 2013 2:42 PM
  • User-2033772850 posted

    Hi, thanks for your responses!

    Actually, there is nothing in the page source -- no header, now rows (output is below)... so maybe my problem is not the database connection and is actually the repeater?  I tried using the Eval method but the result is the same.  I have verified that the table and field I'm querying for exist, and have verified the database name and location... 

    <form method="post" action="Default.aspx" id="Form2">
    <div class="aspNetHidden">
    <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTAyMTY2OTU2N2RkythJsIww6bZPTifT+KWUK66HLv60TeLm0Hb/ShSzZ04=" />
    </div>
    
    </form>



    Tuesday, March 19, 2013 2:58 PM
  • User281315223 posted

    Yeah it looks like there is an issue with the repeater in this case. You may want to try setting the DataSource within your Repeater to use to bind these values that you are using from your query rather than dynamically if that is an option, but your code appears correct from here.

    (I'm not currently around a development environment to make an example, but that is likely the case).

    Tuesday, March 19, 2013 3:12 PM
  • User-2033772850 posted

    I am starting to think that the whole page_load isn't working.  I tried adding a Response.Write to check and I'm still getting a blank page.  This seems so basic!  I'm not sure what I'm missing.

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
    
    <%@ Import Namespace="System.Data.OleDb" %>
    
    <script  runat="server">
    
    Sub Page_Load
            Response.Write("hello")  
    End Sub
    
    </script>
    
    



    Tuesday, March 19, 2013 3:56 PM
  • User-2033772850 posted

    I got it to work!  Indeed, the problem was that the page_load wasn't working.  Instead of what I had, I needed:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 19, 2013 4:37 PM
  • User281315223 posted

    That will certainly explain why your code wasn't rendering at all :) 

    I'm glad you got it all taken care of.

    Tuesday, March 19, 2013 4:39 PM