简体   繁体   中英

Input string was not in a correct format vb.net/asp.net

Here's my formview..

<asp:FormView ID="FormView1" runat="server" DataSourceID="SqlDataSource1" DataKeyNames="recid">
<EditItemTemplate>
RECID:
<asp:TextBox ID="recid" runat="server" Text='<%# Eval("RECID") %>' ReadOnly="true" />
<br />
SHIPPER:
<asp:TextBox ID="shipper" runat="server" Text='<%# Bind("SHIPPER") %>' />
<br />
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Update" />
&nbsp;
<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
</asp:FormView>

here's my sqldatasource1

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
SelectCommand="SELECT * FROM IMPORT WHERE (RECID = ?)"
UpdateCommand="UPDATE IMPORT SET SHIPPER=@shipper WHERE RECID=@recid" >
<SelectParameters>
<asp:ControlParameter ControlID="Label1" Name="RECID" PropertyName="Text" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="recid" Type="Int32" />
<asp:Parameter Name="shipper" Type="String" />
</UpdateParameters>
</asp:SqlDataSource> 

Now, my problem is that everytime I update the page, this is the error Input string was not in a correct format.

I tried changing the @recid in the UpdateCommand into a constant UpdateCommand="UPDATE IMPORT SET SHIPPER=@shipper WHERE RECID=11186" this worked well, the update was successful.

I think the @recid is being treated as a string here in the UpdateCommand. Can someone please help me? What do I need to do in order to change the data type of @recid to Integer?

Thanks!

First thing you need to fix is to bind raceid with the textbox like below:

<asp:TextBox ID="recid" runat="server" Text='<%# Bind("RECID") %>' ReadOnly="true" />

Now there's a question about provider. In select command you have used RACEID = ? , ? is used for OleDb or Odbc . If you are using any of them, you have to change Update Command to this:

UpdateCommand="UPDATE IMPORT SET SHIPPER=? WHERE RECID=?"

And UpdateParameters to this (Notice their order):

<UpdateParameters>
    <asp:Parameter Name="shipper" Type="String" />
    <asp:Parameter Name="recid" Type="Int32" />
</UpdateParameters>

I would recommend reading this MSDN article: Using Parameters with the SqlDataSource Control

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM