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="" 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>

Dani AI

Generated

For : the DetailsView insert is not automatically taking the DropDownList choice unless that value is explicitly provided to the data source or to the DetailsView insert values. Two simple, reliable fixes are: bind the AccessDataSource insert parameter directly to the DropDownList (markup) or inject the selected value in the DetailsView inserting event (code-behind).

Markup approach — add a ControlParameter that points at the DropDownList's SelectedValue. Place it in the InsertParameters collection so it lines up with the Access INSERT placeholders:

<InsertParameters>
  <asp:ControlParameter Name="Category" ControlID="Category" PropertyName="SelectedValue" Type="String" />
</InsertParameters>

(ControlParameter docs: ControlParameter class.)

Code-behind approach — handle the DetailsView ItemInserting event and set the Category value programmatically. This is useful when some validation/transformation is needed before insert:

Protected Sub DetailsView1_ItemInserting(sender As Object, e As DetailsViewInsertEventArgs)
    e.Values("Category") = Category.SelectedValue
End Sub

(See the DetailsView ItemInserting event: DetailsView.ItemInserting.)

Troubleshooting notes: when using Access/OleDb the SQL uses positional ? parameters, so ensure the ControlParameter appears in the same order as the placeholders. If the Category field is a BoundField that doesn’t create an editor in Insert mode, e.Values("Category") may be missing — either make the field insert-visible, add a hidden input in an InsertItemTemplate, or inject the value as shown above. Verify Category.SelectedValue is non-empty before inserting and set breakpoints/log to confirm the value reaches the insert parameters.

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.