chwong 0 Newbie Poster

HI all,

I am the beginner of ASP.net. My cases scenario, I have a dropdown list (category) field which allow user to make selection then it will display the record on the gridview. I need to add the new record by using the detailview. In my detailview, i cannot showing the (category) field, I need to get the value of category from dropdown list and insert the new record into database. My code as below:

<%@ Page Title="" Language="VB" MasterPageFile="~/MasterPage/MasterPage.master" AutoEventWireup="false" CodeFile="AddItems.aspx.vb" Inherits="AddItem" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">

/this function is filtering the category and display it on the gridview/
<SCRIPT Runat="Server">

Sub Get_Category_Type(ByVal Src As Object, ByVal Args As EventArgs)

Dim SQLString As String
SQLString = "SELECT * FROM products " & _
"WHERE Category = '" & Category.SelectedValue & "' " & _
"ORDER BY ID"
AccessDataSource2.SelectCommand = SQLString

End Sub

</SCRIPT>

/drop down list/
<asp:DropDownList ID="Category" runat="server" AutoPostBack="True"
DataSourceID="AccessDataSource1" DataTextField="Category"
DataValueField="Category" OnSelectedIndexChanged="Get_Category_Type">
</asp:DropDownList>
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/GiftShop.mdb"
SelectCommand="SELECT DISTINCT [Category] FROM [Products]">
</asp:AccessDataSource>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="ID"
DataSourceID="AccessDataSource2">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True"
SortExpression="ID" />
<asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type" />
<asp:BoundField DataField="Description" HeaderText="Description"
SortExpression="Description" />
<asp:BoundField DataField="RRP" HeaderText="RRP" SortExpression="RRP" />
<asp:BoundField DataField="WholesalePrice" HeaderText="WholesalePrice"
SortExpression="WholesalePrice" />
<asp:CheckBoxField DataField="Special" HeaderText="Special"
SortExpression="Special" />
<asp:BoundField DataField="InStock" HeaderText="InStock"
SortExpression="InStock" />
<asp:BoundField DataField="Image" HeaderText="Image" SortExpression="Image" />
<asp:BoundField DataField="LargeImage" HeaderText="LargeImage"
SortExpression="LargeImage" />
<asp:BoundField DataField="Category" HeaderText="Category"
SortExpression="Category" />
</Columns>

/gridview. I am stuck in insert record in the database by adding the value from dropdown list (category), can someone tell me how to do that? /

</asp:GridView>
<asp:AccessDataSource ID="AccessDataSource2" runat="server"
DataFile="~/App_Data/GiftShop.mdb"
DeleteCommand="DELETE FROM [Products] WHERE (([ID] = ?) OR ([ID] IS NULL AND ? IS NULL))"
InsertCommand="INSERT INTO [Products] ([ID], [Type], [Description], [RRP], [WholesalePrice], [Special], [InStock], [Image], [LargeImage], [Category]) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
SelectCommand="SELECT [ID], [Type], [Description], [RRP], [WholesalePrice], [Special], [InStock], [Image], [LargeImage], [Category] FROM [Products]"

UpdateCommand="UPDATE [Products] SET [Type] = ?, [Description] = ?, [RRP] = ?, [WholesalePrice] = ?, [Special] = ?, [InStock] = ?, [Image] = ?, [LargeImage] = ?, [Category] = ? WHERE (([ID] = ?) OR ([ID] IS NULL AND ? IS NULL))">
<DeleteParameters>
<asp:Parameter Name="ID" Type="String" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="Type" Type="String" />
<asp:Parameter Name="Description" Type="String" />
<asp:Parameter Name="RRP" Type="Decimal" />
<asp:Parameter Name="WholesalePrice" Type="Decimal" />
<asp:Parameter Name="Special" Type="Boolean" />
<asp:Parameter Name="InStock" Type="Int32" />
<asp:Parameter Name="Image" Type="String" />
<asp:Parameter Name="LargeImage" Type="String" />
<asp:Parameter Name="Category" Type="String" />
<asp:Parameter Name="ID" Type="String" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="ID" Type="String" />
<asp:Parameter Name="Type" Type="String" />
<asp:Parameter Name="Description" Type="String" />
<asp:Parameter Name="RRP" Type="Decimal" />
<asp:Parameter Name="WholesalePrice" Type="Decimal" />
<asp:Parameter Name="Special" Type="Boolean" />
<asp:Parameter Name="InStock" Type="Int32" />
<asp:Parameter Name="Image" Type="String" />
<asp:Parameter Name="LargeImage" Type="String" />
<asp:Parameter Name="Category" Type="String" />
</InsertParameters>
</asp:AccessDataSource>
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
DataKeyNames="ID" DataSourceID="AccessDataSource2" Height="50px" Width="125px">
<Fields>
<asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True"
SortExpression="ID" />
<asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type" />
<asp:BoundField DataField="Description" HeaderText="Description"
SortExpression="Description" />
<asp:BoundField DataField="RRP" HeaderText="RRP" SortExpression="RRP" />
<asp:BoundField DataField="WholesalePrice" HeaderText="WholesalePrice"
SortExpression="WholesalePrice" />
<asp:CheckBoxField DataField="Special" HeaderText="Special"
SortExpression="Special" />
<asp:BoundField DataField="InStock" HeaderText="InStock"
SortExpression="InStock" />
<asp:BoundField DataField="Image" HeaderText="Image" SortExpression="Image" />
<asp:BoundField DataField="LargeImage" HeaderText="LargeImage"
SortExpression="LargeImage" />
<asp:BoundField DataField="Category" HeaderText="Category"
SortExpression="Category" /> /*this code is not meet to my requirement because it is bound to the database */
<asp:CommandField ShowInsertButton="True" />

</Fields>
</asp:DetailsView>
</asp:Content>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.