Solved

Object reference not set to an instance of an object. error msg

Posted on 2004-09-13
6
229 Views
Last Modified: 2012-05-05
I all,
  Am new to the dot net world and unfortunatly facing what is probably the most discouraging step when learning a new environment, this is those weirdos error message that could be written in chiness ( well for my case ) and would be meaning the same thing... so.

Am simply trying to populate a datagrid from data in a sql table. Currently the datagrid is define to be update/edit/add also.
Am using web matrix. There is yet no data in the table, could it be this the problem? I highly doubt.
Any help would be greatly appreciated.

here's what is under the hood.


Error message I received? yeah sure..
Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error: dtGridPersDir.DataSource=ds.Tables("MyTable").DefaultView

And the code

<%@ Page Language="VB" Debug="true"%>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<script runat="server">

    Sub Page_Load(Sender As Object, E As EventArgs)

         Dim DS As DataSet
         Dim MyConnection As SqlConnection
         Dim MyCommand As SqlDataAdapter

         MyConnection = New SqlConnection("server=(local);database=testing;Trusted_Connection=yes")
         MyCommand = New SqlDataAdapter("select * from MyTable", MyConnection)

         DS = new DataSet()
         MyCommand.Fill(ds, "MyTable")
         dtGridPersDir.DataSource=ds.Tables("MyTable").DefaultView
         dtGridPersDir.DataBind()
     End Sub

</script>

<html>
<head>
</head>
<body>
    <form runat="server">
        <asp:DataGrid id="dtGridPersDir" runat="server">
            <Columns>
                <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
                <asp:HyperLinkColumn Text="Set Inactive" HeaderText="Set Inactive"></asp:HyperLinkColumn>
            </Columns>
        </asp:DataGrid>
        <!-- Insert content here -->
    </form>
</body>
</html>

Thanks
0
Comment
Question by:DaDMan
6 Comments
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 12049687
Your code looks absolutely alright to me. Anyway try this, and see what you get...

         DS = new DataSet()
         MyCommand.Fill(ds, "MyTable")
         If DS.Tables("MyTable").Rows.Count <> 0 Then
              dtGridPersDir.DataSource=DS.Tables("MyTable").DefaultView
              dtGridPersDir.DataBind()
         End If

-Baan
0
 
LVL 10

Expert Comment

by:123654789987
ID: 12051306
This error shows that data is not returned from the database.
0
 
LVL 1

Author Comment

by:DaDMan
ID: 12070535
Thanks for your thoughts guy's but this was my own mistake...
Am stupid..

MyCommand.Fill(ds,"MyTable") was not openening MyTable... was Opening "Authors" table!!!! Did a copy paste from an exemple and missed to change this name...

Well I'll award the points or I ask for a refund. What do you think guys?

Thanks


0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 12070573
lol.. that was funny... must be working too much.
Go here    http://www.experts-exchange.com/Community_Support/
and ask for refund.  Good luck :)
-Baan
0
 

Accepted Solution

by:
ee_ai_construct earned 0 total points
ID: 12173129
Closed, 150 points refunded.
ee_ai_construct (replacement part #xm34)
Community Support Admin
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Need help with a query 6 55
visual Basic Net vs c# net 8 56
Hide Tab Page 3 20
Recommendation vb6 to vb.net or others 14 41
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

762 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

18 Experts available now in Live!

Get 1:1 Help Now