View Single Post
  #1 (permalink)  
Old November 30th, 2005, 12:41 AM
steve35719 steve35719 is offline
Authorized User
Join Date: Nov 2005
Location: , , .
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default Saving DropDownList selection to a database

What am I missing? I can't figure it out. Granted, I'm a complete newb, but I've been trying for weeks! While in "template mode", removed the default textbox in the DetailsView EditItemTemplate and InsertItemTemplate and replaced with a dropdownlist control. When I place SelectedValue= '<%# Bind("Color") %>' in the DropDownList tag I get an error at runtime:

"'DropDownList2' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value "

If I leave out "SelectedValue='<%# Bind("Color") %>" from the tag, the selection gets lost and doesn't get saved to the database. What's going on and how do I fix it? All I want to do is provide a form and save it to a database. Seems like it should be straightfoward...

Thanks for the much needed help,

Here's my code:

 <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="" >

<head runat="server">

<title>Untitled Page</title>



<form id="form1" runat="server">


<br />


<asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True" AutoGenerateRows="False"

DataSourceID="SqlDataSource1" Height="50px" Width="125px">


<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />

<asp:TemplateField HeaderText="Color" SortExpression="Color">


<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" SelectedValue='<%# Bind("Color") %>'>







<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" SelectedValue='<%# Bind("Color") %>'>







<asp:Label ID="Label1" runat="server" Text='<%# Bind("Color") %>'></asp:Label>



<asp:CommandField ShowEditButton="True" ShowInsertButton="True" />



<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"

SelectCommand="SELECT * FROM [Color]"

InsertCommand="INSERT INTO Color([Name], [Color]) VALUES (@Name, @Color)"

UpdateCommand="UPDATE Color

SET Name =@Name,

Color =@Color

WHERE [Name]=@Name">

<UpdateParameters><asp:Parameter Name="Name" Type="string" /><asp:Parameter Name="Color" Type="string" /></UpdateParameters>

<InsertParameters><asp:Parameter Name="Name" Type="string" /><asp:Parameter Name="Color" Type="string" /></InsertParameters>






What about removing "SelecedValue" from the DropDownList tag and replacing it with an OnSelectedIndexChanged function. Can't figure out how to put together the code behind to make it work. Don't have a clue...

    Protected Sub MyMethod(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim DropDownList1 As New DropDownList
        DropDownList1 = CType(Me.DetailsView1.FindControl("DropDownList1") , DropDownList)

        'Dim DropDownList1 As DropDownList
        'DropDownList1 = CType(Me.DetailsView1.FindControl("DropDownList1") , DropDownList)

        'Dim selectedValue As String = CType(DetailsView1.FindControl("DropDownList1"), DropDownList).SelectedValue
    End Sub

Reply With Quote