输入字符串不在“正确格式错误”中 [英] Input string is not in the Correct Format Error

查看:79
本文介绍了输入字符串不在“正确格式错误”中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到的错误输入字符串格式不正确..



i检查我所有的数据库类型代码..但是没有弄清楚问题..



i我正在实施我想要将商品添加到购物车中..



i有一张桌子具有属性的ShoppingCart(CustomerID (Varchar50),ProductID(int),ProductID(Int)



 <  < pre  >  asp:GridView ShowHeader =falseID =GridView1runat =serverAutoGenerateColumns =False
onselectedindexchanged =GridView1_SelectedIndexChangedWidth =385px
style =margin-right:4pxonrowcommand =_ RowCammand>
< >
< asp:TemplateField ShowHeader = 错误 >
< ItemTemplate >
< table style = width:100% >
< tr >
< td < /跨度> rowspan = 4 valign = top width = 5% >
< ; asp:图像 ID = Image1 runat = server ImageUrl = ' <% #Eval( ProductImage)%>' / >
< / td >
< td align = left >
< asp:HyperLink ID = HyperLink1 runat = 服务器

< span class =code-attribute>
< span class =code-attribute> NavigateUrl =' <% #Eval( < span class =code-string> ProductID 〜/ displayproduct.aspx?productid = { 0})%>'

< span class =code-attribute> 正文 =' <% #Eval( ProductName)% >' > < / asp:HyperLink >
< / td >
< / tr >
< tr >
< td align = left >
< asp:标签 ID = Label3 runat = server

< span class =code-attribute> 文字 =' <% #GetShortDescription(Eval( Description)。ToString())%>' > < / asp:Label >
< / td >
< / tr >
< tr >
< td align = left >
< ; asp:标签 ID = Label5 runat = server SkinID = FormLabel 文字 = 价格: > < / asp:标签 >
< asp:标签 < span class =code-attribute> ID = Label4 runat = server 文字 =' <% #Eval( UnitCost {0:C}) %>' > < / asp:标签 >
< / td >
< / tr >
< tr >
< ; td align < span class =code-keyword> = right >
< asp:按钮 ID = btnaddcart runat = < span class =code-keyword> server 文本 = 添加到购物车 / >
< / td >
< / tr >
< / table >
< / ItemTemplate >
< / asp:TemplateField < span class =code-keyword>>
< / Columns >
< / asp: GridView >





  protected   void  _RowCammand( object  sender ,GridViewCommandEventArgs e)
{
if (!(User.Identity.IsAuthenticated))
{
Response.Redirect ( 〜/ login.aspx);
}
ShoppingCartItem item = new ShoppingCartItem();
item.CustomerID = User.Identity.Name;
item.ProductID = System.Convert.ToInt32(e.CommandArgument); // 此行正在做什么????
item.Quantity = 1 ;
ShoppingCart.AddItem(item);





在ShoppingCartItem类中,我将CustomerID设置为String,将ProductID和Quantity设置为Int



和以下是StoreProcedure



  set  ANSI_NULLS ON 
set QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo]。[AddToCart]
@CustomerID varchar( 50 ),
@ProductID int
@Quantity int
AS
BEGIN
INSERT INTO ShoppingCart
(CustomerID,Quantity,ProductID)
VALUES(@ CustomerID,@ Quantity, @ProductID)
结束









  public   class  ShoppingCart 
{

< span class =code-keyword> public static int AddItem(ShoppingCartItem item)
{
dbcon obj = new dbcon();
SqlParameter [] objParams = new SqlParameter [ 3 ];
objParams [ 0 ] = new SqlParameter( @ CustomerID,item.CustomerID);
// objParams [1] = new SqlParameter(@ ProductID,item.ProductID);
objParams [ 1 ] = new SqlParameter( @ Quantity,item.Quantity);
objParams [ 2 ] = new SqlParameter( @ ProductID,item.Quantity);
return obj.ExecuteNonQuery( AddToCart ,objParams);
}







和dbcon类我有一个方法



< pre>  public   int  ExecuteNonQuery( string  query,< span class =code-keyword> params  SqlParameter []参数)
{
SqlConnection cnn = new SqlConnection(connectionString) ;
SqlCommand cmd = new SqlCommand(query,cnn);
if (query.StartsWith( INSERT )| query.StartsWith( insert)| query.StartsWith( UPDATE)| query.StartsWith( update)| query.StartsWith( DELETE)| query.StartsWith( delete))
{
cmd.CommandType = CommandType.Text;
}
else
{
cmd.CommandType = CommandType.StoredProcedure;
}
for int i = 0 ; i < = parameters.Length - 1 ; i ++)
{
cmd.Parameters.Add(parameters [i]);
}
cnn.Open();
int retval = cmd.ExecuteNonQuery();
cnn.Close();
return retval;
}

解决方案

item.ProductID 可能为null。

I am getting error INput String in not in the correct format..

i check all my database types code.. but not figure out the problem..

i am implementing i want add item into the cart..

i have a table ShoppingCart having Attributes ( CustomerID(Varchar50),ProductID(int), ProductID(Int)

<<pre>asp:GridView ShowHeader="false" ID="GridView1" runat="server" AutoGenerateColumns="False" 
            onselectedindexchanged="GridView1_SelectedIndexChanged" Width="385px" 
            style="margin-right: 4px" onrowcommand="_RowCammand">
            <Columns>
                <asp:TemplateField ShowHeader="False">
                    <ItemTemplate>
                        <table style="width: 100%">
                            <tr>
                                <td rowspan="4" valign="top" width="5%">
                                    <asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("ProductImage") %>' />
                                </td>
                                <td align="left">
                                    <asp:HyperLink ID="HyperLink1" runat="server" 

                                        NavigateUrl='<%# Eval("ProductID", "~/displayproduct.aspx?productid={0}") %>' 

                                        Text='<%# Eval("ProductName") %>'></asp:HyperLink>
                                </td>
                            </tr>
                            <tr>
                                <td align="left">
                                    <asp:Label ID="Label3" runat="server" 

                                        Text='<%# GetShortDescription(Eval("Description").ToString()) %>'></asp:Label>
                                </td>
                            </tr>
                            <tr>
                                <td align="left">
                                    <asp:Label ID="Label5" runat="server" SkinID="FormLabel" Text="Price :"></asp:Label>
                                    <asp:Label ID="Label4" runat="server" Text='<%# Eval("UnitCost", "{0:C}") %>'></asp:Label>
                                </td>
                            </tr>
                            <tr>
                                <td align="right">
                                    <asp:Button ID="btnaddcart" runat="server" Text="Add to Cart" />
                                </td>
                            </tr>
                        </table>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>



protected void _RowCammand(object sender, GridViewCommandEventArgs e)
   {
       if (!(User.Identity.IsAuthenticated))
       {
           Response.Redirect("~/login.aspx");
       }
       ShoppingCartItem item = new ShoppingCartItem();
       item.CustomerID = User.Identity.Name;
       item.ProductID = System.Convert.ToInt32(e.CommandArgument); //What this line is doing????
       item.Quantity = 1;
       ShoppingCart.AddItem(item);



In ShoppingCartItem Class i set CustomerID to String and ProductID and Quantity to Int

and Here is the StoreProcedure

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[AddToCart] 
	@CustomerID varchar(50),
	@ProductID int,
	@Quantity int
AS
BEGIN
	INSERT INTO ShoppingCart
	(CustomerID,Quantity,ProductID) 
	VALUES(@CustomerID,@Quantity,@ProductID)
END





public class ShoppingCart
{
	
    public static int AddItem(ShoppingCartItem item)
    {
        dbcon obj = new dbcon();
        SqlParameter[] objParams = new SqlParameter[3];
        objParams[0] = new SqlParameter("@CustomerID", item.CustomerID);
        //objParams[1] = new SqlParameter("@ProductID", item.ProductID);
        objParams[1] = new SqlParameter("@Quantity", item.Quantity);
        objParams[2] = new SqlParameter("@ProductID", item.Quantity);
        return obj.ExecuteNonQuery("AddToCart", objParams);
    }




and in dbcon class i have a method

<pre> public int ExecuteNonQuery(string query, params SqlParameter[] parameters)
        {
            SqlConnection cnn = new SqlConnection(connectionString);
            SqlCommand cmd = new SqlCommand(query, cnn);
            if (query.StartsWith("INSERT") | query.StartsWith("insert") | query.StartsWith("UPDATE") | query.StartsWith("update") | query.StartsWith("DELETE") | query.StartsWith("delete"))
            {
                cmd.CommandType = CommandType.Text;
            }
            else
            {
                cmd.CommandType = CommandType.StoredProcedure;
            }
            for (int i = 0; i <= parameters.Length - 1; i++)
            {
                cmd.Parameters.Add(parameters[i]);
            }
            cnn.Open();
            int retval = cmd.ExecuteNonQuery();
            cnn.Close();
            return retval;
        }

解决方案

item.ProductID is probably null.


这篇关于输入字符串不在“正确格式错误”中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆