locked
dropdownlist inside a form view RRS feed

  • Question

  • User-180947504 posted

    Dear all

    i'm newbie on Asp.net.. i have a database and some control inside an asp page

    from a table with "select" command I have connected a formview, on editing i want to change some control from textbox to dropdowlist

    I setup a a table with values, create a sql datasource with correct query..

    Ï take the formview control and on edit template put a dropdownlist, this one correctly linked to datasource

    When i run the page i have this error:

    System.InvalidOperationException: 'Data source is an invalid type. It must be either an IListSource, IEnumerable, or IDataSource.'

    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Stampi.aspx.vb" Inherits="Molds.Stampi" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <style type="text/css">
            .auto-style1 {
                width: 396px;
            }
            .auto-style2 {
                width: 97px;
            }
            .auto-style3 {
                font-size: xx-large;
            }
        </style>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <h1 class="auto-style3">Stampi e figure</h1>
            </div>
            <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="CodiceStampo" DataSourceID="SqlDataSource1" EmptyDataText="There are no data records to display.">
                <Columns>
                    <asp:CommandField ShowSelectButton="True" />
                    <asp:BoundField DataField="CodiceStampo" HeaderText="CodiceStampo" ReadOnly="True" SortExpression="CodiceStampo" />
                    <asp:BoundField DataField="Descrizione" HeaderText="Descrizione" SortExpression="Descrizione" />
                    <asp:BoundField DataField="Posizione" HeaderText="Posizione" SortExpression="Posizione" />
                    <asp:BoundField DataField="OrePreviste" HeaderText="OrePreviste" SortExpression="OrePreviste" />
                    <asp:BoundField DataField="OreResidue" HeaderText="OreResidue" SortExpression="OreResidue" />
                    <asp:BoundField DataField="NoteRD" HeaderText="NoteRD" SortExpression="NoteRD" />
                    <asp:BoundField DataField="NotePrd" HeaderText="NotePrd" SortExpression="NotePrd" />
                    <asp:BoundField DataField="Riferimenti" HeaderText="Riferimenti" SortExpression="Riferimenti" />
                    <asp:BoundField DataField="Notecq" HeaderText="Notecq" SortExpression="Notecq" />
                    <asp:BoundField DataField="NoteCpm" HeaderText="NoteCpm" SortExpression="NoteCpm" />
                    <asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status" />
                    <asp:BoundField DataField="DataCambioStatus" HeaderText="DataCambioStatus" SortExpression="DataCambioStatus" />
                </Columns>
            </asp:GridView>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MoldsConnectionString2 %>" SelectCommand="SELECT * FROM [Stampi]">
            </asp:SqlDataSource>
            <h1>Dettaglio Stampo</h1>
            <br />
            <br />
            <table style="width:100%;">
                <tr>
                    <td class="auto-style1">
                        <asp:SqlDataSource ID="SqlDataSource7" runat="server" ConnectionString="<%$ ConnectionStrings:MoldsConnectionString2 %>" DeleteCommand="DELETE FROM [Stampi] WHERE [CodiceStampo] = @CodiceStampo" InsertCommand="INSERT INTO [Stampi] ([CodiceStampo], [Descrizione], [Posizione], [OrePreviste], [OreResidue], [NoteRD], [NotePrd], [Riferimenti], [Notecq], [NoteCpm], [Status], [DataCambioStatus]) VALUES (@CodiceStampo, @Descrizione, @Posizione, @OrePreviste, @OreResidue, @NoteRD, @NotePrd, @Riferimenti, @Notecq, @NoteCpm, @Status, @DataCambioStatus)" SelectCommand="SELECT * FROM [Stampi] WHERE ([CodiceStampo] = @CodiceStampo)" UpdateCommand="UPDATE [Stampi] SET [Descrizione] = @Descrizione, [Posizione] = @Posizione, [OrePreviste] = @OrePreviste, [OreResidue] = @OreResidue, [NoteRD] = @NoteRD, [NotePrd] = @NotePrd, [Riferimenti] = @Riferimenti, [Notecq] = @Notecq, [NoteCpm] = @NoteCpm, [Status] = @Status, [DataCambioStatus] = @DataCambioStatus WHERE [CodiceStampo] = @CodiceStampo">
                            <DeleteParameters>
                                <asp:Parameter Name="CodiceStampo" Type="String" />
                            </DeleteParameters>
                            <InsertParameters>
                                <asp:Parameter Name="CodiceStampo" Type="String" />
                                <asp:Parameter Name="Descrizione" Type="String" />
                                <asp:Parameter Name="Posizione" Type="String" />
                                <asp:Parameter Name="OrePreviste" Type="Decimal" />
                                <asp:Parameter Name="OreResidue" Type="Decimal" />
                                <asp:Parameter Name="NoteRD" Type="String" />
                                <asp:Parameter Name="NotePrd" Type="String" />
                                <asp:Parameter Name="Riferimenti" Type="String" />
                                <asp:Parameter Name="Notecq" Type="String" />
                                <asp:Parameter Name="NoteCpm" Type="String" />
                                <asp:Parameter Name="Status" Type="String" />
                                <asp:Parameter Name="DataCambioStatus" Type="DateTime" />
                            </InsertParameters>
                            <SelectParameters>
                                <asp:ControlParameter ControlID="GridView1" Name="CodiceStampo" PropertyName="SelectedValue" Type="String" />
                            </SelectParameters>
                            <UpdateParameters>
                                <asp:Parameter Name="Descrizione" Type="String" />
                                <asp:Parameter Name="Posizione" Type="String" />
                                <asp:Parameter Name="OrePreviste" Type="Decimal" />
                                <asp:Parameter Name="OreResidue" Type="Decimal" />
                                <asp:Parameter Name="NoteRD" Type="String" />
                                <asp:Parameter Name="NotePrd" Type="String" />
                                <asp:Parameter Name="Riferimenti" Type="String" />
                                <asp:Parameter Name="Notecq" Type="String" />
                                <asp:Parameter Name="NoteCpm" Type="String" />
                                <asp:Parameter Name="Status" Type="String" />
                                <asp:Parameter Name="DataCambioStatus" Type="DateTime" />
                                <asp:Parameter Name="CodiceStampo" Type="String" />
                            </UpdateParameters>
                        </asp:SqlDataSource>
                        <br />
                        <asp:FormView ID="FormView1" runat="server" DataKeyNames="CodiceStampo" DataSourceID="SqlDataSource7">
                            <EditItemTemplate>
                                CodiceStampo:
                                <asp:Label ID="CodiceStampoLabel1" runat="server" Text='<%# Eval("CodiceStampo") %>' />
                                <br />
                                Descrizione:
                                <asp:TextBox ID="DescrizioneTextBox" runat="server" Text='<%# Bind("Descrizione") %>' />
                                <br />
                                Posizione:
                                <asp:TextBox ID="PosizioneTextBox" runat="server" Text='<%# Bind("Posizione") %>' />
                                <br />
                                OrePreviste:
                                <asp:TextBox ID="OrePrevisteTextBox" runat="server" Text='<%# Bind("OrePreviste") %>' />
                                <br />
                                OreResidue:
                                <asp:TextBox ID="OreResidueTextBox" runat="server" Text='<%# Bind("OreResidue") %>' />
                                <br />
                                NoteRD:
                                <asp:TextBox ID="NoteRDTextBox" runat="server" Text='<%# Bind("NoteRD") %>' />
                                <br />
                                NotePrd:
                                <asp:TextBox ID="NotePrdTextBox" runat="server" Text='<%# Bind("NotePrd") %>' />
                                <br />
                                Riferimenti:
                                <asp:TextBox ID="RiferimentiTextBox" runat="server" Text='<%# Bind("Riferimenti") %>' />
                                <br />
                                Notecq:
                                <asp:TextBox ID="NotecqTextBox" runat="server" Text='<%# Bind("Notecq") %>' />
                                <br />
                                NoteCpm:
                                <asp:TextBox ID="NoteCpmTextBox" runat="server" Text='<%# Bind("NoteCpm") %>' />
                                <br />
                                Status:
                                <asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%# Bind("Status") %>'>
                                    <asp:ListItem>Produzione</asp:ListItem>
                                    <asp:ListItem>Magazzino</asp:ListItem>
                                </asp:DropDownList>
                                <br />
                                DataCambioStatus:
                                <asp:TextBox ID="DataCambioStatusTextBox" runat="server" Text='<%# Bind("DataCambioStatus") %>' />
                                <br />
                                <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Update" />
                                &nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
                            </EditItemTemplate>
                            <InsertItemTemplate>
                                CodiceStampo:
                                <asp:TextBox ID="CodiceStampoTextBox" runat="server" Text='<%# Bind("CodiceStampo") %>' />
                                <br />
                                Descrizione:
                                <asp:TextBox ID="DescrizioneTextBox" runat="server" Text='<%# Bind("Descrizione") %>' />
                                <br />
                                Posizione:
                                <asp:TextBox ID="PosizioneTextBox" runat="server" Text='<%# Bind("Posizione") %>' />
                                <br />
                                OrePreviste:
                                <asp:TextBox ID="OrePrevisteTextBox" runat="server" Text='<%# Bind("OrePreviste") %>' />
                                <br />
                                OreResidue:
                                <asp:TextBox ID="OreResidueTextBox" runat="server" Text='<%# Bind("OreResidue") %>' />
                                <br />
                                NoteRD:
                                <asp:TextBox ID="NoteRDTextBox" runat="server" Text='<%# Bind("NoteRD") %>' />
                                <br />
                                NotePrd:
                                <asp:TextBox ID="NotePrdTextBox" runat="server" Text='<%# Bind("NotePrd") %>' />
                                <br />
                                Riferimenti:
                                <asp:TextBox ID="RiferimentiTextBox" runat="server" Text='<%# Bind("Riferimenti") %>' />
                                <br />
                                Notecq:
                                <asp:TextBox ID="NotecqTextBox" runat="server" Text='<%# Bind("Notecq") %>' />
                                <br />
                                NoteCpm:
                                <asp:TextBox ID="NoteCpmTextBox" runat="server" Text='<%# Bind("NoteCpm") %>' />
                                <br />
                                Status:
                                <asp:TextBox ID="StatusTextBox" runat="server" Text='<%# Bind("Status") %>' />
                                <br />
                                DataCambioStatus:
                                <asp:TextBox ID="DataCambioStatusTextBox" runat="server" Text='<%# Bind("DataCambioStatus") %>' />
                                <br />
                                <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert" Text="Insert" />
                                &nbsp;<asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
                            </InsertItemTemplate>
                            <ItemTemplate>
                                CodiceStampo:
                                <asp:Label ID="CodiceStampoLabel" runat="server" Text='<%# Eval("CodiceStampo") %>' />
                                <br />
                                Descrizione:
                                <asp:Label ID="DescrizioneLabel" runat="server" Text='<%# Bind("Descrizione") %>' />
                                <br />
                                Posizione:
                                <asp:Label ID="PosizioneLabel" runat="server" Text='<%# Bind("Posizione") %>' />
                                <br />
                                OrePreviste:
                                <asp:Label ID="OrePrevisteLabel" runat="server" Text='<%# Bind("OrePreviste") %>' />
                                <br />
                                OreResidue:
                                <asp:Label ID="OreResidueLabel" runat="server" Text='<%# Bind("OreResidue") %>' />
                                <br />
                                NoteRD:
                                <asp:Label ID="NoteRDLabel" runat="server" Text='<%# Bind("NoteRD") %>' />
                                <br />
                                NotePrd:
                                <asp:Label ID="NotePrdLabel" runat="server" Text='<%# Bind("NotePrd") %>' />
                                <br />
                                Riferimenti:
                                <asp:Label ID="RiferimentiLabel" runat="server" Text='<%# Bind("Riferimenti") %>' />
                                <br />
                                Notecq:
                                <asp:Label ID="NotecqLabel" runat="server" Text='<%# Bind("Notecq") %>' />
                                <br />
                                NoteCpm:
                                <asp:Label ID="NoteCpmLabel" runat="server" Text='<%# Bind("NoteCpm") %>' />
                                <br />
                                Status:
                                <asp:Label ID="StatusLabel" runat="server" Text='<%# Bind("Status") %>' />
                                <asp:DropDownList ID="DropDownList2" runat="server" DataSource='<%# Eval("Status") %>' DataSourceID="status" DataTextField="Stato" DataValueField="Stato" SelectedValue='<%# Eval("Status") %>'>
                                </asp:DropDownList>
                                <br />
                                DataCambioStatus:
                                <asp:Label ID="DataCambioStatusLabel" runat="server" Text='<%# Bind("DataCambioStatus") %>' />
                                <br />
                                <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit" />
                                &nbsp;<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete" />
                                &nbsp;<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" CommandName="New" Text="New" />
                            </ItemTemplate>
                        </asp:FormView>
                        <asp:SqlDataSource ID="status" runat="server" ConnectionString="<%$ ConnectionStrings:MoldsConnectionString2 %>" SelectCommand="SELECT  [Stato] FROM [Stati]"></asp:SqlDataSource>
                    </td>
                    <td class="auto-style2">
                        &nbsp;</td>
                    <td rowspan="3">
                        &nbsp;</td>
                </tr>
                <tr>
                    <td class="auto-style1">
                        <h1>Elenco Figure Associate</h1>
                    </td>
                    <td class="auto-style2">
                        &nbsp;</td>
                </tr>
                <tr>
                    <td class="auto-style1">
                        <asp:GridView ID="GridView5" runat="server">
                            <Columns>
                                <asp:CommandField SelectText="Delete" ShowSelectButton="True" />
                            </Columns>
                        </asp:GridView>
                        <asp:SqlDataSource ID="SqlDataSource5" runat="server" ConnectionString="<%$ ConnectionStrings:MoldsConnectionString2 %>" SelectCommand="SELECT * FROM [Figure] WHERE ([Codicefigura] in @Codicefigura2)">
                            <SelectParameters>
                                <asp:SessionParameter Name="Codicefigura2" SessionField="figure" Type="String" />
                            </SelectParameters>
                        </asp:SqlDataSource>
                        <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
                        <br />
                    </td>
                    <td class="auto-style2">
                        <br />
                    </td>
                </tr>
            </table>
            <h1>Elenco Figure Associabili</h1>
            <br />
    &nbsp;<asp:Label ID="Label2" runat="server" Text="Label" Visible="False"></asp:Label>
    &nbsp;<asp:GridView ID="GridView3" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="Codicefigura" DataSourceID="SqlDataSource4" Visible="False">
                <Columns>
                    <asp:CommandField ShowSelectButton="True" />
                    <asp:BoundField DataField="Codicefigura" HeaderText="Codicefigura" ReadOnly="True" SortExpression="Codicefigura" />
                    <asp:BoundField DataField="Descrizione" HeaderText="Descrizione" SortExpression="Descrizione" />
                    <asp:BoundField DataField="OrePreviste" HeaderText="OrePreviste" SortExpression="OrePreviste" />
                    <asp:BoundField DataField="OreResidue" HeaderText="OreResidue" SortExpression="OreResidue" />
                    <asp:BoundField DataField="Note" HeaderText="Note" SortExpression="Note" />
                    <asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status" />
                </Columns>
            </asp:GridView>
            <asp:SqlDataSource ID="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:MoldsConnectionString2 %>" SelectCommand="SELECT Codicefigura, Descrizione, OrePreviste, OreResidue, Note, Status FROM Figure WHERE (Status = N'Magazzino')"></asp:SqlDataSource>
            <asp:SqlDataSource ID="SqlDataSource6" runat="server"></asp:SqlDataSource>
            <asp:Label ID="Label4" runat="server" Text="Label"></asp:Label>
        </form>
    </body>
    </html>
    
    Imports System.Data.SqlClient
    
    Public Class Stampi
        Inherits System.Web.UI.Page
    
        Dim Strstampo As String
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
        End Sub
    
    
    
    
    
        Protected Sub GridView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles GridView1.SelectedIndexChanged
            Dim row As GridViewRow
            row = GridView1.SelectedRow
    
            Session("stampo") = row.Cells(1).Text
    
            SqlDataSource5.SelectParameters.Clear()
            SqlDataSource5.SelectCommand = "SELECT * FROM [Figure] where codiceFigura IN (Select Codicefigura from [RelazioniPF] where CodiceStampo='" & Session("stampo") & "')"
            GridView5.DataSource = SqlDataSource5
            GridView5.DataBind()
            Label2.Text = "Figure Associabili"
            GridView3.Visible = True
            GridView5.DataBind()
            GridView3.DataBind()
            'FormView2.DataBind()
        End Sub
    
        Protected Sub GridView5_SelectedIndexChanged(sender As Object, e As EventArgs) Handles GridView5.SelectedIndexChanged
            Dim row As GridViewRow
            row = GridView5.SelectedRow
            Session("figura") = row.Cells(1).Text
            Label4.Text = Session("stampo")
            'Query eliminazione
    
            Dim FetchSubRows As New SqlConnection("Data Source=CHMOLD01;Initial Catalog=Molds;Persist Security Info=True;User ID=mold;Password=M0lds01") ' << visible change is this
            FetchSubRows.Open()
            Dim FetchQuery As String = "delete FROM [RelazioniPF] where CodiceFigura='" & Trim(Session("figura")) & "' And CodiceStampo='" & Trim(Session("stampo")) & "'"
            Label3.Text = FetchQuery
            Dim ExecuteCommand As New SqlCommand(FetchQuery, FetchSubRows)
            ExecuteCommand.ExecuteNonQuery()
    
    
    
            Label3.Text = "done"
            FetchQuery = "Update [Figure] SET Status = 'Magazzino' where CodiceFigura='" & Session("figura") & "'"
            Label4.Text = FetchQuery
            Dim ExecuteCommand2 As New SqlCommand(FetchQuery, FetchSubRows)
            ExecuteCommand2.ExecuteNonQuery()
            Label3.Text = Label3.Text & " aggiornato"
            FetchSubRows.Close()
            GridView5.DataBind()
            GridView5.Visible = True
    
    
            SqlDataSource5.SelectCommand = "SELECT * FROM [Figure] where codiceFigura IN (Select Codicefigura from [RelazioniPF] where CodiceStampo='" & Session("stampo") & "')"
            GridView5.DataSource = SqlDataSource5
            GridView5.DataBind()
    
    
            GridView3.DataBind()
            FetchSubRows.Close()
        End Sub
    
        Protected Sub GridView3_SelectedIndexChanged(sender As Object, e As EventArgs) Handles GridView3.SelectedIndexChanged
            Dim row As GridViewRow
            row = GridView3.SelectedRow
    
            Dim CodiceFigura, CodiceStampo, Status As String
    
            CodiceStampo = Session("stampo")
            CodiceFigura = row.Cells(1).Text
            Status = "Produzione"
    
            'aggiungi a RelazioniPF e cambia status
            Dim FetchSubRows As New SqlConnection("Data Source=CHMOLD01;Initial Catalog=Molds;Persist Security Info=True;User ID=mold;Password=M0lds01") ' << visible change is this
            FetchSubRows.Open()
            Dim FetchQuery As String = "INSERT INTO [RelazioniPF] (CodiceFigura,CodiceStampo,Stato) values('" & CodiceFigura & "','" & CodiceStampo & "','" & Status & "')"
            Label4.Text = FetchQuery
            Dim ExecuteCommand As New SqlCommand(FetchQuery, FetchSubRows)
            ExecuteCommand.ExecuteNonQuery()
    
            'aggiorna stato in figure
            FetchQuery = "Update [Figure] SET Status = 'Produzione' where CodiceFigura='" & CodiceFigura & "'"
            Label4.Text = FetchQuery
            Dim ExecuteCommand2 As New SqlCommand(FetchQuery, FetchSubRows)
            ExecuteCommand2.ExecuteNonQuery()
            GridView5.DataBind()
            GridView3.DataBind()
            FetchSubRows.Close()
            SqlDataSource5.SelectCommand = "SELECT * FROM [Figure] where codiceFigura IN (Select Codicefigura from [RelazioniPF] where CodiceStampo='" & Session("stampo") & "')"
            GridView5.DataSource = SqlDataSource5
            GridView5.DataBind()
        End Sub
    
    
    
        Private Sub FormView1_DataBound(sender As Object, e As EventArgs) Handles FormView1.DataBound
    
        End Sub
    End Class

    Friday, August 16, 2019 9:51 AM

All replies

  • User288213138 posted

    Hi dibia,

    The reason for this error is that the Datasource type is not a collection.

    dibia

    <asp:DropDownList ID="DropDownList2" runat="server" DataSource='<%# Eval("Status") %>' DataSourceID="status" DataTextField="Stato" DataValueField="Stato" SelectedValue='<%# Eval("Status") %>'>
                                </asp:DropDownList>


    In you Dropdownlist2 control, The datasource bound value is the "Status" field in the database. it should be a collection.

    You can try to delete DataSource='<%# Eval("Status") %>'. and you even set the DataSourceID, so there is no need to set the Datasource.

    note: SelectedValue='<%# Eval("Status") %>'> is not correct. The value in SelectValue must be more than one.


    Best regards,
    Sam

    Monday, August 19, 2019 7:29 AM
  • User-1716253493 posted

    Remove

    DataSource='<%# Eval("Status") %>'

    You have correct DataSourceId

    DataSourceID="status"

    Thursday, August 22, 2019 7:00 AM