wiring up a datagrid button

tbcmartinharvey

Newcomer
Joined
Oct 7, 2005
Messages
10
This is probably a very simple problem but i would appreciate some help.

I have wired up a datagrid button with the following code.

Sub AddProduct(sender As Object, e As DataGridCommandEventArgs)
Dim ProductID As Integer = CType(dg.DataKeys(e.Item.ItemIndex), Integer)
' Create the connection object
Dim connection As New SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
' Create and initialize the command object
Dim command As New SqlCommand("SP_AddToCart", connection)
command.CommandType = CommandType.StoredProcedure.........................

This code works fine on the page


I am trying to compile a public shared function using the following

Imports System
Imports System.Data
ImportS System.Data.SqlClient
Imports System.configuration
Imports System.Web

Namespace worldshop
Public Class ShoppingCart

Public Shared Function AddProduct(sender As Object, e As DataGridCommandEventArgs)
Dim ProductID As Integer = CType(dg.DataKeys(e.Item.ItemIndex), Integer)
' Create the connection object
Dim connection As New SqlConnection(connectionString)
' Create and initialize the command object
Dim command As New SqlCommand("SP_AddToCart", connection)
command.CommandType = CommandType.StoredProcedure

But i get the the following error:

Public Shared Function AddProduct(sender As Object, e As DataGridCommandEven
tArgs) ~~~~~~~~~~~~~~~~~~~~~~~~
C:\worldshopdevelopement\ShoppingCart.vb(53) : error BC30451: ?? 'dg' ??????????
? Dim ProductID As Integer = CType(dg.DataKeys(e.Item.ItemIndex), Integer
)

Can anyone tell me where i am going wrong

Many thanks

Martin
 
BC30451 means a variable you are using is not declared. The line number will give you a reference. If you use Option Strict you'll see this error before you compile it.
 
Back
Top