Link to home
Start Free TrialLog in
Avatar of NaumLitvin
NaumLitvin

asked on

How to connect DropDown Lists in Edit/Insert templates of a DetailView with stored procedure parameters?

I use DetailView to update/insert records into the tbl_Items table. I need a field (Category) to get its values from tbl_Categories table for edit/insert.

Inserted DropDown boxes in appropriate templates.

In Edit mode I am getting an error even before I can see the form:

'ddlDV_Category_Edit' has a SelectedValue which is invalid because it does not exist in the list of items.

Parameter name: value

I am not sure what is missing... SelectedValue is what user selects in the drop-down. Somehow it should get into appropriate parameter for Update command - not clear how to hook them up...

In Insert mode it seems to be working OK, I can see my drop-down and select a value but it does not get inserted... Cell simply is empty... Most likely it is also because I did not provide any connection between drop-down's SelectedValye and parameter for Insert command.

Source code is also included below, after update and insert stored procedures. full error message is at the end. If needed I can add CodeBehind file...

TIA

ALTER
 PROCEDURE [dbo].[asp_items_insert]
@ItemNbr int,@Year smallint,@ItemName varchar(75),@ImageFilename varchar(50),@ImageBigFilename [varchar](50),@Description [varchar](255) 
,@Footnote [text],@Value [money] ,@Donor [varchar](80),@DonorAddress [varchar](120),@DonorPhone [varchar](30) ,@DonorEmail [varchar](80) 
,@DonorWebsite [varchar](128),@Category [varchar](40),@Legacy_PK [int] ,@CollectorNTID [varchar](12) 
AS
BEGIN
SET NOCOUNT ON;
INSERT  [dbo].[tbl_Items]
 ([Year],[ItemName],[ImageFilename],[ImageBigFilename],[Description],[Footnote],[Value],[Donor],[DonorAddress],[DonorPhone],[DonorEmail],[DonorWebsite],[Category],[Legacy_PK],[CollectorNTID])
VALUES
(@Year,@ItemName,@ImageFilename,@ImageBigFilename,@Description,@Footnote,@Value,@Donor,@DonorAddress,@DonorPhone,@DonorEmail,@DonorWebsite,@Category,@Legacy_PK,@CollectorNTID)
SELECT [ItemNbr],[Year],[ItemName],[ImageFilename],[ImageBigFilename],[Description],[Footnote],[Value],[Donor],[DonorAddress],[DonorPhone],[DonorEmail],[DonorWebsite],[Category],[Legacy_PK],[CollectorNTID]
FROM tbl_Items WHERE (ItemNbr = SCOPE_IDENTITY())
end
 
 
 
ALTER PROCEDURE [dbo].[asp_items_update]
@ItemNbr int,@Year smallint,@ItemName varchar(75),@ImageFilename varchar(50),@ImageBigFilename [varchar](50)
,@Description [varchar](255) ,@Footnote [text],@Value [money] ,@Donor [varchar](80),@DonorAddress [varchar](120)
,@DonorPhone [varchar](30) ,@DonorEmail [varchar](80) ,@DonorWebsite [varchar](128) ,@Category [varchar](40),
@Legacy_PK [int] ,@CollectorNTID [varchar](12) 
AS
BEGIN
SET NOCOUNT ON;
UPDATE  [dbo].[tbl_Items]SET [Year] = @Year,[ItemName] = @ItemName,[ImageFilename] = @ImageFilename,[ImageBigFilename] = @ImageBigFilename,[Description] = @Description,[Footnote] = @Footnote,[Value] = @Value,[Donor] = @Donor,[DonorAddress] = @DonorAddress,[DonorPhone] = @DonorPhone,[DonorEmail] = @DonorEmail,[DonorWebsite] = @DonorWebsite,[Category] = @Category,[Legacy_PK] = @Legacy_PK,[CollectorNTID] = @CollectorNTID
WHERE ItemNbr = @ItemNbr
end
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Admin_Items_01.aspx.cs" Inherits="AAA_01.Admin_Items_01" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Admin_Table_Items</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Style="position: relative; left: 91px; top: -10px;" Text="Admin Item Numbers Table" Font-Bold="True" Font-Size="X-Large" Height="26px" Width="298px"></asp:Label>
        <asp:Button ID="btnShowDetails" runat="server" OnClick="btnShowDetails_Click" Style="left: 137px; position: relative; top: 1px" Text="Show Details" />
        <asp:Button ID="btnShowMaster" runat="server" Style="left: 144px; position: relative; top: 1px" Text="Show Master" OnClick="btnShowMaster_Click" />
        <asp:Button ID="btnInsert" runat="server" OnClick="btnInsert_Click" Style="left: 157px; position: relative; top: 1px" Text="Insert Mode" Width="94px" />
        <hr style="position: relative" />
        <br />
        <asp:ValidationSummary ID="ValidationSummary1" runat="server" Font-Bold="True" Font-Size="X-Large" HeaderText="Validation Summary" ShowMessageBox="True" ShowSummary="False" Style="left: 0px; position: relative; top: -25px" />
        <br />
        <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" BackColor="White" 
            BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" 
            DataKeyNames="ItemNbr" DataSourceID="Details_SqlDataSource" GridLines="Vertical" 
            Height="53px" Style="position: relative; left: -2px; top: -49px;" Width="100%" 
            OnItemDeleted="DetailsView1_ItemDeleted"  OnItemInserted="DetailsView1_ItemInserted" OnItemUpdated="DetailsView1_ItemUpdated"   
            >
            <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
            <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
            <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
            <Fields>
                <asp:BoundField DataField="ItemNbr" HeaderText="ItemNbr" ReadOnly="True" SortExpression="ItemNbr" InsertVisible="False" />
                <asp:BoundField DataField="Year" HeaderText="Year" SortExpression="Year" >
                    <HeaderStyle Font-Bold="False" />
                </asp:BoundField>
                <asp:BoundField DataField="ItemName" HeaderText="ItemName" SortExpression="ItemName" />
                <asp:BoundField DataField="ImageFilename" HeaderText="ImageFilename" SortExpression="ImageFilename" />
                <asp:BoundField DataField="ImageBigFilename" HeaderText="ImageBigFilename" SortExpression="ImageBigFilename" />
            
                <asp:TemplateField HeaderText="Description" SortExpression="Description">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtDV_Description_Edit" runat="server" Height="40px" 
                        Text='<%# Bind("Description") %>' TextMode="MultiLine" Width="98%"></asp:TextBox>
                    </EditItemTemplate>
                    <InsertItemTemplate>
                        <asp:TextBox ID="txtDV_Description_Insert" runat="server" Height="40px" 
                        Text='<%# Bind("Description") %>' TextMode="MultiLine" Width="98%">
                        </asp:TextBox>
                        <asp:RequiredFieldValidator ID="rfv_txtDV_Description_Insert" 
                        runat="server" ControlToValidate="txtDV_Description_Insert" 
                        ErrorMessage="Please, provide DESCRIPTION" 
                        Style="position: relative" Font-Bold="True" Font-Size="Large">*******                    </asp:RequiredFieldValidator><br />
                    </InsertItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("Description") %>'></asp:Label>
                    </ItemTemplate>
                    <ControlStyle Height="40px" Width="98%" />
                    <ItemStyle Wrap="True" />
                </asp:TemplateField>
            
                <asp:TemplateField HeaderText="Footnote" SortExpression="Footnote">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtDV_Footnote_Edit" runat="server" 
                        Text='<%# Bind("Footnote") %>' TextMode="MultiLine" Width="98%"></asp:TextBox>
                    </EditItemTemplate>
                    <InsertItemTemplate>
                        <asp:TextBox ID="txtDV_Footnote_Insert" runat="server" 
                        Text='<%# Bind("Footnote") %>' TextMode="MultiLine" Width="98%"></asp:TextBox>
                    </InsertItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("Footnote") %>'></asp:Label>
                    </ItemTemplate>
                    <ControlStyle Height="40px" Width="98%" />
                    <ItemStyle Wrap="True" />
                </asp:TemplateField>
            
                <asp:BoundField DataField="Value" HeaderText="Value" SortExpression="Value" />
                <asp:BoundField DataField="Donor" HeaderText="Donor" SortExpression="Donor" />
                <asp:BoundField DataField="DonorAddress" HeaderText="DonorAddress" SortExpression="DonorAddress" />
                <asp:BoundField DataField="DonorPhone" HeaderText="DonorPhone" SortExpression="DonorPhone" />
                <asp:BoundField DataField="DonorEmail" HeaderText="DonorEmail" SortExpression="DonorEmail" />
                <asp:BoundField DataField="DonorWebsite" HeaderText="DonorWebsite" SortExpression="DonorWebsite" />
                <asp:TemplateField HeaderText="Category" SortExpression="Category">
                    <EditItemTemplate>
                        <asp:DropDownList ID="ddlDV_Category_Edit" runat="server" DataSourceID="getCategory" DataTextField="CategoryName" DataValueField="CategoryName" SelectedValue='<%# Bind("Category") %>' Style="position: relative">
                        </asp:DropDownList>
                    </EditItemTemplate>
                    <InsertItemTemplate>
                         <asp:DropDownList ID="ddlDV_Category_Insert" runat="server" DataSourceID="getCategories" DataTextField="CategoryName" DataValueField="CategoryName" Style="position: relative">
                        </asp:DropDownList>
                    </InsertItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label3" runat="server" Text='<%# Bind("Category") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="Legacy_PK" HeaderText="Legacy_PK" SortExpression="Legacy_PK" />
                <asp:BoundField DataField="CollectorNTID" HeaderText="CollectorNTID" SortExpression="CollectorNTID" />
                <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowInsertButton="True" />
            </Fields>
            <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" Width="100%" />
            <AlternatingRowStyle BackColor="Gainsboro" />
            <FieldHeaderStyle Width="10%" />
        </asp:DetailsView>
              
        <asp:GridView ID="GridView1" runat="server" 
            AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" BackColor="White" 
            BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" 
            DataKeyNames="ItemNbr" DataSourceID="Grid_SqlDataSource" GridLines="Vertical" 
            Style="position: relative; left: 0px; top: -65px;" PageSize="20">
            <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
            <Columns>
                <asp:CommandField ShowSelectButton="True" />
                <asp:BoundField DataField="ItemNbr" HeaderText="ItemNbr" InsertVisible="False" ReadOnly="True" SortExpression="ItemNbr" />
                <asp:BoundField DataField="Year" HeaderText="Year" SortExpression="Year" />
                <asp:BoundField DataField="ItemName" HeaderText="ItemName" SortExpression="ItemName" />
                <asp:BoundField DataField="ImageFilename" HeaderText="ImageFilename" SortExpression="ImageFilename" />
                <asp:BoundField DataField="ImageBigFilename" HeaderText="ImageBigFilename" SortExpression="ImageBigFilename" />
                <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
                <asp:BoundField DataField="Footnote" HeaderText="Footnote" SortExpression="Footnote" />
                <asp:BoundField DataField="Value" HeaderText="Value" SortExpression="Value" />
                <asp:BoundField DataField="Donor" HeaderText="Donor" SortExpression="Donor" />
                <asp:BoundField DataField="DonorAddress" HeaderText="DonorAddress" SortExpression="DonorAddress" />
                <asp:BoundField DataField="DonorPhone" HeaderText="DonorPhone" SortExpression="DonorPhone" />
                <asp:BoundField DataField="DonorEmail" HeaderText="DonorEmail" SortExpression="DonorEmail" />
                <asp:BoundField DataField="DonorWebsite" HeaderText="DonorWebsite" SortExpression="DonorWebsite" />
                <asp:BoundField DataField="Category" HeaderText="Category" SortExpression="Category" />
                <asp:BoundField DataField="Legacy_PK" HeaderText="Legacy_PK" SortExpression="Legacy_PK" />
                <asp:BoundField DataField="CollectorNTID" HeaderText="CollectorNTID" SortExpression="CollectorNTID" />
            </Columns>
            <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
            <PagerStyle BackColor="#999999" ForeColor="White" HorizontalAlign="Left" />
            <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="Gainsboro" />
            <PagerSettings FirstPageText="First" LastPageText="Last" Mode="NumericFirstLast" PageButtonCount="20" Position="TopAndBottom" />
        </asp:GridView>
        <br />
    
    </div>
        <asp:SqlDataSource ID="Grid_SqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:devauctionConnectionString %>" 
            SelectCommand="asp_items_get" SelectCommandType="StoredProcedure">
            <%--
            DeleteCommand="asp_items_delete" DeleteCommandType="StoredProcedure" 
            InsertCommand="asp_items_insert" InsertCommandType="StoredProcedure" 
            UpdateCommand="asp_items_update" UpdateCommandType="StoredProcedure">
            <DeleteParameters>
                <asp:Parameter Name="ItemNbr" Type="Int32" />
            </DeleteParameters>
            <UpdateParameters>
                <asp:Parameter Name="ItemNbr" Type="Int32" />
                <asp:Parameter Name="Year" Type="Int16" />
                <asp:Parameter Name="ItemName" Type="String" />
                <asp:Parameter Name="ImageFilename" Type="String" />
                <asp:Parameter Name="ImageBigFilename" Type="String" />
                <asp:Parameter Name="Description" Type="String" />
                <asp:Parameter Name="Footnote" Type="String" />
                <asp:Parameter Name="Value" Type="Decimal" />
                <asp:Parameter Name="Donor" Type="String" />
                <asp:Parameter Name="DonorAddress" Type="String" />
                <asp:Parameter Name="DonorPhone" Type="String" />
                <asp:Parameter Name="DonorEmail" Type="String" />
                <asp:Parameter Name="DonorWebsite" Type="String" />
                <asp:Parameter Name="Category" Type="String" />
                <asp:Parameter Name="Legacy_PK" Type="Int32" />
                <asp:Parameter Name="CollectorNTID" Type="String" />
            </UpdateParameters>
            <InsertParameters>
                <asp:Parameter Name="Year" Type="Int16" />
                <asp:Parameter Name="ItemName" Type="String" />
                <asp:Parameter Name="ImageFilename" Type="String" />
                <asp:Parameter Name="ImageBigFilename" Type="String" />
                <asp:Parameter Name="Description" Type="String" />
                <asp:Parameter Name="Footnote" Type="String" />
                <asp:Parameter Name="Value" Type="Decimal" />
                <asp:Parameter Name="Donor" Type="String" />
                <asp:Parameter Name="DonorAddress" Type="String" />
                <asp:Parameter Name="DonorPhone" Type="String" />
                <asp:Parameter Name="DonorEmail" Type="String" />
                <asp:Parameter Name="DonorWebsite" Type="String" />
                <asp:Parameter Name="Category" Type="String" />
                <asp:Parameter Name="Legacy_PK" Type="Int32" />
                <asp:Parameter Name="CollectorNTID" Type="String" />
            </InsertParameters>
            --%>
        </asp:SqlDataSource>
        <asp:SqlDataSource ID="Details_SqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:devauctionConnectionString %>" DeleteCommand="asp_items_delete" DeleteCommandType="StoredProcedure" InsertCommand="asp_items_insert" InsertCommandType="StoredProcedure" SelectCommand="asp_item_get" SelectCommandType="StoredProcedure" UpdateCommand="asp_items_update" UpdateCommandType="StoredProcedure">
            <SelectParameters>
                <asp:ControlParameter ControlID="GridView1" DefaultValue="1341" Name="ItemNbr" PropertyName="SelectedValue" Type="Int32" />
            </SelectParameters>
            <DeleteParameters>
                <asp:Parameter Name="ItemNbr" Type="Int32" />
            </DeleteParameters>
            <UpdateParameters>
                <asp:Parameter Name="ItemNbr" Type="Int32" />
                <asp:Parameter Name="Year" Type="Int16" />
                <asp:Parameter Name="ItemName" Type="String" />
                <asp:Parameter Name="ImageFilename" Type="String" />
                <asp:Parameter Name="ImageBigFilename" Type="String" />
                <asp:Parameter Name="Description" Type="String" />
                <asp:Parameter Name="Footnote" Type="String" />
                <asp:Parameter Name="Value" Type="Decimal" />
                <asp:Parameter Name="Donor" Type="String" />
                <asp:Parameter Name="DonorAddress" Type="String" />
                <asp:Parameter Name="DonorPhone" Type="String" />
                <asp:Parameter Name="DonorEmail" Type="String" />
                <asp:Parameter Name="DonorWebsite" Type="String" />
                <asp:Parameter Name="Category" Type="String" />
                <asp:Parameter Name="Legacy_PK" Type="Int32" />
                <asp:Parameter Name="CollectorNTID" Type="String" />
            </UpdateParameters>
            <InsertParameters>
                <asp:Parameter Name="ItemNbr" Type="Int32" />
                <asp:Parameter Name="Year" Type="Int16" />
                <asp:Parameter Name="ItemName" Type="String" />
                <asp:Parameter Name="ImageFilename" Type="String" />
                <asp:Parameter Name="ImageBigFilename" Type="String" />
                <asp:Parameter Name="Description" Type="String" />
                <asp:Parameter Name="Footnote" Type="String" />
                <asp:Parameter Name="Value" Type="Decimal" />
                <asp:Parameter Name="Donor" Type="String" />
                <asp:Parameter Name="DonorAddress" Type="String" />
                <asp:Parameter Name="DonorPhone" Type="String" />
                <asp:Parameter Name="DonorEmail" Type="String" />
                <asp:Parameter Name="DonorWebsite" Type="String" />
                <asp:Parameter Name="Category" Type="String" />
                <asp:Parameter Name="Legacy_PK" Type="Int32" />
                <asp:Parameter Name="CollectorNTID" Type="String" />
            </InsertParameters>
        </asp:SqlDataSource>
        <asp:SqlDataSource ID="getCategories" runat="server" ConnectionString="<%$ ConnectionStrings:devauctionConnectionString %>" SelectCommand="asp_Categories_get" SelectCommandType="StoredProcedure"></asp:SqlDataSource>
        <asp:SqlDataSource ID="getCategory" runat="server" ConnectionString="<%$ ConnectionStrings:devauctionConnectionString %>" SelectCommand="asp_Category_get" SelectCommandType="StoredProcedure">
            <SelectParameters>
                <asp:ControlParameter ControlID="DetailsView1" Name="value" PropertyName="SelectedValue" Type="String" />
            </SelectParameters>
        </asp:SqlDataSource>
    </form>
</body>
</html>
*******   ERROR  MESSAGE IN EDIT MODE  *************
 
Server Error in '/' Application.
--------------------------------------------------------------------------------
 
'ddlDV_Category_Edit' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
 
Exception Details: System.ArgumentOutOfRangeException: 'ddlDV_Category_Edit' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
 
Source Error: 
 
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  
 
Stack Trace: 
 
 
[ArgumentOutOfRangeException: 'ddlDV_Category_Edit' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value]
   System.Web.UI.WebControls.ListControl.PerformDataBinding(IEnumerable dataSource) +1604142
   System.Web.UI.WebControls.ListControl.OnDataBinding(EventArgs e) +107
   System.Web.UI.WebControls.ListControl.PerformSelect() +34
   System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +73
   System.Web.UI.Control.DataBindChildren() +211
   System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +102
   System.Web.UI.Control.DataBind() +15
   System.Web.UI.Control.DataBindChildren() +211
   System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +102
   System.Web.UI.Control.DataBind() +15
   System.Web.UI.Control.DataBindChildren() +211
   System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +102
   System.Web.UI.Control.DataBind() +15
   System.Web.UI.Control.DataBindChildren() +211
   System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +102
   System.Web.UI.WebControls.DetailsView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding) +1536
   System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +57
   System.Web.UI.WebControls.DetailsView.PerformDataBinding(IEnumerable data) +12
   System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +114
   System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +31
   System.Web.UI.WebControls.DataBoundControl.PerformSelect() +142
   System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +73
   System.Web.UI.WebControls.DetailsView.DataBind() +4
   System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +82
   System.Web.UI.WebControls.DetailsView.EnsureDataBound() +184
   System.Web.UI.WebControls.BaseDataBoundControl.OnPreRender(EventArgs e) +22
   System.Web.UI.WebControls.DetailsView.OnPreRender(EventArgs e) +17
   System.Web.UI.Control.PreRenderRecursiveInternal() +80
   System.Web.UI.Control.PreRenderRecursiveInternal() +171
   System.Web.UI.Control.PreRenderRecursiveInternal() +171
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +842

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of carlnorrbom
carlnorrbom
Flag of Sweden image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Salim Fayad
I noticed something on the SqlDataSource "getCategory". The "SelectParameter" is getting from the "DetailsView1" from its property "SelectedValue". Is this returning values or not?? And the returned values do they satisfy what should be in the dropdownlist??
Avatar of NaumLitvin
NaumLitvin

ASKER

Thanks, Carl! I figured it out and my solution was to add a record with the NULL value to the stored procedure that populates the drop-down using UNION in SQL. It would be great if you can post few lines of code explaining your #3 as it seems to be a useful approach...
Hi,

Sure, why not. But consider this a piece of pseudo code as I don't have access to my devbox at the moment and can test it out. I think the easiest way is to simply add a hiddenfield in the markup, reference this in code behind and check whether the returned value is null or empty. If not, assign it as the selected value of the dropdown. You also need to add the dropdown as a control parameter to your update method.

/Carl.
<EditItemTemplate>
    <asp:HiddenField ID="hf_Category_Edit" runat="server" Value='<%# Bind("Category") %>' />
    <asp:DropDownList ID="ddlDV_Category_Edit" runat="server" DataSourceID="getCategory" DataTextField="CategoryName" DataValueField="CategoryName" Style="position: relative">
    </asp:DropDownList>
</EditItemTemplate>
 
<UpdateParameters>
    <asp:Parameter Name="ItemNbr" Type="Int32" />
    <asp:Parameter Name="Year" Type="Int16" />
    <asp:Parameter Name="ItemName" Type="String" />
    <asp:Parameter Name="ImageFilename" Type="String" />
    <asp:Parameter Name="ImageBigFilename" Type="String" />
    <asp:Parameter Name="Description" Type="String" />
    <asp:Parameter Name="Footnote" Type="String" />
    <asp:Parameter Name="Value" Type="Decimal" />
    <asp:Parameter Name="Donor" Type="String" />
    <asp:Parameter Name="DonorAddress" Type="String" />
    <asp:Parameter Name="DonorPhone" Type="String" />
    <asp:Parameter Name="DonorEmail" Type="String" />
    <asp:Parameter Name="DonorWebsite" Type="String" />
    <asp:ControlParameter Name="Category" Type="String" ControlID="ddlDV_Category_Edit" PropertyName="SelectedValue" />
    <asp:Parameter Name="Legacy_PK" Type="Int32" />
    <asp:Parameter Name="CollectorNTID" Type="String" />
</UpdateParameters>
 
Protected Sub DetailsView1_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs) Handles DetailsView1.DataBinding
    If CType(sender, DetailsView).CurrentMode = DetailsViewMode.Edit Then
        Dim hf_Category_Edit As HiddenField = CType(CType(sender, DetailsView).FindControl("hf_Category_Edit"), HiddenField)
        If Not String.IsNullOrEmpty(hf_Category_Edit.Value.ToString()) Then
            CType(CType(sender, DetailsView).FindControl("ddlDV_Category_Edit"), DropDownList).SelectedValue = hf_Category_Edit.Value
        Else
            CType(CType(sender, DetailsView).FindControl("ddlDV_Category_Edit"), DropDownList).SelectedIndex = 0
        End If
    End If
End Sub

Open in new window

Thanks, that code certainly clarifies your suggestion. I should be able to convert it to C# myself.
Hi,

Oops, sorry, didn't realise you needed it in C#..

/Carl.
01.protected void DetailsView1_DataBinding(object sender, System.EventArgs e)
02.{
03.    if (((DetailsView)sender).CurrentMode == DetailsViewMode.Edit) {
04.        HiddenField hf_Category_Edit = (HiddenField)((DetailsView)sender).FindControl("hf_Category_Edit");
05.        if (!string.IsNullOrEmpty(hf_Category_Edit.Value.ToString())) {
06.            ((DropDownList)((DetailsView)sender).FindControl("ddlDV_Category_Edit")).SelectedValue = hf_Category_Edit.Value;
07.        }
08.        else {
09.            ((DropDownList)((DetailsView)sender).FindControl("ddlDV_Category_Edit")).SelectedIndex = 0;
10.        }
11.    }
12.}

Open in new window

that's great, thanks!!