I am trying to execute a stored procedure and place the data in a datagrid manually (without using the .net wizard). I am using vb.net and asp.net in visual studio.
Here is my code but I can’t figure out where I’m going wrong.
Imports System.Data.Sql,
Imports System.Data.SqlClient
Imports System.Diagnostics
Imports System
Imports System.Configuration
Imports System.Data
Imports System.Collections.Generic
Partial Public Class WebForm3
Inherits System.Web.UI.Page
Dim myConn As New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("ConnectionString"))
Public Cmd As New SqlCommand
Private _storedProc As String
Dim Conn As New SqlConnection(ConfigurationManager.AppSettings("ConnectionString").ToString)
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim dt As SqlDataReader
Cmd.CommandType = CommandType.StoredProcedure
Cmd.CommandText = "proc_getTEST"
Cmd.Connection = Conn
Conn.Open()
Try
dt = Cmd.ExecuteReader
GridView1.DataSource = dt
GridView1.DataBind()
Conn.Dispose()
Catch ex As Exception
End Try
End If
End Sub
End Class
Any tips?
Instead of a DataReader, you can use a SqlDataAdapter where you can set the SqlConnection and SqlCommand to execute then call the Fill method of the SqlDataAdapter passing the DataTable that want to fill.
Since your DataReader returns 2 result sets, you have to use a DataSet for this. You will still use a SqlDataAdapter for this purpose but instead of passing a DataTable in the Fill method, you pass a DataSet and it will create 2 DataTables inside that DataSet.