Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Object variable or with block not set error asp.net

Posted on 2003-12-01
6
Medium Priority
?
390 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Loops Section Overview

610 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