Solved

Object variable or with block not set error asp.net

Posted on 2003-12-01
6
382 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

912 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

22 Experts available now in Live!

Get 1:1 Help Now