Friday, March 23, 2012

Saving data to another table

Hi

I'm making a very primitive shopping cart. I've created a table containing a list of products and using an SQLdataSource with a GridView I've got the products displayed – simple.

What I now need is to add a button with an Insert command, but the SQL needs to add a row of data to a different table – tblShoppingBasket. I've tried adding in the insert command and changing the SQL but it doesn't add a new record, although is also doesn't through and error.

The second part of the problem is need to pass the productID from the row they selected (pressed the button that triggered the insert command) and add this to the shoppingBasket table via the insert SQL.

I'd really be grateful for any help (code below).

Thanks
Richard

<asp:SqlDataSourceID="SqlDataSource_GenralProducts"runat="server"ConnectionString="<%$ ConnectionStrings:AquilaConnectionString %>"

SelectCommand="SELECT * FROM [tblProducts] WHERE ([productAgeGroup] = @.productAgeGroup)"

InsertCommand="INSERT INTO [tblShoppingBasket] ([userID], [productID]) VALUES (@.userID, @.productID)">

<SelectParameters>

<asp:ParameterDefaultValue="3"Name="productAgeGroup"Type="Int32"/>

</SelectParameters>

<InsertParameters>

<asp:ParameterName="userID"Type="Object"DefaultValue="123"/>

<asp:ParameterName="productID"Type="Int32"/>

</InsertParameters>

</asp:SqlDataSource>

<asp:GridViewID="GridView3"runat="server"AutoGenerateColumns="False"DataKeyNames="productsID"

DataSourceID="SqlDataSource_GenralProducts">

<Columns>

<asp:ImageFieldDataImageUrlField="productThumbNail"DataImageUrlFormatString="../images/shop/thumbNails/{0}">

</asp:ImageField>

<asp:BoundFieldDataField="productLabel"HeaderText="Product:"SortExpression="productLabel"/>

<asp:BoundFieldDataField="productDescription"HeaderText="Description:"SortExpression="productDescription"/>

<asp:BoundFieldDataField="productCost"HeaderText="Cost:"SortExpression="productCost"/>

<asp:ButtonFieldButtonType="Button"CommandName="insert"Text="Add to Shopping Basket"/>

</Columns>

</asp:GridView>

Hi,

You can use RowDataBound event to handle the problem. Here's a sample for you.

In your aspx:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="InsertEvent"> <Columns> <asp:BoundField DataField="CategoryName" /> <asp:TemplateField> <ItemTemplate><asp:Button ID="button1" Text="Button" runat="server" OnClick="InsertEvent" /> </ItemTemplate> </asp:TemplateField> </Columns></asp:GridView>

In your code-behind:

protected void InsertEvent(object sender, GridViewRowEventArgs e) {string CategoryDescription = e.Row.Cells[0].ToString(); /// Here, it depends on which datafield do you want to insert into your datatable? You also can use Cell[int] to access other datafield in the current row.string conn_str=ConfigurationManager.ConnectionStrings["AquilaConnectionString"].ConnectionString; SqlConnection myconn =new SqlConnection(conn_str);string insert_command ="Your insert command here."; SqlCommand mycomm =new SqlCommand(myconn, insert_command); myconn.Open(); mycomm.ExecuteNonQuery(); mycomm.Dispose(); myconn.Close(); }
Thanks.sql

No comments:

Post a Comment