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.Sql Client" %>
<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=fr oggie;Pers ist 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.StoredProcedur e
' 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(m yParameter )
' 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(Co mmandBehav ior.CloseC onnection)
' 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 **********************Erro r 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>
<<%@ Page Language="VB" Debug =true%>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.Sql
<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=fr
' 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.StoredProcedur
' 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(m
' 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(Co
' 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 **********************Erro
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>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
change
Dim SimpleDataGrid As DataGrid
to
Dim SimpleDataGrid As new DataGrid
if myDataReader.HasRows Then
SimpleDataGrid.DataSource = myDataReader
SimpleDataGrid.DataBind()
End if
--Nauman.