C# Foreach语句

C# Foreach语句,c#,asp.net,C#,Asp.net,我想将多行数据从listview插入数据库 我遇到一个错误,说明: 不存在从对象类型System.Web.UI.WebControl.Literal到已知托管提供程序本机类型的映射 我有这个aspx <asp:ListView ID="lvPODetails" runat="server"> <ItemTemplate> <tr> <td> <asp:Litera

我想将多行数据从listview插入数据库

我遇到一个错误,说明:

不存在从对象类型System.Web.UI.WebControl.Literal到已知托管提供程序本机类型的映射

我有这个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>

我认为foreach循环是错误的,但我不能说它有什么问题。

ltr.Text

 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.Text);
        cmd.Parameters.AddWithValue("@Quantity", quantity.Text);
        cmd.ExecuteNonQuery();
        con.Close();

    }

}

希望您的问题得到解决。

ltr.Text

 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.Text);
        cmd.Parameters.AddWithValue("@Quantity", quantity.Text);
        cmd.ExecuteNonQuery();
        con.Close();

    }

}

希望您的问题得到解决。

ltr.Text

 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.Text);
        cmd.Parameters.AddWithValue("@Quantity", quantity.Text);
        cmd.ExecuteNonQuery();
        con.Close();

    }

}

希望您的问题得到解决。

ltr.Text

 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.Text);
        cmd.Parameters.AddWithValue("@Quantity", quantity.Text);
        cmd.ExecuteNonQuery();
        con.Close();

    }

}

希望您的问题得到解决。

您应该对文本和文本框使用
.Text

cmd.Parameters.AddWithValue("@ProductID", ltr.Text);
cmd.Parameters.AddWithValue("@Quantity", quantity.Text);

您应该对文本和文本框使用
.Text

cmd.Parameters.AddWithValue("@ProductID", ltr.Text);
cmd.Parameters.AddWithValue("@Quantity", quantity.Text);

您应该对文本和文本框使用
.Text

cmd.Parameters.AddWithValue("@ProductID", ltr.Text);
cmd.Parameters.AddWithValue("@Quantity", quantity.Text);

您应该对文本和文本框使用
.Text

cmd.Parameters.AddWithValue("@ProductID", ltr.Text);
cmd.Parameters.AddWithValue("@Quantity", quantity.Text);

你的意思是仅仅
cmd.Parameters.AddWithValue(“@ProductID”,ltr.Text)
(注意末尾的
.Text
)?您的意思是仅仅
cmd.Parameters.AddWithValue(“@ProductID”,ltr.Text”)
(注意末尾的
.Text
)?您的意思是仅仅
cmd.Parameters.AddWithValue(“@ProductID”,ltr.Text”)
(注意末尾的
.Text
)?您的意思是仅仅
cmd.Parameters.AddWithValue(“@ProductID”,ltr.Text”)(注意结尾处的
.Text
)@Downvoter-请发表评论?这个答案怎么了?谢谢你的帮助lot@Downvoter-请评论?这个答案怎么了?谢谢你的帮助lot@Downvoter-请评论?这个答案怎么了?谢谢你的帮助lot@Downvoter-请评论?这个答案怎么了?非常感谢你的帮助