Datagrid button column gives BC30456: "not a member" error

T

Tom

Hi,

Relatively new to asp.net and having some trouble wiring up a button
column in a datagrid. I keep getting the error: BC30456: 'AddToCart'
is not a member of 'ASP.homepage_aspx'.
Have been following the guidelines in ASP.NET Data Web Controls, but
seem to be pounding my head on the wall.

Probably an easy fix for one of you all. Would really appreciate your
input. Thanks in advance.

Tom

Here's the code:

<body MS_POSITIONING="GridLayout">
<form id="homepage" action="browse_cat.aspx" method="post"
runat="server">
<asp:label id="lblCategory" style="Z-INDEX: 101; LEFT:
251px; POSITION: absolute; TOP: 10px" runat="server" Width="59px"
Height="30px">Category</asp:label>
<asp:dropdownlist id="ddlCategories" style="Z-INDEX: 102;
LEFT: 327px; POSITION: absolute; TOP: 10px" runat="server"
Width="218px" Height="31px" AutoPostBack="True"></asp:dropdownlist>
<asp:datagrid id="grdProducts" style="Z-INDEX: 103; LEFT:
44px; POSITION: absolute; TOP: 47px" runat="server" Width="575px"
Height="148px" OnItemCommand="AddToCart" AutoGenerateColumns="False"
AllowPaging="True">
<Columns>
<asp:BoundColumn DataField="product_num"
HeaderText="Product #"></asp:BoundColumn>
<asp:BoundColumn DataField="description"
HeaderText="Description"></asp:BoundColumn>
<asp:BoundColumn DataField="price"
HeaderText="Price"></asp:BoundColumn>
<asp:BoundColumn DataField="qty_on_hand"
HeaderText="Qty On-Hand"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Qty">
<EditItemTemplate>
<asp:TextBox id=txtAddToCart
runat="server" Text='<%# DataBinder.Eval(DsCart1,
"Tables[Cart].DefaultView.[0].cart_qty_ord") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:ButtonColumn Text="Add"
ButtonType="PushButton" HeaderText="Add to Cart"
CommandName="Add2Cart"></asp:ButtonColumn>
</Columns>
</asp:datagrid></form>
</body>
</HTML>

Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.UI.WebControls


Public Class WebForm1
Inherits System.Web.UI.Page
Protected WithEvents DsCategory1 As test3.dsCategory
Protected WithEvents lblCategory As
System.Web.UI.WebControls.Label
Protected WithEvents ddlCategories As
System.Web.UI.WebControls.DropDownList

Protected WithEvents grdProducts As
System.Web.UI.WebControls.DataGrid


#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form
Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not Page.IsPostBack Then
CategoryLoad()
End If

End Sub

Private Sub CategoryLoad()
'create a connection
Const strCatLoadConn As String =
"server=localhost;uid=sa;pwd=;database=tsproductsSQL3"

' data source=ENVISAGENT1;initial
catalog=tsproductsSQL3;integrated security=SSPI;persist security
info=False;workstation id=ENVISAGENT1;packet size=4096
Dim objCatLoadConn As New SqlConnection(strCatLoadConn)


'create command object for query
Dim strSQLCatLoad As String = "select category_num,
category_description " & _
"from category_detail"


objCatLoadConn.Open() 'open connection

'specify data adapter and data source and call databind
Dim da2 As SqlDataAdapter
Dim ds2 As DataSet

ds2 = New DataSet()
da2 = New SqlDataAdapter(strSQLCatLoad, strCatLoadConn)
da2.Fill(ds2)

With ddlCategories
.DataTextField = "category_description"
.DataValueField = "category_num"
.DataSource = ds2
.DataBind()
End With
objCatLoadConn.Close() 'close connection

End Sub

Private Sub ddlCategories_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
ddlCategories.SelectedIndexChanged
ProductsLoad()
End Sub

Private Sub ProductsLoad()
'create a connection
Const strProdLoadConn As String =
"server=localhost;uid=sa;pwd=;database=tsproductsSQL3"
Dim objProdLoadConn As New SqlConnection(strProdLoadConn)
Dim dsProdLoad As DataSet
Dim daProdLoad As SqlDataAdapter

dsProdLoad = New DataSet()

'create command object for query
Dim strSQLProdLoad As String =
daProdLoad.SelectCommand.CommandText & _
" Where category_num = " & _
ddlCategories.SelectedItem.Value

objProdLoadConn.Open() 'open connection

'specify data adapter and data source and call databind
daProdLoad = New SqlDataAdapter(strSQLProdLoad,
strProdLoadConn)
daProdLoad.Fill(dsProdLoad)

With grdProducts
.DataSource = dsProdLoad
.DataBind()
End With
objprodLoadConn.Close() 'close connection
End Sub

Sub AddToCart(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs)
'Take item selected and put it into shopping cart
Dim ProdNumCol As TableCell = e.Item.Cells(0)
Dim DescCol As TableCell = e.Item.Cells(1)
Dim PriceCol As TableCell = e.Item.Cells(2)
Dim Qoh As TableCell = e.Item.Cells(3)
Dim Qty As TableCell = e.Item.Cells(4)
Dim AddTo As TableCell = e.Item.Cells(5)

End Sub





Private Sub grdProducts_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
grdProducts.SelectedIndexChanged

End Sub
End Class
 
K

Ken Cox [Microsoft MVP]

Hi Tom,

I don't see where you are capturing the ItemCommand event. If you add this
to your code does it help?

Private Sub grdProducts_ItemCommand _
(ByVal source As Object, _
ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) _
Handles grdProducts.ItemCommand
Call AddToCart(source, e)
End Sub

Ken
Microsoft MVP [ASP.NET]


Tom said:
Hi,

Relatively new to asp.net and having some trouble wiring up a button
column in a datagrid. I keep getting the error: BC30456: 'AddToCart'
is not a member of 'ASP.homepage_aspx'.
Have been following the guidelines in ASP.NET Data Web Controls, but
seem to be pounding my head on the wall.

Probably an easy fix for one of you all. Would really appreciate your
input. Thanks in advance.

Tom

Here's the code:

<body MS_POSITIONING="GridLayout">
<form id="homepage" action="browse_cat.aspx" method="post"
runat="server">
<asp:label id="lblCategory" style="Z-INDEX: 101; LEFT:
251px; POSITION: absolute; TOP: 10px" runat="server" Width="59px"
Height="30px">Category</asp:label>
<asp:dropdownlist id="ddlCategories" style="Z-INDEX: 102;
LEFT: 327px; POSITION: absolute; TOP: 10px" runat="server"
Width="218px" Height="31px" AutoPostBack="True"></asp:dropdownlist>
<asp:datagrid id="grdProducts" style="Z-INDEX: 103; LEFT:
44px; POSITION: absolute; TOP: 47px" runat="server" Width="575px"
Height="148px" OnItemCommand="AddToCart" AutoGenerateColumns="False"
AllowPaging="True">
<Columns>
<asp:BoundColumn DataField="product_num"
HeaderText="Product #"></asp:BoundColumn>
<asp:BoundColumn DataField="description"
HeaderText="Description"></asp:BoundColumn>
<asp:BoundColumn DataField="price"
HeaderText="Price"></asp:BoundColumn>
<asp:BoundColumn DataField="qty_on_hand"
HeaderText="Qty On-Hand"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Qty">
<EditItemTemplate>
<asp:TextBox id=txtAddToCart
runat="server" Text='<%# DataBinder.Eval(DsCart1,
"Tables[Cart].DefaultView.[0].cart_qty_ord") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:ButtonColumn Text="Add"
ButtonType="PushButton" HeaderText="Add to Cart"
CommandName="Add2Cart"></asp:ButtonColumn>
</Columns>
</asp:datagrid></form>
</body>
</HTML>

Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.UI.WebControls


Public Class WebForm1
Inherits System.Web.UI.Page
Protected WithEvents DsCategory1 As test3.dsCategory
Protected WithEvents lblCategory As
System.Web.UI.WebControls.Label
Protected WithEvents ddlCategories As
System.Web.UI.WebControls.DropDownList

Protected WithEvents grdProducts As
System.Web.UI.WebControls.DataGrid


#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form
Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not Page.IsPostBack Then
CategoryLoad()
End If

End Sub

Private Sub CategoryLoad()
'create a connection
Const strCatLoadConn As String =
"server=localhost;uid=sa;pwd=;database=tsproductsSQL3"

' data source=ENVISAGENT1;initial
catalog=tsproductsSQL3;integrated security=SSPI;persist security
info=False;workstation id=ENVISAGENT1;packet size=4096
Dim objCatLoadConn As New SqlConnection(strCatLoadConn)


'create command object for query
Dim strSQLCatLoad As String = "select category_num,
category_description " & _
"from category_detail"


objCatLoadConn.Open() 'open connection

'specify data adapter and data source and call databind
Dim da2 As SqlDataAdapter
Dim ds2 As DataSet

ds2 = New DataSet()
da2 = New SqlDataAdapter(strSQLCatLoad, strCatLoadConn)
da2.Fill(ds2)

With ddlCategories
.DataTextField = "category_description"
.DataValueField = "category_num"
.DataSource = ds2
.DataBind()
End With
objCatLoadConn.Close() 'close connection

End Sub

Private Sub ddlCategories_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
ddlCategories.SelectedIndexChanged
ProductsLoad()
End Sub

Private Sub ProductsLoad()
'create a connection
Const strProdLoadConn As String =
"server=localhost;uid=sa;pwd=;database=tsproductsSQL3"
Dim objProdLoadConn As New SqlConnection(strProdLoadConn)
Dim dsProdLoad As DataSet
Dim daProdLoad As SqlDataAdapter

dsProdLoad = New DataSet()

'create command object for query
Dim strSQLProdLoad As String =
daProdLoad.SelectCommand.CommandText & _
" Where category_num = " & _
ddlCategories.SelectedItem.Value

objProdLoadConn.Open() 'open connection

'specify data adapter and data source and call databind
daProdLoad = New SqlDataAdapter(strSQLProdLoad,
strProdLoadConn)
daProdLoad.Fill(dsProdLoad)

With grdProducts
.DataSource = dsProdLoad
.DataBind()
End With
objprodLoadConn.Close() 'close connection
End Sub

Sub AddToCart(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs)
'Take item selected and put it into shopping cart
Dim ProdNumCol As TableCell = e.Item.Cells(0)
Dim DescCol As TableCell = e.Item.Cells(1)
Dim PriceCol As TableCell = e.Item.Cells(2)
Dim Qoh As TableCell = e.Item.Cells(3)
Dim Qty As TableCell = e.Item.Cells(4)
Dim AddTo As TableCell = e.Item.Cells(5)

End Sub





Private Sub grdProducts_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
grdProducts.SelectedIndexChanged

End Sub
End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top