I want to insert multiple row of data from listview to database

I have encountered an error stating:

No mapping exists from object type System.Web.UI.WebControls.Literal to a known managed provider native type.

I have this aspx

<asp:ListView ID="lvPODetails" runat="server">
    <ItemTemplate>
        <tr>
            <td>
                <asp:Literal ID="ltRefNo" runat="server" Text='<%# Eval("PODetailNo") %>' Visible="false" />
                <%# Eval("ProductName")%>
            </td>
            <td>
                <%# Eval("Price", "{0: #,###.00}") %>
            </td>
            <td><%# Eval("DesiredQuantity") %></td>
            <td><asp:TextBox ID="txtQuantity" runat="server" type="number" Text='<%# Eval("DesiredQuantity") %>' class="form-control" /></td>
            <td><%# Eval("POAmount", "{0: #,###.00}") %></td>
            <td><%# Eval("POAmount", "{0: #,###.00}") %></td>
            <td>

            </td>
        </tr>

    </ItemTemplate>
    <EmptyDataTemplate>
        <tr>
            <td colspan="4"><h2 class="text-center">No records found.</h2></td>
        </tr>
    </EmptyDataTemplate>
</asp:ListView>

This is my .cs for the insert

 protected void btnAdd_Click(object sender, EventArgs e)
{

    foreach (ListViewItem item in lvPODetails.Items)
    {
        TextBox quantity = (TextBox)item.FindControl("txtQuantity");
        Literal ltr = (Literal)item.FindControl("ltRefNo");


        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "INSERT INTO Inventory VALUES (@ProductID, @Quantity)";
        cmd.Parameters.AddWithValue("@ProductID", ltr);
        cmd.Parameters.AddWithValue("@Quantity", quantity);
        cmd.ExecuteNonQuery();
        con.Close();

    }

}

I think the foreach loop is wrong, but I can't say what is wrong with it.

Related posts

Recent Viewed