Solved

Object variable or with block not set error asp.net

Posted on 2003-12-01
6
385 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: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
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

717 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