Wrox Programmer Forums
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 13th, 2004, 03:54 PM
Authorized User
 
Join Date: Jun 2004
Posts: 44
Thanks: 0
Thanked 0 Times in 0 Posts
Default In the dark with VS

Can anyone help with Visual Studio?
I have the following result for the Wrox United Players page with a whole load of Result data added, how can I stop this appearing and what generates it?
************************************************** ************
Wrox United
Players
  PlayerID PlayerName Profile JoinDate
1 Chris Hart Enter profile here 1/1/2003 00:00:00
2 Chris Ullman Excellent forward 1/1/2003 00:00:00
3 Dave Sussman Good in defence 1/1/2003 00:00:00
4 John Kauffman Recommended in goal 1/1/2003 00:00:00

--------------------------------------------------------------
Request Details
Session Id: nb104fzs5savar55da5tgn2m Request Type: GET
Time of Request: 7/13/2004 21:21:55 Status Code: 200
Request Encoding: Unicode (UTF-8) Response Encoding: Unicode (UTF-8)
Trace Information
Category Message From First(s) From Last(s)
aspx.page Begin Init
aspx.page End Init 0.000043 0.000043
aspx.page Begin PreRender 0.051637 0.051594
aspx.page End PreRender 0.051720 0.000084
aspx.page Begin SaveViewState 0.053210 0.001489
aspx.page End SaveViewState 0.053841 0.000632
aspx.page Begin Render 0.053899 0.000058
aspx.page End Render 0.056019 0.002120
Control Tree
Control Id Type Render Size Bytes (including children) Viewstate Size Bytes (excluding children)
__PAGE ASP.Players_aspx 2661 20
    _ctl0 System.Web.UI.LiteralControl 46 0
    Form1 System.Web.UI.HtmlControls.HtmlForm 2594 0
        _ctl1 System.Web.UI.LiteralControl 76 0
        DataGrid1 System.Web.UI.WebControls.DataGrid 589 656
            DataGrid1:_ctl0 System.Web.UI.WebControls.DataGridTable 589 0
                DataGrid1:_ctl1 System.Web.UI.WebControls.DataGridItem 86 0
                    DataGrid1:_ctl1:_ctl0 System.Web.UI.WebControls.TableCell 19 0
                    DataGrid1:_ctl1:_ctl1 System.Web.UI.WebControls.TableCell 19 0
                    DataGrid1:_ctl1:_ctl2 System.Web.UI.WebControls.TableCell 16 0
-----------------------------------------------------------------

My code is as follows:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~
<%@ Page Language="VB" Debug="true" Trace="true" %>
<%@ Import Namespace="System.Data.Oledb" %>
<%@ Import Namespace="System.Data" %>


<script runat="server">

   Sub Page_Load(Sender As Object, e As EventArgs)
      Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; " & _
        "Data Source=C:\BegASPNET11\WroxUnited\Database\WroxUnit ed.mdb"
      Dim dbConnection As New OleDbConnection(ConnectionString)

      Dim queryString As String = _
         "Select PlayerID, PlayerName, Profile, JoinDate FROM Players"
      Dim dbCommand As New OleDbCommand

      dbCommand.CommandText = queryString
      dbCommand.connection = dbConnection
      Dim dataAdapter As New OleDbDataAdapter
      dataAdapter.SelectCommand = dbCommand
      Dim data As DataSet = New System.Data.DataSet
      dataAdapter.Fill(Data, "Players")

      DataGrid1.DataSource = data.tables("Players")
      DataGrid1.DataBind()

      dbCommand.connection.Close()
      dbConnection.Close()

   End Sub
</script>

<HTML>
    <HEAD>
    </HEAD>
    <body>
        <form runat="server" ID="Form1">
            <h1>Wrox United
            </h1>
            <h2>Players
            </h2>
            <p>&nbsp;
                <asp:DataGrid id="DataGrid1" runat="server"></asp:DataGrid>
            </p>
        </form>
    </body>
</HTML>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~
It is derived from an earlier chapter (I wasn't sure which line to use to close the connection? Either of the first or second lines or both, makes no difference to the result)
Not using Web matrix I don't have the wizard that built the code listed in pages 372 & 373 apart from which some of the controls like </wmx:AccessDataSourceControl> does not appear to be part of Visual Studio's controles?
Can anyone through any light on my problem?
Regards,

Edward.
 
Old July 13th, 2004, 04:29 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Somewhere along the line you have Tracing turned on for the page. This has nothing to do with Visual Studio. There are several places you can try, first look at the beginning of the page you are running. Look for Trace="True". Also, look in the web.config for the trace node. In it is an attribute: "enabled". It could be set to true.
 
Old July 14th, 2004, 04:34 PM
Authorized User
 
Join Date: Jun 2004
Posts: 44
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi planoie, that is it, Trace has got into the first line of my page. I have looked at the exercise that I used as the base for editing and it isn't there - anyway thanks.

My code above is as follows:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~
<%@ Page Language="VB" Debug="true" Trace="true" %>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~

I initially tried to write the above code into the back page of my Visual Studio web form Sub Page_Load() but I couldn't get the 'Import' accepted without a declaration which means that I am not connected to the Import Class, the reserved word that VS accepts is 'Imports' which presumably is not the same class file.
Any advice on how I can write thic code in the back page and get it working?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~

My Code:
Public Class Players
    Inherits System.Web.UI.Page
    Import Namespace="System.Data.Oledb" (Import <-- Declaration expected)
    Imports Namespace="System.Data" (Imports <-- 'Imports' statements must precede any declaration)


    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
        Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; " & _
          "Data Source=C:\BegASPNET11\WroxUnited\Database\WroxUnit ed.mdb"
        Dim dbConnection As New OleDbConnection(connectionString) <-- Type OleDbConnection is not defined

        Dim queryString As String = _
           "Select PlayerID, PlayerName, Profile, JoinDate FROM Players"
        Dim dbCommand As New OleDbCommand<-- Type OleDbcommand is not defined

        dbCommand.CommandText = queryString
        dbCommand.connection = dbConnection
        Dim dataAdapter As New OleDbDataAdapter <-- Type OleDbDataAdapter is not defined
        dataAdapter.SelectCommand = dbCommand
        Dim data As DataSet = New System.Data.DataSet
        dataAdapter.Fill(data, "Players")

        DataGrid1.DataSource = data.Tables("Players")
        DataGrid1.DataBind()

        dbCommand.connection.Close()
        dbConnection.Close()
    End Sub

End Class
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~


 
Old July 14th, 2004, 04:47 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Hello,

For your code behind page, the Imports statement must be at the top, above the class file. An example would be:

Imports System.Data
Imports System.Data.OLEDB

Public Class ...
  ..
End Class

In the code-behind page, this is how it appears. The syntax of:

Imports Namespace="System.Data.OLEDB"

Is what you would enter into the presentation page (.aspx). That would be important if you code in-line, where the code is in the same page as the HTML and server controls.

Hope this helps,

Brian
 
Old July 14th, 2004, 04:51 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Edward,

Import is an instruction, rather than a class. For some weird reasons, in the ASPX page you need Import to reference a namespace (and the keyword Namespace itself), while in the Code Behind, all you need is the keyword Imports, like this:
Code:
Public Class Players
  Inherits System.Web.UI.Page

Imports  System.Data.Oledb    
Imports System.Data
Although it's supported, it's not recommend (no best practice) to import inside a Class or Namespace, so it's probably better to use this code:
Code:
Imports  System.Data.Oledb    
Imports System.Data

Namespace MyNamespace
  Public Class Players
    Inherits System.Web.UI.Page

  ' ...
  End Class
End Namespace
HtH,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Warning by Incubus (Track 8 from the album: Morning View) What's This?
 
Old July 15th, 2004, 09:27 AM
Authorized User
 
Join Date: Jun 2004
Posts: 44
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Brian & Imar your advice has allowed the OleDbConnection, OleDbcommand & OleDbDataAdapter classes to be recognized with a tip message saying what they are.
One more thing remains, the DataGrid1 placed on the front page designer cannot be seen from inside the back page Public Class Players > Sub Page_Load() how can I get this into scope?
Always appreciative of all your help!
Edward.

 
Old July 15th, 2004, 11:19 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Is there a line in the page codebehind that looks like this:

Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid

This brings the DataGrid into the class.

(What this is doing, technically is:
You declare the datagrid as a protected member of the page class. Then when ASP.NET creates the object instance of your .aspx file that object is derived from your code-behind page class. This is what the "Inherits=..." attribute is for in the "@ Page" directive. Because the page is inheriting from the class defined in the code-behind, the controls are "linked up" from the codebehind to the markup.)
 
Old July 16th, 2004, 11:23 AM
Authorized User
 
Join Date: Jun 2004
Posts: 44
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi planoie, no the line 'Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid' was missing. I have done two things, firstly insert the line into the back page between the sub s in the #Region " Web Form Designer Generated Code " which got rid of the Error underlining, however the table data was not passed to the grid1, so Ibuilt the page again this time my first action was to drag onto the desiner the Grid tool which placed the above line into the back page - great! however, after writing the remaining lines the new Players page still fails to pass the Players Table data to the front page when viewed in the browser?
What have I missed?
My current code:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~
Imports System.Data.Oledb
Imports System.Data
Public Class Players2
    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 DataGrid1 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
        Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; " & _
        "Data Source=C:\BegASPNET11\WroxUnited\Database\WroxUnit ed.mdb"
        Dim dbConnection As New OleDbConnection(connectionString)

        Dim queryString As String = _
           "Select PlayerID, PlayerName, Profile, JoinDate FROM Players"
        Dim dbCommand As New OleDbCommand

        dbCommand.CommandText = queryString
        dbCommand.Connection = dbConnection
        Dim dataAdapter As New OleDbDataAdapter
        dataAdapter.SelectCommand = dbCommand
        Dim data As DataSet = New System.Data.DataSet
        dataAdapter.Fill(data, "Players")

        DataGrid1.DataSource = data.Tables("Players")
        DataGrid1.DataBind()

        dbCommand.Connection.Close()
        dbConnection.Close()
    End Sub
End Class

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~

 
Old July 16th, 2004, 11:40 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Edward,

Is this the entire code for the page? I tried your example and it runs fine. I get a nice datagrid with four records. Can you post the code for the "Code In Front" (tm) page? (That is, the code in the actual ASPX page?)

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old July 19th, 2004, 07:10 AM
Authorized User
 
Join Date: Jun 2004
Posts: 44
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Imar, thanks for your reply, below is the code from the front page:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~
<%@ Page Language="VB" Debug="true" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<HTML>
  <HEAD>
    <title>Players2</title>
      <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
      <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
      <meta name="vs_defaultClientScript" content="JavaScript">
      <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body>
      <form id="Form1" method="post" runat="server">
        <H1>Wrox United</H1>
        <H3>Players
        </H3>
        <P>&nbsp;</P>
        <H3>
        <asp:DataGrid id="DataGrid1" runat="server"></asp:DataGrid></H3>
    </form>
    </body>
</HTML>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~
unfortunately my version returns an empty datagrid. The data is available since I have a page with the code similar to the downloaded code which works and returns the four records, I must however learn how to use the back page!
I have tried passing the data to the frontpage datagrid using a Session variable which I take to be like a global variable, however, in my front page this variable requires to be declared as does Datagrid1 which already appears as an <asp:DataGrid > with the id="DataGrid1" in the HTML section of the code in the same page.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~
<Script runat="server">
' DataGrid1.DataSource = Session("DataGrid")
' DataGrid1.DataBind()
</Script>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~
What have I missed?
Thanks in anticipation.
Edward.










Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.