DaDMan
asked on
Object reference not set to an instance of an object. error msg
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.NullReferenceExcept ion: Object reference not set to an instance of an object.
Source Error: dtGridPersDir.DataSource=d s.Tables(" MyTable"). DefaultVie w
And the code
<%@ Page Language="VB" Debug="true"%>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.Sql Client" %>
<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=(loc al);databa se=testing ;Trusted_C onnection= yes")
MyCommand = New SqlDataAdapter("select * from MyTable", MyConnection)
DS = new DataSet()
MyCommand.Fill(ds, "MyTable")
dtGridPersDir.DataSource=d s.Tables(" MyTable"). DefaultVie w
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:Edit CommandCol umn>
<asp:HyperLinkColumn Text="Set Inactive" HeaderText="Set Inactive"></asp:HyperLinkC olumn>
</Columns>
</asp:DataGrid>
<!-- Insert content here -->
</form>
</body>
</html>
Thanks
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.NullReferenceExcept
Source Error: dtGridPersDir.DataSource=d
And the code
<%@ Page Language="VB" Debug="true"%>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.Sql
<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=(loc
MyCommand = New SqlDataAdapter("select * from MyTable", MyConnection)
DS = new DataSet()
MyCommand.Fill(ds, "MyTable")
dtGridPersDir.DataSource=d
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:Edit
<asp:HyperLinkColumn Text="Set Inactive" HeaderText="Set Inactive"></asp:HyperLinkC
</Columns>
</asp:DataGrid>
<!-- Insert content here -->
</form>
</body>
</html>
Thanks
This error shows that data is not returned from the database.
ASKER
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
Am stupid..
MyCommand.Fill(ds,"MyTable
Well I'll award the points or I ask for a refund. What do you think guys?
Thanks
lol.. that was funny... must be working too much.
Go here https://www.experts-exchange.com/Community_Support/
and ask for refund. Good luck :)
-Baan
Go here https://www.experts-exchange.com/Community_Support/
and ask for refund. Good luck :)
-Baan
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
DS = new DataSet()
MyCommand.Fill(ds, "MyTable")
If DS.Tables("MyTable").Rows.
dtGridPersDir.DataSource=D
dtGridPersDir.DataBind()
End If
-Baan