[RESOLVED]ListView error on update: Object must implement IConvertible

I am a beginner in asp.net. I have listView where I try to update SQL Server data by means of a stored procedure. I use VS2010 and a local SQL Server 2008 R2. There is no code behind (no C# code).

The update from listview works perfectly when I make the update with traditional update statement from ListView:   UpdateCommand="UPDATE Hour SET TheHours = @TheHours WHERE (TheHoursID = @TheHoursID)"

But when I try to do the same thing (update same fields) with stored procedure I get the following error:"Object must implement IConvertible". The stored procedure is tested and does the job perfectly when tested on SQL Server.This error appears when I introduce
EditItemTemplate for one or more parameters  in html asp code in VS2010.   I miss something basic here. I am very confused by the fact that the update works perfectly in one case, but not with stored procedure.  What could be wrong?

I have tried to change datatype in SQL Server table from decimal(18,4) to float, no effect.

 

Regards

 

<asp:SqlDataSource ID="SqlDataTestStoored" runat="server"
            ConnectionString="<%$ ConnectionStrings:HoursConnectionString %>"
            SelectCommand="Hours_testSelect" SelectCommandType="StoredProcedure"
            UpdateCommand="Hours_testUpdate" UpdateCommandType="StoredProcedure"
            ondatabinding="ListView1_SelectedIndexChanged">
            <UpdateParameters>
                <asp:ControlParameter ControlID="ListView1" Name="TheHours"
                    PropertyName="EditItemTemplate" Type="Decimal" DefaultValue="5"  />
                <asp:ControlParameter ControlID="ListView1" Name="TheHoursID"
                    PropertyName="EditItemTemplate" Type="Int32" DefaultValue="" />
                <asp:ControlParameter ControlID="ListView1" Name="ActivityID"
                    PropertyName="EditItemTemplate" Type="Int32"  />
            </UpdateParameters>
        </asp:SqlDataSource>

Here is stored procedure:

ALTER

PROCEDURE [dbo].[Hours_testUpdate] 

– Add the parameters for the stored procedure here

@TheHours

as
decimal(18,4),
@TheHoursID
int,
@ActivityID
int

AS

BEGIN

Update
Hour

Set TheHours
= @TheHours

where TheHoursID
= @TheHoursID

END

Use parameter instead of ControlParameter. Like : <asp:Parameter Name="TheHours" Type="Decimal" DefaultValue="5" /> then bind the control property like Text=’<%# Bind("TheHours") %>’ for textbox text property inside edit/insert template. Set also ListView DataKeyNames
= "ActivityId"

Then I get empty parameter values in the procedure call. Here is what SQL Profiler gives me for the procedure call:

exec Hours_testUpdate @TheHours = NULL, @ActivityID = NULL, @TheHoursID = 8

Regards

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestStoredProc.aspx.cs" Inherits="TestStoredProc_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:ListView ID="ListView1" runat="server" DataKeyNames="TheHoursID"
            DataSourceID="SqlDataTestStoored"
            onselectedindexchanged="ListView1_SelectedIndexChanged"
            onload="ListView1_Load">
            <AlternatingItemTemplate>
                <tr style="">
                    <td>
                        <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
                    </td>
                    <td>
                        <asp:Label ID="TheHoursIDLabel" runat="server"
                            Text='<%# Eval("TheHoursID") %>' />
                    </td>
                    <td>
                        <asp:Label ID="TheHoursLabel" runat="server" Text='<%# Eval("TheHours") %>' />
                    </td>
                    <td>
                        <asp:Label ID="TheDateLabel" runat="server" Text='<%# Eval("TheDate") %>' />
                    </td>
                    <td>
                        <asp:Label ID="ActivityIDLabel" runat="server"
                            Text='<%# Eval("ActivityID") %>' />
                    </td>
                    <td>
                        <asp:Label ID="InvoiceIDLabel" runat="server" Text='<%# Eval("InvoiceID") %>' />
                    </td>
                    <td>
                        <asp:Label ID="UserIDLabel" runat="server" Text='<%# Eval("UserID") %>' />
                    </td>
                </tr>
            </AlternatingItemTemplate>
            <EditItemTemplate>
                <tr style="">
                    <td>
                        <asp:Button ID="UpdateButton" runat="server" CommandName="Update"
                            Text="Update" />
                        <asp:Button ID="CancelButton" runat="server" CommandName="Cancel"
                            Text="Cancel" />
                    </td>
                    <td>
                        <asp:Label ID="TheHoursIDLabel1" runat="server"
                            Text='<%# Eval("TheHoursID") %>' />
                    </td>
                    <td>
                        <asp:TextBox ID="TheHoursTextBox" runat="server"
                            Text='<%# Eval("TheHours") %>' />
                    </td>
                    <td>
                        <asp:TextBox ID="TheDateTextBox" runat="server" Text='<%# Eval("TheDate") %>' />
                    </td>
                    <td>
                        <asp:TextBox ID="ActivityIDTextBox" runat="server"
                            Text='<%# Eval("ActivityID") %>' />
                    </td>
                    <td>
                        <asp:TextBox ID="InvoiceIDTextBox" runat="server"
                            Text='<%# Eval("InvoiceID") %>' />
                    </td>
                    <td>
                        <asp:TextBox ID="UserIDTextBox" runat="server" Text='<%# Eval("UserID") %>' />
                    </td>
                </tr>
            </EditItemTemplate>
            <EmptyDataTemplate>
                <table runat="server" style="">
                    <tr>
                        <td>
                            No data was returned.</td>
                    </tr>
                </table>
            </EmptyDataTemplate>
            <InsertItemTemplate>
                <tr style="">
                    <td>
                        <asp:Button ID="InsertButton" runat="server" CommandName="Insert"
                            Text="Insert" />
                        <asp:Button ID="CancelButton" runat="server" CommandName="Cancel"
                            Text="Clear" />
                    </td>
                    <td>
                        &nbsp;</td>
                    <td>
                        <asp:TextBox ID="TheHoursTextBox" runat="server"
                            Text='<%# Eval("TheHours") %>' />
                    </td>
                    <td>
                        <asp:TextBox ID="TheDateTextBox" runat="server" Text='<%# Eval("TheDate") %>' />
                    </td>
                    <td>
                        <asp:TextBox ID="ActivityIDTextBox" runat="server"
                            Text='<%# Eval("ActivityID") %>' />
                    </td>
                    <td>
                        <asp:TextBox ID="InvoiceIDTextBox" runat="server"
                            Text='<%# Eval("InvoiceID") %>' />
                    </td>
                    <td>
                        <asp:TextBox ID="UserIDTextBox" runat="server" Text='<%# Eval("UserID") %>' />
                    </td>
                </tr>
            </InsertItemTemplate>
            <ItemTemplate>
                <tr style="">
                    <td>
                        <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
                    </td>
                    <td>
                        <asp:Label ID="TheHoursIDLabel" runat="server"
                            Text='<%# Eval("TheHoursID") %>' />
                    </td>
                    <td>
                        <asp:Label ID="TheHoursLabel" runat="server" Text='<%# Eval("TheHours") %>' />
                    </td>
                    <td>
                        <asp:Label ID="TheDateLabel" runat="server" Text='<%# Eval("TheDate") %>' />
                    </td>
                    <td>
                        <asp:Label ID="ActivityIDLabel" runat="server"
                            Text='<%# Eval("ActivityID") %>' />
                    </td>
                    <td>
                        <asp:Label ID="InvoiceIDLabel" runat="server" Text='<%# Eval("InvoiceID") %>' />
                    </td>
                    <td>
                        <asp:Label ID="UserIDLabel" runat="server" Text='<%# Eval("UserID") %>' />
                    </td>
                </tr>
            </ItemTemplate>
            <LayoutTemplate>
                <table runat="server">
                    <tr runat="server">
                        <td runat="server">
                            <table ID="itemPlaceholderContainer" runat="server" border="0" style="">
                                <tr runat="server" style="">
                                    <th runat="server">
                                    </th>
                                    <th runat="server">
                                        TheHoursID</th>
                                    <th runat="server">
                                        TheHours</th>
                                    <th runat="server">
                                        TheDate</th>
                                    <th runat="server">
                                        ActivityID</th>
                                    <th runat="server">
                                        InvoiceID</th>
                                    <th runat="server">
                                        UserID</th>
                                </tr>
                                <tr ID="itemPlaceholder" runat="server">
                                </tr>
                            </table>
                        </td>
                    </tr>
                    <tr runat="server">
                        <td runat="server" style="">
                            <asp:DataPager ID="DataPager1" runat="server">
                                <Fields>
                                    <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True"
                                        ShowLastPageButton="True" />
                                </Fields>
                            </asp:DataPager>
                        </td>
                    </tr>
                </table>
            </LayoutTemplate>
            <SelectedItemTemplate>
                <tr style="">
                    <td>
                        <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
                    </td>
                    <td>
                        <asp:Label ID="TheHoursIDLabel" runat="server"
                            Text='<%# Eval("TheHoursID") %>' />
                    </td>
                    <td>
                        <asp:Label ID="TheHoursLabel" runat="server" Text='<%# Eval("TheHours") %>' />
                    </td>
                    <td>
                        <asp:Label ID="TheDateLabel" runat="server" Text='<%# Eval("TheDate") %>' />
                    </td>
                    <td>
                        <asp:Label ID="ActivityIDLabel" runat="server"
                            Text='<%# Eval("ActivityID") %>' />
                    </td>
                    <td>
                        <asp:Label ID="InvoiceIDLabel" runat="server" Text='<%# Eval("InvoiceID") %>' />
                    </td>
                    <td>
                        <asp:Label ID="UserIDLabel" runat="server" Text='<%# Eval("UserID") %>' />
                    </td>
                </tr>
            </SelectedItemTemplate>
        </asp:ListView>
        <asp:SqlDataSource ID="SqlDataTestStoored" runat="server"
            ConnectionString="<%$ ConnectionStrings:HoursConnectionString %>"
            SelectCommand="Hours_testSelect" SelectCommandType="StoredProcedure"
            UpdateCommand="Hours_testUpdate" UpdateCommandType="StoredProcedure"
            ondatabinding="ListView1_SelectedIndexChanged">
            <UpdateParameters>
                <asp:Parameter  Name="TheHours" Type="Decimal" />
                <asp:Parameter  Name="TheHoursID" Type="Int32"  />
                <asp:Parameter  Name="ActivityID" Type="Int32"  />
                <asp:Parameter Direction="ReturnValue" Name="RETURN_VALUE" Type="Int32" />
            </UpdateParameters>
        </asp:SqlDataSource>
    
    </div>
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
    </form>
</body>
</html>
Use two way binding (Bind) to pass value for updating instead one way binding (Eval).

Thanks, it worked.

Regards

Leave a Reply