Stored Procedures Customization

Populate GridView with Your Own Database Stored Procedures

Description
This customization shows how to change the appearance of cursor when moving over cells in the DataGrid.
Variables
Applies to
Page class
Code
 
''' 
''' Calls custom stored procedure.
''' 
Private Function CallCustomStoredProcedureForGridView() As System.Data.DataSet
	'' Step 1: Configure parameters for your stored procedure if the stored procedure accepts parameters.
	'' Each database type has different parameter type.
	'' Please change them accordingly to fit your application's logic.
	'' SQL Server: 
	''	Parameter type: System.Data.SqlDbType
	'' Oracle:
	''	Parameter type: System.Data.OracleClient.OracleType
	'' MySql:
	'' 	Parameter type: MySql.Data.MySqlClient.MySqlDbType
	
	'Dim firstParameter As BaseClasses.Data.StoredProcedureParameter = Nothing
	
	'' For SQL Server: use parameter type System.Data.SqlDbType
	'firstParameter = New BaseClasses.Data.StoredProcedureParameter("@pk_EmployeeID", 5, System.Data.SqlDbType.Int, System.Data.ParameterDirection.Input)

	'' For Oracle: use parameter type System.Data.OracleClient.OracleType	
	'firstParameter = New BaseClasses.Data.StoredProcedureParameter("@pk_EmployeeID", 5, System.Data.OracleClient.OracleType.Int, System.Data.ParameterDirection.Input)

	'' For MySql: use parameter type MySql.Data.MySqlClient.MySqlDbType
	'firstParameter = New BaseClasses.Data.StoredProcedureParameter("@pk_EmployeeID", 5, MySql.Data.MySqlClient.MySqlDbType.Int, System.Data.ParameterDirection.Input)


	'Dim secondParameter As BaseClasses.Data.StoredProcedureParameter = Nothing
	
	'' For SQL Server: use parameter type System.Data.SqlDbType
	'secondParameter = New BaseClasses.Data.StoredProcedureParameter("@pk_EmployeeLastName", "Smith", System.Data.SqlDbType.VarChar, System.Data.ParameterDirection.Input)    
	
	'' For Oracle: use parameter type System.Data.OracleClient.OracleType
	'secondParameter = New BaseClasses.Data.StoredProcedureParameter("@pk_EmployeeLastName", "Smith", System.Data.OracleClient.OracleType.VarChar, System.Data.ParameterDirection.Input)    

	'' For MySql: use parameter type MySql.Data.MySqlClient.MySqlDbType
	'secondParameter = New BaseClasses.Data.StoredProcedureParameter("@pk_EmployeeLastName", "Smith", MySql.Data.MySqlClient.MySqlDbType.VarChar, System.Data.ParameterDirection.Input)    


	'' Step 2: Add the configured parameters to an array list.
	'Dim parameterList(0) As BaseClasses.Data.StoredProcedureParameter
	'parameterList(0) = firstParameter
	'parameterList(1) = secondParameter

	'' Skip steps 1 and 2 if stored procedure does not accept parameters.

	Dim myStoredProcedure As BaseClasses.Data.StoredProcedure = Nothing

	'' "DatabaseNorthwind1" is a connection string obtained from Web.config 
	'' located in application's root directory.

	'' Step 3: Connect to the stored procedure using following line if 
	'' the stored procedure does not accept any parameters
	myStoredProcedure = New BaseClasses.Data.StoredProcedure("DatabaseNorthwind1", "Stored_Procedure_Name", nothing)

	'' If the stored procedure accepts parameters, use the following
	'myStoredProcedure = New BaseClasses.Data.StoredProcedure("DatabaseNorthwind1", "Stored_Procedure_Name", parameterList)    
    

	'' Step 4: Run the stored procedure.    
	'' RunQuery() will return true if stored procedure successfully executed. Otherwise, it will return false.
	'' Use RunQuery or RunNonQuery.  RunQuery is used when a set of records is being returned.  RunNonQuery is used when one or more values are returned through output parameters.
	If (myStoredProcedure.RunQuery()) Then

		'' Result from stored procedure is available 
		'' as a DataSet or as an array of RecordValue objects.
		return (myStoredProcedure.DataSet)

	Else
		'' You can raise an exception in the custom stored procedure and catch the exception and reporting it to the user.
		'' To raise the exception:
		
		'' SET NOCOUNT ON;
	    	'' RAISERROR (N'My custom error message goes here', 11, 1)
	
		'' IMPORTANT: If you raise an error that has a severity level of 10 or less, it is considered  
		'' a warning, and no exception is raised. The severity of the error must be between 11 and 20
		'' for an exception to be thrown.
	
		'' Once the exception is raised, you can look at:
		'' myStoredProcedure.ErrorMessage to get the text of the error message and use RegisterJScriptAlert to report this to the user.
		return nothing
	End If   
End Function
 
Applies to
Page class
Code
 
	Public Sub LoadData()
		' Load the GridView control when page loads.
		Me.LoadGridControl()
	End Sub

     
Applies to
Page class
Code
 
	''' 
	''' Binds the GridView control with data from stored procedure.
	''' 
	Private Sub LoadGridControl()
		' Enable pagination for the GridView control.
		Me.MyGrid.AllowPaging = True

		' Get the data for GridView control from stored procedure.	
		Me.MyGrid.DataSource = Me.CallCustomStoredProcedureForGridView()

		' Save the data obtained from stored procedure in
		' session to be used re-bind the GridView control
		' after postback occurs since GridView control loses 
		' data after postback occurs.
		Me.Page.Session("DataSet") = Me.MyGrid.DataSource

		' Let the GridView control handle rendering of columns
		' and their names.
		Me.MyGrid.AutoGenerateColumns = True

		' Bind the GridView control with the data from stored procedure.		
		Me.MyGrid.DataBind()
		Me.MyGrid.Visible = True
	End Sub

     
Applies to
Page class
Code
 
	''' 
	''' Handles pagination for the GridView control.
	''' 
	Protected Sub gridView_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
		' Set the GridView control to display
		' new page
		Me.MyGrid.PageIndex = e.NewPageIndex
		
		' Obtain the stored procedure data
		' that was saved in session.
		Dim ds As Object = Me.Page.Session("DataSet")
		If (Not IsNothing(ds)) Then
			' Re-bind the GridView control with the data
			' from session.
			Me.MyGrid.DataSource = CType(ds, DataSet)
			Me.MyGrid.DataBind()
			Me.MyGrid.Visible = True
		Else
			Me.MyGrid.Visible = False
		End If
	End Sub
    
     

Terms of Service Privacy Statement