首页 新闻 搜索 专区 学院

ASP.Net 网站设计

0
悬赏园豆:10 [已解决问题] 解决于 2015-10-27 19:07

我有一段代码要实现加入购物车,出现问题。谁能帮我看看代码哪里有问题

protected void addToCart(object sender, ImageClickEventArgs e)
{
SqlDataSource ads = new SqlDataSource();
//string connstr = ConfigurationManager.ConnectionStrings["WebShop"].ToString();
ads.ConnectionString="WebShop".ToString();
string sqlstr = "SELECT * FROM [tb_ShoppingCart] WHERE [User_Id] ='" + Session["cartName"] + "' AND Goods_Id =" + FormView1.DataKey.Value;


// SqlDataSource ads=new SqlDataSource();
// DataSet dv = (DataSet)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
DataView dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
//DataView dv = (DataView)ads.Select(DataSourceSelectArguments.Empty);
if (dv.Count == null)
{
Label LB1=(Label)FormView1.FindControl("productnameLabel");
Label LB2=(Label)FormView1.FindControl("productpriceLabel");
ads.InsertCommand = "INSERT INTO [tb_ShoppingCart] ([User_Id], [Goods_Id], [Goods_Name], [Goods_Price], [Goods_Num]) VALUES (?, ?, ?, ?, 1)";
ads.InsertParameters.Add("User_Id", Convert.ToString(Session["cartName"]));
ads.InsertParameters.Add("Goods_Id", Convert.ToString(FormView1.DataKey.Value));
ads.InsertParameters.Add("Goods_Name", LB1.Text);
ads.InsertParameters.Add("Goods_Price", LB2.Text);
ads.Insert();
}
else
{
ads.UpdateCommand = "UPDATE [tb_ShoppingCart] SET [Cart_Num] = [Cart_Num]+1 WHERE [User_Id] ='" + Session["cartName"] + "' AND Goods_Id =" + FormView1.DataKey.Value;
// ads.Update();
ads.Update();
}
Response.Redirect("~/cartBasket.aspx");
}
}

杨杨昵的主页 杨杨昵 | 初学一级 | 园豆:93
提问于:2015-10-27 14:06
< >
分享
最佳答案
0

ads.ConnectionString="WebShop".ToString();

这不是一个连接字符串吧? 还有你出现异常了.异常信息应该也要贴出来才对吧. 

收获园豆:10
李丶GuanYao | 小虾三级 |园豆:1228 | 2015-10-27 14:24

因为我从来没用过access数据库写代码,我是从access数据库找到的代码改的,我自己不会写,我把原来的发来给你你帮我改一下嘛,我用sql数据库

protected void addToCart(object sender, ImageClickEventArgs e)
{
AccessDataSource ads=new AccessDataSource();
ads.DataFile="~/App_data/mystore.mdb";
ads.SelectCommand = "SELECT * FROM [cart] WHERE [cartCustomer] ='" + Session["cartName"] + "' AND ProductID =" + FormView1.DataKey.Value;
DataView dv = (DataView)ads.Select(DataSourceSelectArguments.Empty);
if(dv.Count == 0)
{
Label LB1=(Label)FormView1.FindControl("productnameLabel");
Label LB2=(Label)FormView1.FindControl("productpriceLabel");
ads.InsertCommand = "INSERT INTO [cart] ([cartCustomer], [ProductID], [ProductName], [UnitPrice], [Quantity]) VALUES (?, ?, ?, ?, 1)";
ads.InsertParameters.Add("cartCustomer", Convert.ToString(Session["cartName"]));
ads.InsertParameters.Add("ProductID", Convert.ToString(FormView1.DataKey.Value));
ads.InsertParameters.Add("ProductName", LB1.Text);
ads.InsertParameters.Add("UnitPrice", LB2.Text);
ads.Insert();
}
else
{
ads.UpdateCommand = "UPDATE [cart] SET [Quantity] = [Quantity]+1 WHERE [cartCustomer] ='" + Session["cartName"] + "' AND ProductID =" + FormView1.DataKey.Value;
ads.Update();
}
Response.Redirect("~/cartBasket.aspx");
}

 

杨杨昵 | 园豆:93 (初学一级) | 2015-10-27 14:36

@杨杨昵: 

你改一下这几句看看.. 还有就是你的WebShop的链接字符串要配置对.

protected void addToCart(object sender, ImageClickEventArgs e)
{
    SqlDataSource ads = new SqlDataSource();
    ads.ConnectionString = ConfigurationManager.ConnectionStrings["WebShop"].ConnectionString;
    ads.SelectCommand = "SELECT * FROM [cart] WHERE [cartCustomer] ='" + Session["cartName"] + "' AND ProductID =" + FormView1.DataKey.Value;
    DataView dv = (DataView)ads.Select(DataSourceSelectArguments.Empty);
    if(dv.Count == 0)
    {
        Label LB1=(Label)FormView1.FindControl("productnameLabel");
        Label LB2=(Label)FormView1.FindControl("productpriceLabel");
        ads.InsertCommand = "INSERT INTO [cart] ([cartCustomer], [ProductID], [ProductName], [UnitPrice], [Quantity]) VALUES (@cartCustomer, @ProductID, @ProductName, @UnitPrice, 1)";
        ads.InsertParameters.Add("cartCustomer", Convert.ToString(Session["cartName"]));
        ads.InsertParameters.Add("ProductID",    Convert.ToString(FormView1.DataKey.Value));
        ads.InsertParameters.Add("ProductName", LB1.Text);
        ads.InsertParameters.Add("UnitPrice", LB2.Text);
        ads.Insert();
    }
    else
    {
        ads.UpdateCommand = "UPDATE [cart] SET [Quantity] = [Quantity]+1 WHERE [cartCustomer] ='" + Session["cartName"] + "' AND ProductID =" + FormView1.DataKey.Value;
        ads.Update();
    }
    Response.Redirect("~/cartBasket.aspx");
} 
李丶GuanYao | 园豆:1228 (小虾三级) | 2015-10-27 14:57

@李丶GuanYao: 非常感谢,我先试一下

杨杨昵 | 园豆:93 (初学一级) | 2015-10-27 15:07

@李丶GuanYao: 

ads.Update();这里有问题,总显示:在将 varchar 值 'pl4a13uk4tiazn55omoq2krf' 转换成数据类型 int 时失败。这是什么问题啊?

杨杨昵 | 园豆:93 (初学一级) | 2015-10-27 15:45

@杨杨昵: 

你debug。 查看FormView1.DataKey.Value 和Session["cartName"] 的值是什么,然后对比一下数据库里面carCustomer和ProjectID 是什么类型的就知道了. 

肯定是有一个值是错的. 异常已经说的很清楚了.  就是一个varchar型的值想转int型失败了.

李丶GuanYao | 园豆:1228 (小虾三级) | 2015-10-27 15:50

@李丶GuanYao: 恩恩,我看一下

杨杨昵 | 园豆:93 (初学一级) | 2015-10-27 15:52

@杨杨昵: 

异常详细信息: System.Data.SqlClient.SqlException: 在将 varchar 值 'pl4a13uk4tiazn55omoq2krf' 转换成数据类型 int 时失败。

源错误: 

行 43: ads.UpdateCommand = "UPDATE [tb_ShoppingCart] SET [Cart_Num] = [Cart_Num]+1 WHERE [User_Id] ='" + Session["cartName"] + "' AND Goods_Id =" + FormView1.DataKey.Value; 行 44: // ads.Update(); 行 45: ads.Update(); 行 46: } 行 47: Response.Redirect("~/cartBasket.aspx");


源文件: d:\ch10\完成文件\shopCart\product.aspx.cs    行: 45 

堆栈跟踪: 

[SqlException (0x80131904): 在将 varchar 值 'pl4a13uk4tiazn55omoq2krf' 转换成数据类型 int 时失败。] System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +1959034 System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +4876523 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +194 System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +1121 System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async) +192 System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) +317 System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +137 System.Web.UI.WebControls.SqlDataSourceView.ExecuteDbCommand(DbCommand command, DataSourceOperation operation) +386 System.Web.UI.WebControls.SqlDataSourceView.ExecuteUpdate(IDictionary keys, IDictionary values, IDictionary oldValues) +325 System.Web.UI.WebControls.SqlDataSource.Update() +20 product.addToCart(Object sender, ImageClickEventArgs e) in d:\ch10\完成文件\shopCart\product.aspx.cs:45 System.Web.UI.WebControls.ImageButton.OnClick(ImageClickEventArgs e) +108 System.Web.UI.WebControls.ImageButton.RaisePostBackEvent(String eventArgument) +118 System.Web.UI.WebControls.ImageButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565

我找不来,能帮我看看吗

杨杨昵 | 园豆:93 (初学一级) | 2015-10-27 16:24

@杨杨昵: 

你FormView1.DataKey.Value的值和Session["cartName"]的值分别是什么?.

李丶GuanYao | 园豆:1228 (小虾三级) | 2015-10-27 17:07

@李丶GuanYao:是商品编号 

数据源里面我配置过:

<SelectParameters>
<asp:SessionParameter Name="User_Id" SessionField="cartName" Type="String" />
</SelectParameters>

杨杨昵 | 园豆:93 (初学一级) | 2015-10-27 17:50

@李丶GuanYao: 数据库商品编号,用户编号,购物车编号都是int型,代码就是刚刚发的那个

杨杨昵 | 园豆:93 (初学一级) | 2015-10-27 17:58
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册