Results 1 to 11 of 11

Thread: Object reference not set to an instance of an object...

  1. #1

    Thread Starter
    ^:^...ANGEL...^:^ wrack's Avatar
    Join Date
    Mar 2002
    Location
    Melbourne, AUSTRALIA
    Posts
    2,695

    Thumbs down Object reference not set to an instance of an object...

    Hi,

    I have following code to make a shopping cart and when I run it I get an error saying "Object reference not set to an instance of an object" and the error is in this line,

    VB Code:
    1. "Line 51:         For Each objDR In objDT.Rows"
    Its the line in SUB AddToCart...

    VB Code:
    1. Private objDT As System.Data.DataTable
    2.     Private objDR As System.Data.DataRow
    3.  
    4.     Private Sub Page_Load(ByVal s As Object, ByVal e As EventArgs)
    5.         If Not IsPostBack Then
    6.             makeCart()
    7.         End If
    8.     End Sub
    9.  
    10.     Function makeCart()
    11.         objDT = New System.Data.DataTable("Cart")
    12.         objDT.Columns.Add("ID", GetType(Integer))
    13.         objDT.Columns("ID").AutoIncrement = True
    14.         objDT.Columns("ID").AutoIncrementSeed = 1
    15.  
    16.         objDT.Columns.Add("Quantity", GetType(Integer))
    17.         objDT.Columns.Add("Product", GetType(String))
    18.         objDT.Columns.Add("Cost", GetType(Decimal))
    19.  
    20.         Session("Cart") = objDT
    21.     End Function
    22.  
    23.     Sub AddToCart(ByVal s As Object, ByVal e As EventArgs)
    24.         objDT = Session("Cart")
    25.         Dim Product = ddlProducts.SelectedItem.Text
    26.         Dim blnMatch As Boolean = False
    27.  
    28.         For Each objDR In objDT.Rows
    29.             If objDR("Product") = Product Then
    30.                 objDR("Quantity") += txtQuantity.Text
    31.                 blnMatch = True
    32.                 Exit For
    33.             End If
    34.         Next
    35.  
    36.         If Not blnMatch Then
    37.             objDR = objDT.NewRow
    38.             objDR("Quantity") = txtQuantity.Text
    39.             objDR("Product") = ddlProducts.SelectedItem.Text
    40.             objDR("Cost") = Decimal.Parse(ddlProducts.SelectedItem.Value)
    41.             objDT.Rows.Add(objDR)
    42.         End If
    43.         Session("Cart") = objDT
    44.  
    45.         dg.DataSource = objDT
    46.         dg.DataBind()
    47.  
    48.         lblTotal.Text = "$" & GetItemTotal()
    49.     End Sub
    50.  
    51.     Function GetItemTotal() As Decimal
    52.         Dim intCounter As Integer
    53.         Dim decRunningTotal As Decimal
    54.  
    55.         For intCounter = 0 To objDT.Rows.Count - 1
    56.             objDR = objDT.Rows(intCounter)
    57.             decRunningTotal += (objDR("Cost") * objDR("Quantity"))
    58.         Next
    59.  
    60.         Return decRunningTotal
    61.     End Function
    62.  
    63.     Sub Delete_Item(ByVal s As Object, ByVal e As DataGridCommandEventArgs)
    64.         objDT = Session("Cart")
    65.         objDT.Rows(e.Item.ItemIndex).Delete()
    66.         Session("Cart") = objDT
    67.  
    68.         dg.DataSource = objDT
    69.         dg.DataBind()
    70.  
    71.         lblTotal.Text = "$" & GetItemTotal()
    72.     End Sub

    Can anyone tell me how to create an object of this type...???

    Cheers...

  2. #2
    Hyperactive Member
    Join Date
    Aug 2002
    Posts
    416
    Try this instead.....

    Code:
    Private objDT As New System.Data.DataTable
    Private objDR As New System.Data.DataRow
    if u dont use the above, then u will need these in your pageload...

    Code:
    Set objDT = New DataTable()
    Set objDR = New DataRow()

  3. #3

    Thread Starter
    ^:^...ANGEL...^:^ wrack's Avatar
    Join Date
    Mar 2002
    Location
    Melbourne, AUSTRALIA
    Posts
    2,695
    Originally posted by Eras3r
    Try this instead.....

    Code:
    Private objDT As New System.Data.DataTable
    Private objDR As New System.Data.DataRow
    if u dont use the above, then u will need these in your pageload...

    Code:
    Set objDT = New DataTable()
    Set objDR = New DataRow()
    Thanks for the help but I am getting this error...

    Overload resolution falied becasue no "New" is accessible.

    I should have say that I am using VB.NET and framework 1.0.

    Any idea what it can be...???

    Cheers...

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You can't create a datatable in that way, it has required parameters in the constructor. I believe the same for the Datarow.

  5. #5

    Thread Starter
    ^:^...ANGEL...^:^ wrack's Avatar
    Join Date
    Mar 2002
    Location
    Melbourne, AUSTRALIA
    Posts
    2,695
    Originally posted by Edneeis
    You can't create a datatable in that way, it has required parameters in the constructor. I believe the same for the Datarow.
    I am already defining DataTable as follows...

    VB Code:
    1. objDT = New System.Data.DataTable("Cart")

    But Can't find proper way to create object for datarow...

    Cheers...

  6. #6
    Hyperactive Member
    Join Date
    Jan 2003
    Location
    Cape Cod, US
    Posts
    292
    wrack, what you're doing looks legit to me.

    Use the NewRow method to add rows to the table.

    I don't think the error you are getting is related to objDR as the "For Each" should not error even if there are no rows.

    I don't think you are referencing an instance of objDT at the time you get the error. Test it for Nothing to verify this theory.

    How you're storing and retreiving it from a Session variable also looks good, but make sure you have session state persistance turned on...

  7. #7

    Thread Starter
    ^:^...ANGEL...^:^ wrack's Avatar
    Join Date
    Mar 2002
    Location
    Melbourne, AUSTRALIA
    Posts
    2,695
    Originally posted by fungi
    wrack, what you're doing looks legit to me.

    Use the NewRow method to add rows to the table.

    I don't think the error you are getting is related to objDR as the "For Each" should not error even if there are no rows.

    I don't think you are referencing an instance of objDT at the time you get the error. Test it for Nothing to verify this theory.

    How you're storing and retreiving it from a Session variable also looks good, but make sure you have session state persistance turned on...
    Can you please tell me how exactly you are intended to check this...I am confused...

    Cheers..

  8. #8
    Hyperactive Member
    Join Date
    Jan 2003
    Location
    Cape Cod, US
    Posts
    292
    Here's a quick prog I whipped up that uses most of your code. Let me know how it runs..

    CODE BEHIND

    Code:
    Public Class WebForm1
        Inherits System.Web.UI.Page
        Protected WithEvents txtQty As System.Web.UI.WebControls.TextBox
        Protected WithEvents txtProduct As System.Web.UI.WebControls.TextBox
        Protected WithEvents txtCost As System.Web.UI.WebControls.TextBox
        Protected WithEvents cmdAdd As System.Web.UI.WebControls.Button
        Private objDR As System.Data.DataRow
        Private objDT As System.Data.DataTable
    
    
    #Region " Web Form Designer Generated Code "
    
        'This call is required by the Web Form Designer.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    
        End Sub
    
        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
    
            If Not IsPostBack Then
                makeCart()
            Else
                objDT = Session("Cart")
            End If
    
        End Sub
    
        Function showcart()
    
            If objDT Is Nothing Then
                Response.Write("objDT is Nothing")
                Exit Function
            End If
    
            Dim szLine As String
            Response.Write(objDT.Rows.Count.ToString & " rows<BR>")
            For Each objDR In objDT.Rows
                szLine = "[Id] " & objDR("id") & "  "
                szLine += "[Qty] " & objDR("quantity") & "  "
                szLine += "[Prod] " & objDR("product") & "  "
                szLine += "[Qty] " & objDR("cost") & "<BR>"
                Response.Write(szLine)
            Next
    
        End Function
    
        Function makeCart()
            objDT = New System.Data.DataTable("Cart")
            objDT.Columns.Add("ID", GetType(Integer))
            objDT.Columns("ID").AutoIncrement = True
            objDT.Columns("ID").AutoIncrementSeed = 1
    
            objDT.Columns.Add("Quantity", GetType(Integer))
            objDT.Columns.Add("Product", GetType(String))
            objDT.Columns.Add("Cost", GetType(Decimal))
    
            Session("Cart") = objDT
        End Function
    
        Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click
    
            objDR = objDT.NewRow()
            objDR("Quantity") = CInt(txtQty.Text)
            objDR("Product") = txtProduct.Text
            objDR("Cost") = CDec(txtCost.Text)
    
            objDT.Rows.Add(objDR)
    
            Session("Cart") = objDT
    
            showcart()
    
    
        End Sub
    
    End Class
    ASPX
    Code:
    <%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="test4.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>
    		<form id="Form1" method="post" runat="server">
    			<P>
    				<asp:TextBox id="txtQty" runat="server" Width="65px">1</asp:TextBox>&nbsp;&nbsp;
    				<asp:TextBox id="txtProduct" runat="server" Width="68px">Prod1</asp:TextBox>&nbsp;&nbsp;
    				<asp:TextBox id="txtCost" runat="server" Width="69px">1.00</asp:TextBox>&nbsp;&nbsp;
    				<asp:Button id="cmdAdd" runat="server" Text="Add"></asp:Button></P>
    			<P>&nbsp;</P>
    		</form>
    	</body>
    </HTML>

  9. #9

    Thread Starter
    ^:^...ANGEL...^:^ wrack's Avatar
    Join Date
    Mar 2002
    Location
    Melbourne, AUSTRALIA
    Posts
    2,695
    I am getting this error...
    Attached Files Attached Files

  10. #10
    Hyperactive Member
    Join Date
    Jan 2003
    Location
    Cape Cod, US
    Posts
    292
    I'm afraid I haven't got a clue what your error is

    Are you having success running other ASP.NET apps?

  11. #11

    Thread Starter
    ^:^...ANGEL...^:^ wrack's Avatar
    Join Date
    Mar 2002
    Location
    Melbourne, AUSTRALIA
    Posts
    2,695
    Originally posted by fungi
    I'm afraid I haven't got a clue what your error is

    Are you having success running other ASP.NET apps?
    I figured out that I can't run any ASP.NET app...As a matter of fact I can't even uninstall VS.NET or install it again...

    Give me sometime and I will check it out...

    Cheers...

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