Whats the error:- System.NullReferenceException:

Discussion in 'ASP .Net' started by =?Utf-8?B?UGF0cmljay5PLklnZQ==?=, Oct 31, 2004.

  1. What could cause the error:-
    System.NullReferenceException: Object reference not set to an instance of an
    object.
    Any ideas?
    =?Utf-8?B?UGF0cmljay5PLklnZQ==?=, Oct 31, 2004
    #1
    1. Advertising

  2. =?Utf-8?B?UGF0cmljay5PLklnZQ==?=

    Chris Austin Guest

    Your code is attempting reference an object that is currently equal to Null
    (or, Nothing if you are using VB).

    -Chris
    ~
    http://weblogs.austinspad.com/caustin

    "Patrick.O.Ige" <> wrote in message
    news:...
    > What could cause the error:-
    > System.NullReferenceException: Object reference not set to an instance of
    > an
    > object.
    > Any ideas?
    >
    >
    Chris Austin, Nov 1, 2004
    #2
    1. Advertising

  3. =?Utf-8?B?UGF0cmljay5PLklnZQ==?=

    Karl Seguin Guest

    If you showed us some code, we'd probably be more likely to point out the
    exact error..especially if you could provide a line #

    Karl

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/


    "Patrick.O.Ige" <> wrote in message
    news:...
    > What could cause the error:-
    > System.NullReferenceException: Object reference not set to an instance of

    an
    > object.
    > Any ideas?
    >
    >
    Karl Seguin, Nov 1, 2004
    #3
  4. Well what i have is a Datagrid that ,Updates,Adds and Delete.
    When i add i have no error but when i click Update and Delete commands it
    fires an error..
    The Error line is at :-

    "ItemNumber = CType(Args.Item.FindControl("ItemNumber"), TextBox).Text"

    Any ideas?

    My code is below(its my EditRecord Sub porgram):-
    Sub EditRecord(ByVal Src As Object, ByVal Args As DataGridCommandEventArgs)


    Dim Command As String
    Dim ItemNumber As String
    Dim ItemType As String
    Dim ItemSupplier As String
    Dim ItemName As String
    Dim ItemDescription As String
    Dim ItemPrice As String
    Dim ItemQuantity As String
    Dim DBConnection As SqlConnection
    Dim DBCommand As SqlCommand
    Dim DBReader As SqlDataReader
    Dim SQLString As String


    Command = Args.CommandSource.Text

    ItemNumber = CType(Args.Item.FindControl("ItemNumber"), TextBox).Text
    ItemType = CType(Args.Item.FindControl("ItemType"),
    DropDownList).SelectedItem.Text
    ItemSupplier = CType(Args.Item.FindControl("ItemSupplier"),
    TextBox).Text
    ItemName = CType(Args.Item.FindControl("ItemName"), TextBox).Text
    ItemDescription = CType(Args.Item.FindControl("ItemDescription"),
    TextBox).Text
    ItemPrice = CType(Args.Item.FindControl("ItemPrice"), TextBox).Text
    ItemQuantity = CType(Args.Item.FindControl("ItemQuantity"),
    TextBox).Text

    UpdateMessage.Text = " "

    Dim ValidRecord As Boolean = True

    If Command = "Add" Or Command = "Update" Then

    '-- CHECK FOR VALID RECORD ---

    '-- Check for valid ItemNumber
    If Len(ItemNumber) <> 6 Then
    UpdateMessage.Text &= "- Invalid Item Number length "
    ValidRecord = False
    ElseIf Not IsNumeric(Right(ItemNumber, 4)) Then
    UpdateMessage.Text &= "- Invalid Item Number format "
    ValidRecord = False
    Else
    ItemNumber = UCase(ItemNumber)
    End If

    '-- Check for missing Item Supplier
    If ItemSupplier = "" Then
    UpdateMessage.Text &= "- Missing Supplier "
    ValidRecord = False
    End If

    '-- Check for missing Item Name
    If ItemName = "" Then
    UpdateMessage.Text &= "- Missing Name "
    ValidRecord = False
    End If

    '-- Check for missing Item Description
    If ItemDescription = "" Then
    UpdateMessage.Text &= "- Missing Description "
    ValidRecord = False
    End If

    '-- Check for valid Item Price
    If Not IsNumeric(ItemPrice) Then
    UpdateMessage.Text &= "- Invalid Price format "
    ValidRecord = False
    End If

    '-- Check for valid Item Quantity
    If Not IsNumeric(ItemQuantity) Then
    UpdateMessage.Text &= "- Invalid Quantity format "
    ValidRecord = False
    End If

    If ValidRecord = True And Command = "Add" Then

    '--- CHECK FOR DUPLICATE RECORD ---
    DBConnection = New
    SqlConnection("server=(local);database=Northwind;integrated security=true;")
    DBConnection.Open()
    SQLString = "SELECT Count(*) FROM products1 WHERE ItemNumber
    = '" & ItemNumber & "'"
    DBCommand = New SqlCommand(SQLString, DBConnection)
    If DBCommand.ExecuteScalar() <> 0 Then
    UpdateMessage.Text = "- Duplicate Item Number. Record
    not added."
    ValidRecord = False
    End If
    DBConnection.Close()

    End If

    End If

    If ValidRecord = True Then

    Select Case Command

    Case "Add"
    SQLString = "INSERT INTO products1 " & _
    "(ItemNumber, ItemType, ItemSupplier, ItemName, " & _
    "ItemDescription, ItemPrice, ItemQuantity) VALUES(" & _
    "'" & ItemNumber & "', " & _
    "'" & ItemType & "', " & _
    "'" & Replace(ItemSupplier, "'", "''") & "', " & _
    "'" & Replace(ItemName, "'", "''") & "', " & _
    "'" & Replace(ItemDescription, "'", "''") & "', " & _
    ItemPrice & ", " & _
    ItemQuantity & ")"
    UpdateMessage.Text = "- Record " & ItemNumber & " added"

    Case "Update"
    SQLString = "UPDATE products1 SET " & _
    "ItemType = '" & ItemType & "', " & _
    "ItemSupplier = '" & Replace(ItemSupplier, "'", "''")
    & "', " & _
    "ItemName = '" & Replace(ItemName, "'", "''") & "', "
    & _
    "ItemDescription = '" & Replace(ItemDescription, "'",
    "''") & "', " & _
    "ItemPrice = " & ItemPrice & ", " & _
    "ItemQuantity = " & ItemQuantity & " " & _
    "WHERE ItemNumber = '" & ItemNumber & "'"
    UpdateMessage.Text = "- Record " & ItemNumber & " updated"

    Case "Delete"
    SQLString = "DELETE FROM products1 WHERE ItemNumber = '"
    & ItemNumber & "'"
    UpdateMessage.Text = "- Record " & ItemNumber & " deleted"

    End Select

    DBConnection = New
    SqlConnection("server=(local);database=Northwind;integrated security=true;")
    DBConnection.Open()
    DBCommand = New SqlCommand(SQLString, DBConnection)
    DBCommand.ExecuteNonQuery()
    DBConnection.Close()

    If Command = "Add" Or Command = "Delete" Then

    ViewState("Updated") = True
    Page_Load(Me, EventArgs.Empty)
    'Page_Load(ByRef sender As System.Object, ByRef e As
    System.EventArgs)
    End If

    End If

    End Sub


    "Karl Seguin" wrote:

    > If you showed us some code, we'd probably be more likely to point out the
    > exact error..especially if you could provide a line #
    >
    > Karl
    >
    > --
    > MY ASP.Net tutorials
    > http://www.openmymind.net/
    >
    >
    > "Patrick.O.Ige" <> wrote in message
    > news:...
    > > What could cause the error:-
    > > System.NullReferenceException: Object reference not set to an instance of

    > an
    > > object.
    > > Any ideas?
    > >
    > >

    >
    >
    >
    =?Utf-8?B?UGF0cmljay5PLklnZQ==?=, Nov 1, 2004
    #4
  5. =?Utf-8?B?UGF0cmljay5PLklnZQ==?=

    Jarmo Muukka Guest

    Hello,

    You het the error because Args.Item.FindControl("ItemNumber") does not find
    the control and returns Nothing and/or the found control is not TextBox. You
    cast the control to TextBox and if it is not a TextBox, it returns Nothing.
    I assume that VB .NET works similarry than "control as TextBox" which
    returns null, if the control is null or is not a TextBox. Then you try to
    use its property.

    It's basically the same if you do this:

    Control control = null;
    TextBox textBox = control as TextBox;
    string text = textBox.Text; // exception happens in here

    or this:

    Label label = new Label();
    TextBox textBox = label as TextBox;
    string text = textBox.Text; // exception happens in here

    JMu

    "Patrick.O.Ige" <> wrote in message
    news:...
    > Well what i have is a Datagrid that ,Updates,Adds and Delete.
    > When i add i have no error but when i click Update and Delete commands it
    > fires an error..
    > The Error line is at :-
    >
    > "ItemNumber = CType(Args.Item.FindControl("ItemNumber"), TextBox).Text"
    >
    > Any ideas?
    Jarmo Muukka, Nov 1, 2004
    #5
  6. Thx Jarmo the reply.
    But my html looks like the following below and ItemNumber is TEXTBOX!
    I don't know why its returning Null then!
    Its works with ASP.NET WebMatrix but gives the error with VStudio .Net!!!

    <asp:DataGrid id="DataGridDisplay" Width="560px" runat="server"
    OnItemCommand="EditRecord"
    AutoGenerateColumns="False"
    BackColor="#F9F9F9"
    HeaderStyle-BackColor="#990000"
    HeaderStyle-ForeColor="#FFFFFF"
    HeaderStyle-Font-Bold="True"
    HeaderStyle-HorizontalAlign="Center"
    HeaderStyle-VerticalAlign="Top"
    ItemStyle-VerticalAlign="Top"
    ItemStyle-HorizontalAlign="Center">

    <Columns>

    <asp:TemplateColumn>
    <HeaderTemplate>
    No<br>
    <asp:TextBox id="ItemNumber" runat="server"/>
    </HeaderTemplate>
    <ItemTemplate>
    <asp:TextBox id="ItemNumber" runat="server"
    Text='<%# Container.DataItem("ItemNumber") %>'
    ReadOnly="True"/>
    </ItemTemplate>
    </asp:TemplateColumn>

    <asp:TemplateColumn>
    <HeaderTemplate>
    Type<br>
    <asp:DropDownList id="ItemType" runat="server"
    DataSource='<%# ItemTypes %>'/>
    </HeaderTemplate>
    <ItemTemplate>
    <asp:DropDownList id="ItemType" runat="server"
    DataSource='<%# ItemTypes %>'
    SelectedIndex='<%# SetIndex(Container.DataItem("ItemType")) %>'/>
    </ItemTemplate>
    </asp:TemplateColumn>

    <asp:TemplateColumn>
    <HeaderTemplate>
    Supplier<br>
    <asp:TextBox id="ItemSupplier" runat="server"/>
    </HeaderTemplate>
    <ItemTemplate>
    <asp:TextBox id="ItemSupplier" runat="server"
    Text='<%# Container.DataItem("ItemSupplier") %>'/>
    </ItemTemplate>
    </asp:TemplateColumn>

    <asp:TemplateColumn>
    <HeaderTemplate>
    Name<br>
    <asp:TextBox id="ItemName" runat="server"/>
    </HeaderTemplate>
    <ItemTemplate>
    <asp:TextBox id="ItemName" runat="server"
    Text='<%# Container.DataItem("ItemName") %>'/>
    </ItemTemplate>
    </asp:TemplateColumn>

    <asp:TemplateColumn>
    <HeaderTemplate>
    Description<br>
    <asp:TextBox id="ItemDescription" runat="server"
    TextMode="MultiLine"
    Cols="25"
    Rows="2"/>
    </HeaderTemplate>
    <ItemTemplate>
    <asp:TextBox id="ItemDescription" runat="server"
    Text='<%# Container.DataItem("ItemDescription") %>'
    TextMode="MultiLine"
    Cols="25"
    Rows="2"/>
    </ItemTemplate>
    </asp:TemplateColumn>

    <asp:TemplateColumn>
    <HeaderTemplate>
    Price<br>
    <asp:TextBox id="ItemPrice" runat="server"
    Text="0.00"/>
    </HeaderTemplate>
    <ItemTemplate>
    <asp:TextBox id="ItemPrice" runat="server"
    Text='<%# FormatNumber(Container.DataItem("ItemPrice"),2) %>'/>
    </ItemTemplate>
    </asp:TemplateColumn>

    <asp:TemplateColumn>
    <HeaderTemplate>
    Qty<br>
    <asp:TextBox id="ItemQuantity" runat="server"
    Text="0"/>
    </HeaderTemplate>
    <ItemTemplate>
    <asp:TextBox id="ItemQuantity" runat="server"
    Text='<%# Container.DataItem("ItemQuantity") %>'/>
    </ItemTemplate>
    </asp:TemplateColumn>

    <asp:TemplateColumn>
    <HeaderTemplate>
    <br>
    <asp:Button Text="Add" runat="server"/>
    </HeaderTemplate>
    <ItemTemplate>
    <asp:Button Text="Update" runat="server"/>
    </ItemTemplate>
    </asp:TemplateColumn>

    <asp:TemplateColumn>
    <HeaderTemplate>
    <br>
    </HeaderTemplate>
    <ItemTemplate>
    <asp:Button Text="Delete" runat="server"/>
    </ItemTemplate>
    </asp:TemplateColumn>

    </Columns>

    </asp:DataGrid>


    "Jarmo Muukka" wrote:

    > Hello,
    >
    > You het the error because Args.Item.FindControl("ItemNumber") does not find
    > the control and returns Nothing and/or the found control is not TextBox. You
    > cast the control to TextBox and if it is not a TextBox, it returns Nothing.
    > I assume that VB .NET works similarry than "control as TextBox" which
    > returns null, if the control is null or is not a TextBox. Then you try to
    > use its property.
    >
    > It's basically the same if you do this:
    >
    > Control control = null;
    > TextBox textBox = control as TextBox;
    > string text = textBox.Text; // exception happens in here
    >
    > or this:
    >
    > Label label = new Label();
    > TextBox textBox = label as TextBox;
    > string text = textBox.Text; // exception happens in here
    >
    > JMu
    >
    > "Patrick.O.Ige" <> wrote in message
    > news:...
    > > Well what i have is a Datagrid that ,Updates,Adds and Delete.
    > > When i add i have no error but when i click Update and Delete commands it
    > > fires an error..
    > > The Error line is at :-
    > >
    > > "ItemNumber = CType(Args.Item.FindControl("ItemNumber"), TextBox).Text"
    > >
    > > Any ideas?

    >
    >
    >
    =?Utf-8?B?UGF0cmljay5PLklnZQ==?=, Nov 1, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. William
    Replies:
    0
    Views:
    1,598
    William
    Aug 10, 2004
  2. msnews.microsoft.com

    Help with System.NullReferenceException

    msnews.microsoft.com, Jul 16, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    571
    msnews.microsoft.com
    Jul 16, 2003
  3. Manny
    Replies:
    1
    Views:
    439
    Lau Lei Cheong
    Aug 2, 2004
  4. EmmaKate
    Replies:
    0
    Views:
    603
    EmmaKate
    Jan 8, 2008
  5. Ruchi22
    Replies:
    0
    Views:
    916
    Ruchi22
    Sep 23, 2011
Loading...

Share This Page