passing more then one parameter in CommandArgument

M

Me LK

Hi,

I understand the code for passing a parameter of one field item from a
button using the command argument. What I need to do is pass three
fields. For example

My button code currently is as follows:

--------------------------------------------------------------------------------------------
Inside a data grid

<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton id=linkButton CommandArgument='<
%#databinder.eval(Container.dataitem, "intProductID") %>' Text="Buy"
Runat="server"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>

--------------------------------------------------------------------------------------------------
My codebehind is

Private Sub itemInfo_itemCommand(ByVal source As Object, ByVal e As
System.web.UI.WebControls.DataGridCommandEventArgs) Handles
itemInfo.ItemCommand
'The command arguement of the button that was clicked
'In the datagrid contains the productID
Dim intProductID As Integer = e.CommandArgument
'Add the product to the shopping cart
ShoppingCart.AddProduct(intProductID)

End Sub
------------------------------------------------------------------------------------------

ShoppingCart.AddProduct(intProductID) is in a class document and is as
follows

Public Shared Function AddProduct(ByVal intProductID As Integer)


' Create the connection object
Dim connection As New SqlConnection(connectionString)
' Create and initialize the command object
Dim command As New SqlCommand("AddProductToCart", connection)
command.CommandType = CommandType.StoredProcedure
' Add an input parameter and supply a value for it
command.Parameters.Add("@CartID", SqlDbType.Char, 36)
command.Parameters("@CartID").Value = shoppingCartID
' Add an input parameter and supply a value for it
command.Parameters.Add("@intProductID", SqlDbType.Int)
command.Parameters("@intProductID").Value = intProductID

' Open the connection, execute the command, and close the
connection
Try
connection.Open()
command.ExecuteNonQuery()
Finally
connection.Close()
End Try
End Function



Now I not only need to pass IntProductID but also size and color. How
will I create three parameters using a button?

Would love ideas

Thanks
LK
 
T

tomisarobot

i hope there are better options, but here are 2 ideas.

concatenate your fields in the argument and split them on the other
end.

set the DataGrid's DataKeyField and use it to lookup your arguments
again later from a persisted object.
 
E

Elroyskimms

Hi,

I understand the code for passing a parameter of one field item from a
button using the command argument. What I need to do is pass three
fields. For example

My button code currently is as follows:

---------------------------------------------------------------------------­-----------------
Inside a data grid

<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton id=linkButton CommandArgument='<
%#databinder.eval(Container.dataitem, "intProductID") %>' Text="Buy"
Runat="server"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>

---------------------------------------------------------------------------­-----------------------
My codebehind is

Private Sub itemInfo_itemCommand(ByVal source As Object, ByVal e As
System.web.UI.WebControls.DataGridCommandEventArgs) Handles
itemInfo.ItemCommand
'The command arguement of the button that was clicked
'In the datagrid contains the productID
Dim intProductID As Integer = e.CommandArgument
'Add the product to the shopping cart
ShoppingCart.AddProduct(intProductID)

End Sub
---------------------------------------------------------------------------­---------------

ShoppingCart.AddProduct(intProductID) is in a class document and is as
follows

Public Shared Function AddProduct(ByVal intProductID As Integer)

' Create the connection object
Dim connection As New SqlConnection(connectionString)
' Create and initialize the command object
Dim command As New SqlCommand("AddProductToCart", connection)
command.CommandType = CommandType.StoredProcedure
' Add an input parameter and supply a value for it
command.Parameters.Add("@CartID", SqlDbType.Char, 36)
command.Parameters("@CartID").Value = shoppingCartID
' Add an input parameter and supply a value for it
command.Parameters.Add("@intProductID", SqlDbType.Int)
command.Parameters("@intProductID").Value = intProductID

' Open the connection, execute the command, and close the
connection
Try
connection.Open()
command.ExecuteNonQuery()
Finally
connection.Close()
End Try
End Function

Now I not only need to pass IntProductID but also size and color. How
will I create three parameters using a button?

Would love ideas

Thanks
LK



<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton
id=linkButton
CommandArgument='<%# databinder.eval(Container.dataitem,
"intProductID") & "|" & databinder.eval(Container.dataitem,
"charProductColor") & "|" & databinder.eval(Container.dataitem,
"intProductSize") %>' Text="Buy"
Runat="server"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>

In your ItemCommand event handler:
dim Args(2) as string = cstr(e.CommandName).Split("|")
dim ProductID as integer = cint(Args(0))
dim ProductColor as string = Args(1)
dim ProductSize as integer = cint(Args(2))

HTH,

-E
 

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

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top