Link to home
Start Free TrialLog in
Avatar of shelbyinfotech
shelbyinfotech

asked on

Object reference not set to an instance of an object. Error Line Highlighted

To those of you who have been around awhile....tell me where I screwed up. I did a mashup of code samples to get me this far I am passing a parameter to a stored procedure, attempting to place the returned results in a datareader and display the datareader in a datagrid.

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

      Sub Page_Load(sender as Object, e as EventArgs)
            Dim myConnection As SqlConnection
            Dim myCommand    As SqlCommand
            Dim myParameter  As SqlParameter
            Dim myDataReader As SqlDataReader
        Dim SimpleDataGrid As DataGrid
       
            ' Create connection and set connection string      
        myConnection = New SqlConnection("Password=froggie;Persist Security Info=True;User ID=kermit;Initial Catalog=Sesame;Data Source=BIRD")

            ' Create a new command object
            myCommand = New SqlCommand()

            ' Set the properties of the command so that it uses
            ' our connection, knows the name of the stored proc
            ' to execute, and knows that it is going to be
            ' executing a stored proc and not something else
            myCommand.Connection = myConnection
        myCommand.CommandText = "PPSUB_RTGROUP1"
            myCommand.CommandType = CommandType.StoredProcedure

            ' Create our input parameter @Id, tell it it's
            ' and integer and set it's value
            myParameter = myCommand.CreateParameter()
            myParameter.ParameterName = "@Id"
            myParameter.Direction = ParameterDirection.Input
        myParameter.SqlDbType = SqlDbType.VarChar
        myParameter.Value = "P003100"

            ' Add parameter to our command
            myCommand.Parameters.Add(myParameter)

            ' Execute our command again, but this time we have
            ' it return a datareader and pull the data we want out
            ' of that.  Could just as easily databind or whatever
        ' you need to do with your data.
        myConnection.Open()
        myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
       
        ' Specify the DataReader as the source of the data for our DataGrid
        ' and then DataBind to display the data in our DataGrid.
        SimpleDataGrid.DataSource = myDataReader   **********************Error Occurs Here *****************************
        SimpleDataGrid.DataBind()

        ' Close our reader
            myDataReader.Close

            ' Close our connection
            myConnection.Close()
      End Sub
</script>

<html>
<head>
<title>ASP.NET  Database Sample</title>
</head>
<body>

<form id="Form1" runat="server">

      <asp:DataGrid id="SimpleDataGrid" runat="server" />

</form>


</body>
</html>
Avatar of nauman_ahmed
nauman_ahmed
Flag of United States of America image

Try

if myDataReader.HasRows Then
 SimpleDataGrid.DataSource = myDataReader  
 SimpleDataGrid.DataBind()
End if

--Nauman.
ASKER CERTIFIED SOLUTION
Avatar of nauman_ahmed
nauman_ahmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lijunguo

change
 Dim SimpleDataGrid As DataGrid
to
Dim SimpleDataGrid As new DataGrid