开发者

ASP.NET FormView: "Object of type 'System.Int32' cannot be converted to type 'System.String"

I have a problem with my FromView. I would like to show some data from a Database Table in my FormView. But some data is from the tupe Int32, while this data should be in a TextBox, a string.

How do you convert these Int32's.

FormView and my 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>

My Code behind

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];开发者_高级运维
}

My Data Acces Layer

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"];
}

Thanks a lot.


You set parameter "id" 's value with type of string. But tblOrders.ID is type of int.

Try this:

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

or even this (will work little bit more fast and safe than previous code):

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

Also it's better do next:

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

instead of using DataSet


What about do necessary casts before fill dataset ?

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();  
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜