Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > .NET 1.0 and Visual Studio.NET > .NET Framework 1.x
| Search | Today's Posts | Mark Forums Read
.NET Framework 1.x For discussing versions 1.0 and 1.1 of the Microsoft .NET Framework.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the .NET Framework 1.x 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
  #1 (permalink)  
Old September 11th, 2006, 10:56 AM
Authorized User
 
Join Date: May 2006
Location: , , .
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem with session on Shopping Cart

Hello again!

I've been trying to put up a shopping cart with a items list page and a second one (cart). When I hit the "buy" button from one item I get this error on "cart.aspx":

"Object reference not set to an instance of an object"

Line 5: Dim ds as DataSet Line 6: ds = CType(Session("myDataSet"), Dataset) Line 7: MyDataGrid.Datasource=ds.Tables("Catalogo").Defaul tView Line 8: MyDataGrid.DataBind() Line 9: End Sub

 I have no clu where the error may come from so I'm gonna post both entire files code.

item list.aspx


<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDB" %>
   <%@ Page Language="VB" Debug="true" %>
<html>

<script language="VB" runat="server">

    Dim MyConnection As OleDBConnection

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
    If Not Page.IsPostBack() Then
     DataBind()
    dlsearchby.items.Add("produto")

    End If
    End Sub

    Private Sub DataBind()

     DIM MyConnection As New OleDBConnection("Provider=Microsoft.Jet.OLEDB.4.0; " & "Data Source=C:\Inetpub\wwwroot\DOT\InforJ\db\newBD.mdb" )
     Dim MyCommand As New OleDBDataAdapter("select * from catalogo where promo LIKE 'p%' order by promo", MyConnection)
     Dim DS As New DataSet

        If Not (Page.IsPostBack)

            MyCommand.Fill(DS)
        DS = Nothing
        DS = New DataSet()
    End If
Session("myDataSet") = DS
     MyCommand.Fill (DS, "Catalog")

            MyDataList.DataSource = DS.Tables(0).DefaultView
            MyDataList.DataBind()

    End Sub

Sub doSearch(Source as Object, E as EventArgs)
    Dim sSearchby as String
    MyDataList.visible="false"
    Dim strConn as string = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=C:\Inetpub\wwwroot\DOT\InforJ\db\newBD.mdb"
    Dim MySQL as string = "Select * from Catalogo where " & dlsearchby.selectedItem.text & _
    " Like '%" & txtSearch.text & "%' order by ID"
    Trace.Warn("sql = " & MySQL)
    Dim MyConn as New OleDBConnection(strConn)
    Dim objDR as OledbDataReader
    Dim Cmd as New OleDBCommand(MySQL, MyConn)
    MyConn.Open()
    objDR=Cmd.ExecuteReader(system.data.CommandBehavio r.CloseConnection)
    dlSearch.DataSource = objDR
    dlSearch.DataBind()
    MyConn.Close
dlSearch.visible="true"
End Sub

    Sub doInsert(Source as Object, E as EventArgs)
    Dim MySQL as string = "Insert into mailing (mail) values (@mail)"
    Dim myConn As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; " & _
     "Data Source=" & server.mappath("DB\newBD.mdb") & ";" )

    Dim Cmd as New OleDbCommand(MySQL, MyConn)

    cmd.Parameters.Add(New OleDbParameter("@mail", frmfname.text))

    MyConn.Open()
    cmd.ExecuteNonQuery
    MyConn.Close()
    label1.visible="true"

    label1.text = "E-mail adicionado."
    End Sub

Sub doit(Source as Object, E as EventArgs)
    response.Redirect ("page3.aspx")
End Sub

Sub MyDataList_Select(sender As Object, e As EventArgs)

    Response.Redirect("detail.aspx?id=@id")
End Sub

</script>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>inforJ - produtos informácticos</title>


<script type="text/javascript">
<!--

function newImage(arg) {
    if (document.images) {
        rslt = new Image();
        rslt.src = arg;
        return rslt;
    }
}

function changeImages() {
    if (document.images && (preloadFlag == true)) {
        for (var i=0; i<changeImages.arguments.length; i+=2) {
            document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
        }
    }
}

var preloadFlag = false;
function preloadImages() {
    if (document.images) {
        menu_inicio_over = newImage("images/menu_inicio-over.jpg");
        menu_tinteiros_over = newImage("images/menu_tinteiros-over.jpg");
        menu_hardware_over = newImage("images/menu_hardware-over.jpg");
        menu_software_over = newImage("images/menu_software-over.jpg");
        menu_webdesign_over = newImage("images/menu_webdesign-over.jpg");
        menu_encomendas_over = newImage("images/menu_encomendas-over.jpg");
        menu_contactos_over = newImage("images/menu_contactos-over.jpg");
        menu_campanha_over = newImage("images/menu_campanha-over.jpg");
        preloadFlag = true;
    }
}

// -->
</script>


</head>

<body bgcolor="#FFFFFF" link="#000000" vlink="#000099" alink="#000099" onload="preloadImages();">

<table width="655" height="659" border="1" align="center" id="main">
  <tr>
    <td height="77" colspan="3"> <img src="images/pagina/cab.jpg" width="737" height="142" align="absmiddle"></td>
  </tr>
  <tr>
    <td width="158" valign="top" rowspan="2" align="center">


    <table id="Table_01" width="153" height="290" border="0" cellpadding="0" cellspacing="0">
    <tr>
        <td colspan="3">
            <img src="images/menu_01.jpg" width="153" height="24" alt=""></td>
    </tr>
    <tr>
        <td rowspan="9">
            <img src="images/menu_02.jpg" width="8" height="266" alt=""></td>
        <td>
            <a href="default.aspx"
                onmouseover="changeImages('menu_inicio', 'images/menu_inicio-over.jpg'); return true;"
                onmouseout="changeImages('menu_inicio', 'images/menu_inicio.jpg'); return true;"
                onmousedown="changeImages('menu_inicio', 'images/menu_inicio-over.jpg'); return true;"
                onmouseup="changeImages('menu_inicio', 'images/menu_inicio-over.jpg'); return true;">
                <img name="menu_inicio" src="images/menu_inicio.jpg" width="138" height="30" border="0" alt=""></a></td>
        <td rowspan="9">
            <img src="images/menu_04.jpg" width="7" height="266" alt=""></td>
    </tr>
    <tr>
        <td>
            <a href="tinteiros.aspx"
                onmouseover="changeImages('menu_tinteiros', 'images/menu_tinteiros-over.jpg'); return true;"
                onmouseout="changeImages('menu_tinteiros', 'images/menu_tinteiros.jpg'); return true;"
                onmousedown="changeImages('menu_tinteiros', 'images/menu_tinteiros-over.jpg'); return true;"
                onmouseup="changeImages('menu_tinteiros', 'images/menu_tinteiros-over.jpg'); return true;">
                <img name="menu_tinteiros" src="images/menu_tinteiros.jpg" width="138" height="30" border="0" alt=""></a></td>
    </tr>
    <tr>
        <td>
            <a href="computadores.aspx"
                onmouseover="changeImages('menu_hardware', 'images/menu_hardware-over.jpg'); return true;"
                onmouseout="changeImages('menu_hardware', 'images/menu_hardware.jpg'); return true;"
                onmousedown="changeImages('menu_hardware', 'images/menu_hardware-over.jpg'); return true;"
                onmouseup="changeImages('menu_hardware', 'images/menu_hardware-over.jpg'); return true;">
                <img name="menu_hardware" src="images/menu_hardware.jpg" width="138" height="30" border="0" alt=""></a></td>
    </tr>
    <tr>
        <td>
            <a href="componentes.aspx"
                onmouseover="changeImages('menu_software', 'images/menu_software-over.jpg'); return true;"
                onmouseout="changeImages('menu_software', 'images/menu_software.jpg'); return true;"
                onmousedown="changeImages('menu_software', 'images/menu_software-over.jpg'); return true;"
                onmouseup="changeImages('menu_software', 'images/menu_software-over.jpg'); return true;">
                <img name="menu_software" src="images/menu_software.jpg" width="138" height="30" border="0" alt=""></a></td>
    </tr>
    <tr>
        <td>
            <a href="software.aspx"
                onmouseover="changeImages('menu_webdesign', 'images/menu_webdesign-over.jpg'); return true;"
                onmouseout="changeImages('menu_webdesign', 'images/menu_webdesign.jpg'); return true;"
                onmousedown="changeImages('menu_webdesign', 'images/menu_webdesign-over.jpg'); return true;"
                onmouseup="changeImages('menu_webdesign', 'images/menu_webdesign-over.jpg'); return true;">
                <img name="menu_webdesign" src="images/menu_webdesign.jpg" width="138" height="30" border="0" alt=""></a></td>
    </tr>
    <tr>
        <td>
            <a href="webd.aspx"
                onmouseover="changeImages('menu_encomendas', 'images/menu_encomendas-over.jpg'); return true;"
                onmouseout="changeImages('menu_encomendas', 'images/menu_encomendas.jpg'); return true;"
                onmousedown="changeImages('menu_encomendas', 'images/menu_encomendas-over.jpg'); return true;"
                onmouseup="changeImages('menu_encomendas', 'images/menu_encomendas-over.jpg'); return true;">
                <img name="menu_encomendas" src="images/menu_encomendas.jpg" width="138" height="30" border="0" alt=""></a></td>
    </tr>
    <tr>
        <td>
            <a href="contactos.aspx"
                onmouseover="changeImages('menu_contactos', 'images/menu_contactos-over.jpg'); return true;"
                onmouseout="changeImages('menu_contactos', 'images/menu_contactos.jpg'); return true;"
                onmousedown="changeImages('menu_contactos', 'images/menu_contactos-over.jpg'); return true;"
                onmouseup="changeImages('menu_contactos', 'images/menu_contactos-over.jpg'); return true;">
                <img name="menu_contactos" src="images/menu_contactos.jpg" width="138" height="30" border="0" alt=""></a></td>
    </tr>
    <tr>
        <td>
            <a href="download.aspx"
                onmouseover="changeImages('menu_campanha', 'images/menu_campanha-over.jpg'); return true;"
                onmouseout="changeImages('menu_campanha', 'images/menu_campanha.jpg'); return true;"
                onmousedown="changeImages('menu_campanha', 'images/menu_campanha-over.jpg'); return true;"
                onmouseup="changeImages('menu_campanha', 'images/menu_campanha-over.jpg'); return true;">
                <img name="menu_campanha" src="images/menu_campanha.jpg" width="138" height="36" border="0" alt=""></a></td>
    </tr>
    <tr>
        <td>
            <img src="images/menu_12.jpg" width="138" height="20" alt=""></td>
    </tr>
    </table>

    <br>
    <img src="images/pagina/index2.jpg" width="153" height="73">
    </td>
        <td width="446" rowspan="4" valign="top" align="center">

         <img border="0" src="images/pagina/barraNews.jpg" width="400">


<form runat="server">

<asp:DropDownList id="dlsearchby" runat="server" />
<asp:TextBox id="txtSearch" runat="server" />
<asp:Button id="btnSearch" Text="Search" onclick="doSearch" runat="server" /><br>
<asp:RequiredFieldValidator ID="vldSearch"
    ControlToValidate="txtSearch"
    Display="Dynamic" runat="server">
        You must include search criteria
</asp:RequiredFieldValidator>

<asp:DataList runat="server"
    Id="dlSearch"
    GridLines="None"
    cellpadding="2"
    cellspacing="2"
    Headerstyle-BackColor="#000000"
    HeaderStyle-Forecolor="White"
    Headerstyle-Font-Name="Arial"
    Headerstyle-Font-Size="8"
    OnSelectedIndexChanged="MyDataList_Select"
    Font-Name="Arial"
    Font-Size="10"
    Width="100%">
    <HeaderTemplate>

        <b><i>Resultados de pesquisa </i></b>
        <br>
    </HeaderTemplate>
    <ItemTemplate>
        <b><%# Container.DataItem("produto")%></b><br>
        <a title="Detalhes de <%# DataBinder.Eval(Container.DataItem, "produto") %>" href="detail.aspx?id=<%# DataBinder.Eval(Container.DataItem, "ID") %>"><%# DataBinder.Eval(Container.DataItem, "produto") %></a>

        Categoria: <%# Container.DataItem("categoria")%><br>
        ID: <%# Container.DataItem("codigo")%><br>
    </ItemTemplate>
</ASP:DataList>

  <table border="0" width="100%">
    <tr>
      <td>
          <ASP:DataList id="MyDataList" RepeatColumns="2" RepeatDirection="Horizontal" runat="server">

              <ItemTemplate>

                <table width="100%" border="0" cellpadding=2 cellspacing=4 style="font: 8pt verdana">
                  <tr>
                      <td width="200" valign="top" align="center">
            <b><%# DataBinder.Eval(Container.DataItem, "produto") %></b><br>
                      <IMG alt="foto" align="top" src='<%# DataBinder.Eval(Container.DataItem, "foto") %>' width="100" Border="0"><br>
            <%# DataBinder.Eval(Container.DataItem, "categoria") %><br>
            <a title="Detalhes de <%# DataBinder.Eval(Container.DataItem, "produto") %>" CommandName="Select" href="detailPC.aspx?id=<%# DataBinder.Eval(Container.DataItem, "ID") %>">Detalhes</a><br>
            <b><%# DataBinder.Eval(Container.DataItem, "preco", "{0:c}") %></b><br>
            <a title="Comprar <%# DataBinder.Eval(Container.DataItem, "produto") %>" CommandName="Select" onclick="doit" href="cart/page2.aspx?id=<%# DataBinder.Eval(Container.DataItem, "ID") %>">Comprar</a>
                    </td>

          </tr>
                </table>

              </ItemTemplate>

          </ASP:DataList>
      </td>

    </tr>
  </table>

    </td>
    <td width="153" height="136"><img src="images/pagina/pub02.jpg" width="153" height="132">
    <br><img src="images/pagina/pub01.jpg" width="153" height="207">

      <table border="0">
        <tr>
        <td align="center">Quero receber<br>novidades</td></tr>
        <tr><td align="center"> <asp:textbox columns="10" Text="e-mail" id="frmfname" runat="server" />
        <asp:RequiredFieldValidator
        runat="server"
        id="vldfname"
        ControlToValidate="frmfname"
        ErrorMessage=""
        display="dynamic"
        Font-Names="Arial" Font-Size="8">
        Inválido
        </asp:RequiredFieldValidator>

        <asp:RegularExpressionValidator
        id="emailRegexVal"
        ControlToValidate="frmfname"
        ErrorMessage="Email. "
        Display="dynamic"
        ValidationExpression="^[\w-]+@[\w-]+\.(pt|br|com|net|org|edu|mil)$"
        Font-Names="Arial" Font-Size="8" runat="server">
        Inválido. Deve ser email@host.domínio.
        </asp:RegularExpressionValidator></td>
    </tr>
    <tr>
        <td align="center" colspan="2">
<asp:button id="button1" Text="Inserir" onclick="doInsert" runat="server" />
        </td>
    </tr>
</table>
<div align="center"><b><i><asp:Label ID="label1" runat="server" /></i></b></div>

</Form>
    </td>
  </tr>
  <tr>
    <td height="211"></td>
  </tr>
  <tr>
    <td width="153" rowspan="2">&nbsp;</td>
  </tr>
  <tr>
    <td height="135">&nbsp;</td>
  </tr>
</table>
</body>
</html>


cart.aspx


<%@ Import Namespace="System.Data" %>
<%@ Page Language="VB" Debug="true" %>
<script language="VB" runat="server">
Sub Page_Load(Source as Object, E as EventArgs)
Dim ds as DataSet
    ds = CType(Session("myDataSet"), Dataset)
    MyDataGrid.Datasource=ds.Tables("Catalogo").Defaul tView
    MyDataGrid.DataBind()
End Sub
</script>
<html>
<head>
    <title>Page 2</TITLE>
</head>
<body>
<Form id="form1" runat="server">
<div align="center">
<b><i>Employees Table</i></b>
<asp:Datagrid runat="server"
    Id="MyDataGrid"
    GridLines="Both"
    cellpadding="0"
    cellspacing="0"
    Headerstyle-BackColor="#8080C0"
    Headerstyle-Font-Name="Arial"
    Headerstyle-Font-Bold="True"
    Headerstyle-Font-Size="14"
    BackColor="#8080FF"
    Font-Name="Arial"
    Font-Size="11"
    AlternatingItemStyle-BackColor="#DFDFDF"
    AlternatingItemStyle-Font-Name="Arial"
    AlternatingItemStyle-Font-Size="11"
    BorderColor="Black">
</asp:DataGrid>
</Form>
</body>
</html>


I'm sorry for the size, but I really don't know where the error might be... Please help me, I'm getting desperate

Thanx in advance.


  #2 (permalink)  
Old September 11th, 2006, 09:00 PM
Registered User
 
Join Date: Sep 2006
Location: , , United Kingdom.
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Line 5 should be: Dim ds As New DataSet
You ommitted 'New'!!

Steve

  #3 (permalink)  
Old September 12th, 2006, 08:40 AM
Authorized User
 
Join Date: May 2006
Location: , , .
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you Steve.
That must be one of a few other errors in the code but it didn't stop the error.

Any other suggestions?

  #4 (permalink)  
Old September 12th, 2006, 05:30 PM
Registered User
 
Join Date: Sep 2006
Location: , , United Kingdom.
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The error you mention is related to you using an object that has not been instantiated.

For example, if you 'Dim ds As DataSet' will 'dimension' the ds variable but the object has not been instantiated (ready for use) until you also include the line 'ds = New DataSet'. You can include both these lines in one statement such as 'Dim ds As New DataSet'.

I quickly scanned through your code and noticed a few places where your code needs to be sorted out...

Set breakpoints and step through your code to see where these errors occur and you may be able to fix them yourself.

Alternatively, post the code without HTML, Javascript and designer code. It will be easier to explain line numbers for error modifications...

Steve
  #5 (permalink)  
Old September 13th, 2006, 03:18 AM
Authorized User
 
Join Date: May 2006
Location: , , .
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanx again Steve.

That's exactly what Ive been doing and I came up with an answeer:
I changed my Page_Load to this:

Sub Page_Load(Source as Object, E as EventArgs)
    If Session("myDataSet") Is Nothing Then

        ' Handle session state...

    Exit Sub

    Else

     Dim ds as New DataSet
     ds = CType(Session("myDataSet"), Dataset)

    MyDataList.Datasource=ds.Tables("Catalog").Default View
    MyDataList.DataBind()
End If
End Sub

and a few more things and it's working!

Now I've another question:
How do I manage to get only the item I chose? Because I'm passing the Dataset in the session. Do I have to change that in the itemList.aspx or on page2.aspx?

  #6 (permalink)  
Old September 13th, 2006, 01:14 PM
Registered User
 
Join Date: Sep 2006
Location: , , United Kingdom.
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Item you chose from what?

I don't understand your question...

Steve

  #7 (permalink)  
Old September 21st, 2006, 09:22 AM
Authorized User
 
Join Date: May 2006
Location: , , .
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I only want to put in the cart the item I chose from the list when I'm getting all the items I bound in the items list (when pressing the 'buy' button of any item).



Similar Threads
Thread Thread Starter Forum Replies Last Post
session in shopping cart page Jane Siu Beginning PHP 1 November 22nd, 2006 01:49 AM
Session & secuirty issue of shopping cart apple007 ASP.NET 1.0 and 1.1 Basics 0 July 25th, 2006 01:44 AM
can we manage shopping cart without using session pradeep1976 BOOK: Beginning ASP 3.0 0 April 7th, 2005 05:12 AM
having problem with shopping cart radonfile Classic ASP Databases 0 September 26th, 2003 12:42 PM





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