Solved

Object variable or with block not set error asp.net

Posted on 2003-12-01
6
381 Views
Last Modified: 2007-12-19
I have gone crazy trying to solve this problem . I am displaying certain records with datagrid heres the code where i bind the datagrid
sub datagridbind()
--open the connections
-- get the sql query
dim ds as new dataset("mydbname")
dim dsc as new sqldataadapter(sql,myconnection)
dsc.fill(ds)
Details.DataSource=ds  <-- details is my datagrid id
Details.databind()
-- close connection
end sub

Heres the datagrid line

<asp:DataGrid runat="Server" AllowPaging="false" AutoGenerateColumns="False" HorizontalAlign="Center" id="Details"  OnItemDataBound="getresultdetail">

I am calling the onitemdatabound method to display the orderid

sub getresultdetail(sender as object, e as DataGridItemEventArgs)

lbl_msg.text=e.item.dataitem("orderid")  <-- error on this line
end sub

Exception Details: System.NullReferenceException: Object variable or With block variable not set.

brij
0
Comment
Question by:mr_brij
6 Comments
 
LVL 12

Expert Comment

by:roverm
ID: 9848878
This can be caused by both the label and the datagrid.

First try this to make sure it's not the label:
lbl_msg.Text = "testing"

If that works then you'll know that it's the datagrid.

Then set a breakpoint at that line. Once it reaches that line, goto the local variables window and view e.
Let me know what is says.

D'Mzz!
RoverM
0
 
LVL 15

Expert Comment

by:Raisor
ID: 9849493
Hi,

Have you ever thought of doing this way:

<asp:DataGrid id="MyList" AutoGenerateColumns="false" runat="server">
      <Columns>
            <asp:BoundColumn HeaderText="Message" DataField="OrderID" />
                                 <!-- ... and all your other fields if any ... -->
                </Columns>
</asp:DataGrid>

And:

sub datagridbind()
    Dim orderHistory As OrdersDB = New OrdersDB()
    MyList.DataSource = orderHistory.GetYourOrders()
    MyList.DataBind()
    OrdersDB = nothing
end sub


Public Class OrdersDB

Public Function GetYourOrders() As SqlDataReader
    Dim myConnection As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"))
    Dim myCommand As SqlCommand = New SqlCommand("OrdersList", myConnection)
    myCommand.CommandType = CommandType.StoredProcedure
    myConnection.Open()
    Dim result As SqlDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
    Return result
End Function


Best regards, Raisor
0
 
LVL 1

Author Comment

by:mr_brij
ID: 9849540
Thanks guys for the ideas. But i figured out what happened after adding this condition it started working.. i dont know the reason but it is working now.

If e.Item.ItemType = ListItemType.Item OR  e.Item.ItemType = ListItemType.AlternatingItem then
           lbl_msg.text=e.item.dataitem("orderid")
end if
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 12

Expert Comment

by:roverm
ID: 9849606
Hi mr_brij,

Glad to see that you solved your question.
Can you please close it by:
- Choosing an answer from an expert or,
- Request a PAQ/Refund in CS.

Thanks!

D'Mzz!
RoverM
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 10279743
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

PAQ with points refunded

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

TheLearnedOne
EE Cleanup Volunteer
0
 
LVL 12

Accepted Solution

by:
roverm earned 50 total points
ID: 10279979
Agreed
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now