ASP.NET FormView:“类型'System.Int32'的对象无法转换为'System.String'类型”

时间:2010-05-21 14:54:25

标签: c# .net asp.net

我的FromView出了问题。 我想在FormView中显示数据库表中的一些数据。但有些数据来自tupe Int32,而这些数据应该是TextBox,一个字符串。

如何转换这些Int32。

FormView和我的ObjectDataSource

<asp:FormView ID="fvDetailOrder" runat="server">
 <ItemTemplate>
        Aantal:<br />
        <asp:Label CssClass="txtBox" ID="Label15" runat="server" Text='<%# Eval("COUNT") %>' /><br />
        Prijs:<br />
        <asp:Label CssClass="txtBox" ID="Label16" runat="server" Text='<%# Eval("PRICE") %>' /><br />
        Korting:<br />
        <asp:Label CssClass="txtBox" ID="Label17" runat="server" Text='' /><br />
        Totaal:<br />
        <asp:Label CssClass="txtBox" ID="Label18" runat="server" Text='<%# Eval("AMOUNT") %>' /><br />
        Betaald:<br />
        <asp:Label CssClass="txtBox" ID="Label19" runat="server" Text='<%# Eval("PAID") %>' /><br />
        Datum betaling:<br />
        <asp:Label CssClass="txtBox" ID="Label20" runat="server" Text='<%# Eval("PDATE") %>' /><br />
    </ItemTemplate>
</asp:FormView>

<asp:ObjectDataSource ID="objdsOrderID" runat="server" 
    OnSelecting="objdsOrderID_Selecting" SelectMethod="getOrdersByID" 
    TypeName="DAL.OrdersDAL">
    <SelectParameters>
        <asp:Parameter Name="id" Type="Int32" />
    </SelectParameters>
</asp:ObjectDataSource>

我的代码背后

protected void gvOrdersAdmin_SelectedIndexChanged(object sender, EventArgs e)
{
    fvDetailOrder.DataSource = objdsOrderID;
    fvDetailOrder.DataBind(); //  <-- HERE I GET THE ERROR
}

protected void objdsOrderID_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
    e.InputParameters["id"] = gvOrdersAdmin.DataKeys[gvOrdersAdmin.SelectedRow.RowIndex].Values[0];
}

我的数据访问层

public static DataTable getOrdersByID(string id)
{
    string sql = "SELECT 'AUTHOR' = tblAuthors.FIRSTNAME + ' ' + tblAuthors.LASTNAME, tblBooks.*, tblGenres.*, tblLanguages.*, tblOrders.* FROM tblAuthors INNER JOIN tblBooks ON tblAuthors.AUTHOR_ID = tblBooks.AUTHOR_ID INNER JOIN tblGenres ON tblBooks.GENRE_ID = tblGenres.GENRE_ID INNER JOIN tblLanguages ON tblBooks.LANG_ID = tblLanguages.LANG_ID INNER JOIN tblOrders ON tblBooks.BOOK_ID = tblOrders.BOOK_ID"
        + " WHERE tblOrders.ID = @id;";

    SqlDataAdapter da = new SqlDataAdapter(sql, GetConnectionString());

    da.SelectCommand.Parameters["id"].Value = id;

    DataSet ds = new DataSet();
    da.Fill(ds, "Orders");

    return ds.Tables["Orders"];
}

非常感谢。

2 个答案:

答案 0 :(得分:5)

您可以使用string类型设置参数“id”的值。但是tblOrders.ID是int的类型。

试试这个:

da.SelectCommand.Parameters["id"].Value = Int32.Parse(id);

甚至这个(比以前的代码更快,更安全):

int i;
if (Int32.TryParse(id, out i))
{
   da.SelectCommand.Parameters["id"].Value = i;
}
else
{
   throw new ArgumentException("id");
}

下一步做得更好:

var table = CreateDataTable();
new SqlDataAdapter(command).Fill(table);
return table;

而不是使用DataSet

答案 1 :(得分:1)

在填充数据集之前做必要的演员表怎么样?

protected void gvOrdersAdmin_SelectedIndexChanged(object sender, EventArgs e)
{
    T objdsOrderIDAUX = default(T);
    foreach(var item in objdsOrderID)
    {
         objdsOrderIDAUX.propertyBLA = item.propertyBLA.ToString();
         objdsOrderIDAUX.propertyBLO = item.propertyBLO.ToString();
         objdsOrderIDAUX.propertyBLUM = item.propertyBLUM.ToString();
    }
    fvDetailOrder.DataSource = objdsOrderIDAUX;

    HERE I THINK YOU WILL NOT GET THE ERROR -->  fvDetailOrder.DataBind();  
}