?
Solved

Object variable or with block not set error asp.net

Posted on 2003-12-01
6
Medium Priority
?
388 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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:Ralf Klatt
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
Use Filtering Commands to Process Files in Linux

Learn how to manipulate data with the help of various filtering commands such as `cat`, `fmt`, `pr`, and others in Linux.

 
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 200 total points
ID: 10279979
Agreed
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

752 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