none
Using DetailsView and dropdownlist to insert data to a Sqlserver2000 table. RRS feed

  • Question

  • Can anyone help me ?

     

    I want to insert data to a Sqlserver2000 datatable. This works fine using detailsview. I want to add a Firmname into this row, but do not want to write the Firmname. I want to select the firmname from a firmtable. I am able to select the firm from a dropdown list fra the firmtable, but how to write the selection in the dropdownlist into the right column in the datatable??

     

    Any suggestions ?

    Friday, July 6, 2007 1:53 PM

Answers

  • Finally

     I found the solution :

     

    <Fields>

    <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True"

    SortExpression="ID" />

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

    <InsertItemTemplate>

    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1"

    DataTextField="Loypenavn" DataValueField="LoypeID" SelectedValue='<%# Bind("LoypeID") %>'>

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

    SelectCommand="SELECT [LoypeID], [Loypenavn] FROM [Loype]"></asp:SqlDataSource>

    </InsertItemTemplate>

    <ItemTemplate>

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

    </ItemTemplate>

    </asp:TemplateField>

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

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

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

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

    </Fields>

     

     

     

    The solution was retrieved from : http://www.asp.net/learn/dataaccess/tutorial20cs.aspx?tabid=63

     

    Monday, July 16, 2007 1:49 PM

All replies

  • First off you will need to have your details view and drop down list bound to two datasources... That is, your drop down list is bound to it's own data souce (the lookup table).  With that out of the way it is easy.

     

    In this senario I assume your details view is for an employee.  Your DropDownList will contain a list of departments and you will select one from the list and have the selected department Id (from the department table driving the dropdown) set as the value of the employee's DepartmentId field

     

    Code Snippet

    <asp:DropDownList ID="lstDepartments" runat="server"

         Font-Size="80%"

         DataSourceID="odsDepartments"

         DataTextField="Name"

         DataValueField="Id"

         AppendDataBoundItems="true"

         SelectedValue='<%# Bind("DepartmentId") %>' >

     

             <asp:ListItem Value="0" Text="-- select a department --" Selected="True" />

     

    </asp:DropDownList>

     

     

    Friday, July 6, 2007 7:17 PM
  • I still cant get the selected dropdownlist-value (løypeID) to be inserted into LøypeID column in my detailsview-table.

     

    Here's my code :

     

    <%@ Page Language="C#" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server">

    </script>

    <html xmlns="http://www.w3.org/1999/xhtml" >

    <head runat="server">

    <title>Untitled Page</title>

    </head>

    <body>

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

    <div>

    test input<br />

    <br />

    <br />

    <asp:DropDownList ID="DropDownList1" runat="server"

    AppendDataBoundItems="True"

    AutoPostBack="true"

    DataSourceID="SqlDataSource1"

    DataTextField="Løypenavn"

    DataValueField="LøypeID">

    <asp:ListItem Value="0" Text="-- velg en l&#248;ype --" Selected="True" />

    </asp:DropDownList>

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

    SelectCommand="SELECT [LøypeID], [Løypenavn] FROM [Løype]"></asp:SqlDataSource>

    <br />

    <br />

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

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

    <Fields>

    <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True"

    SortExpression="ID" />

    <asp:BoundField DataField="L&#248;ypeID" HeaderText="L&#248;ypeID" SortExpression="L&#248;ypeID" />

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

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

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

    <asp:CommandField ShowInsertButton="True" />

    </Fields>

    </asp:DetailsView>

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

    DeleteCommand="DELETE FROM [jogg] WHERE [ID] = @ID" InsertCommand="INSERT INTO [jogg] ([LøypeID], [navn], [dato], [tid]) VALUES (@LøypeID, @navn, @dato, @tid)"

    SelectCommand="SELECT [ID], [LøypeID], [navn], [dato], [tid] FROM [jogg]" UpdateCommand="UPDATE [jogg] SET [LøypeID] = @LøypeID, [navn] = @navn, [dato] = @dato, [tid] = @tid WHERE [ID] = @ID">

    <DeleteParameters>

    <asp:Parameter Name="ID" Type="Int32" />

    </DeleteParameters>

    <UpdateParameters>

    <asp:Parameter Name="L&#248;ypeID" Type="Int32" />

    <asp:Parameter Name="navn" Type="String" />

    <asp:Parameter Name="dato" Type="DateTime" />

    <asp:Parameter Name="tid" Type="String" />

    <asp:Parameter Name="ID" Type="Int32" />

    </UpdateParameters>

    <InsertParameters>

    <asp:Parameter Name="L&#248;ypeID" Type="Int32" />

    <asp:Parameter Name="navn" Type="String" />

    <asp:Parameter Name="dato" Type="DateTime" />

    <asp:Parameter Name="tid" Type="String" />

    </InsertParameters>

    </asp:SqlDataSource>

    </div>

    </form>

    </body>

    </html>

    Sunday, July 8, 2007 7:12 PM
  • I think you want the dropdownlist inside the details view

     

    replace:

    <asp:BoundField DataField="L&#248;ypeID" HeaderText="L&#248;ypeID" SortExpression="L&#248;ypeID" />

     

    with:

    <asp:DropDownList ID="DropDownList1" runat="server"

    AppendDataBoundItems="True"

    AutoPostBack="true"

    DataSourceID="SqlDataSource1"

    DataTextField="Løypenavn"

    DataValueField="LøypeID">

    <asp:ListItem Value="0" Text="-- velg en l&#248;ype --" Selected="True" />

    </asp:DropDownList>

    Monday, July 9, 2007 2:30 PM
  • Yes I want to use dropdownlist inside details view.

     

    I replaced as you suggested :

    <%@ Page Language="C#" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server">

    </script>

    <html xmlns="http://www.w3.org/1999/xhtml" >

    <head runat="server">

    <title>Untitled Page</title>

    </head>

    <body>

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

    <div>

    test input<br />

    <br />

    <br />

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

    SelectCommand="SELECT [LøypeID], [Løypenavn] FROM [Løype]"></asp:SqlDataSource>

    <br />

    <br />

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

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

    <Fields>

    <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True"

    SortExpression="ID" />

    <asp:DropDownList ID="DropDownList1" runat="server"

    AppendDataBoundItems="True"

    AutoPostBack="true"

    DataSourceID="SqlDataSource1"

    DataTextField="Løypenavn"

    DataValueField="LøypeID">

    <asp:ListItem Value="0" Text="-- velg en l&#248;ype --" Selected="True" />

    </asp:DropDownList>

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

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

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

    <asp:CommandField ShowInsertButton="True" />

    </Fields>

    </asp:DetailsView>

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

    DeleteCommand="DELETE FROM [jogg] WHERE [ID] = @ID" InsertCommand="INSERT INTO [jogg] ([LøypeID], [navn], [dato], [tid]) VALUES (@LøypeID, @navn, @dato, @tid)"

    SelectCommand="SELECT [ID], [LøypeID], [navn], [dato], [tid] FROM [jogg]" UpdateCommand="UPDATE [jogg] SET [LøypeID] = @LøypeID, [navn] = @navn, [dato] = @dato, [tid] = @tid WHERE [ID] = @ID">

    <DeleteParameters>

    <asp:Parameter Name="ID" Type="Int32" />

    </DeleteParameters>

    <UpdateParameters>

    <asp:Parameter Name="L&#248;ypeID" Type="Int32" />

    <asp:Parameter Name="navn" Type="String" />

    <asp:Parameter Name="dato" Type="DateTime" />

    <asp:Parameter Name="tid" Type="String" />

    <asp:Parameter Name="ID" Type="Int32" />

    </UpdateParameters>

    <InsertParameters>

    <asp:Parameter Name="L&#248;ypeID" Type="Int32" />

    <asp:Parameter Name="navn" Type="String" />

    <asp:Parameter Name="dato" Type="DateTime" />

    <asp:Parameter Name="tid" Type="String" />

    </InsertParameters>

    </asp:SqlDataSource>

    </div>

    </form>

    </body>

    </html>

     

     

    But I got an error :

     Error 2 System.Web.UI.WebControls.DataControlFieldCollection must have items of type 'System.Web.UI.WebControls.DataControlField'. 'asp:ListItem' is of type 'System.Web.UI.WebControls.ListItem'.

    It would help a lot if I can get this thing to work :-) Please help :-)
    Thursday, July 12, 2007 12:38 PM
  • not sure what that errors is...

     

    try replacing

    <asp:DropDownList ID="DropDownList1" runat="server"

        AppendDataBoundItems="True"

        AutoPostBack="true"

        DataSourceID="SqlDataSource1"

        DataTextField="Løypenavn"

        DataValueField="LøypeID">

        <asp:ListItem Value="0" Text="-- velg en l&#248;ype --" Selected="True" />

    </asp:DropDownList>

     

    With

    <asp:DropDownList ID="DropDownList1" runat="server"

        AppendDataBoundItems="True"

        AutoPostBack="true"

        DataSourceID="SqlDataSource1"

        DataTextField="Løypenavn"

        DataValueField="LøypeID"

        SelectedValue='<%# Bind("L&#248;ypeID") %>' >

     

        <asp:ListItem Value="0" Text="-- velg en l&#248;ype --" Selected="True" />

     

    </asp:DropDownList>

     

    Note, that "L&#248;ypeID" looks as if that is URL encoded and is probably not the actual name of your field from the details view

    Friday, July 13, 2007 7:03 PM
  • It seems like I cant have a dropdownlist item inside a apsBig Smileetailsview <fields> section.
    Monday, July 16, 2007 7:49 AM
  • Finally

     I found the solution :

     

    <Fields>

    <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True"

    SortExpression="ID" />

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

    <InsertItemTemplate>

    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1"

    DataTextField="Loypenavn" DataValueField="LoypeID" SelectedValue='<%# Bind("LoypeID") %>'>

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

    SelectCommand="SELECT [LoypeID], [Loypenavn] FROM [Loype]"></asp:SqlDataSource>

    </InsertItemTemplate>

    <ItemTemplate>

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

    </ItemTemplate>

    </asp:TemplateField>

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

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

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

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

    </Fields>

     

     

     

    The solution was retrieved from : http://www.asp.net/learn/dataaccess/tutorial20cs.aspx?tabid=63

     

    Monday, July 16, 2007 1:49 PM