UpdateCommand 参数值始终为字符串 [英] UpdateCommand Parameter value always a string
问题描述
我正在尝试使用 gridview 和 SQLDataSource 控件更新我的应用程序中的购物车,我正在尝试设置更新命令参数,以便我可以将值发送到我的 SQL 存储过程,但它不断抛出异常说它不能从 nvarchar 转换为字符串.
I am trying to update a shopping cart in my application using a gridview and a SQLDataSource control, I'm trying to set the update command parameters so that I can send values to my SQL Stored proc, but it keeps throwing an exception saying that it can't convert from nvarchar to string.
给定以下代码;
ProductsDataSource.UpdateParameters.Add("Description", row.Cells[2].Text);
这个方法似乎不接受除字符串以外的任何东西,因为它是第二个参数,那么我如何将其转换为其他值以传递给我的参数?
it seems that this method will not accept anything other than a String as it's second argument, so how can I convert that to other values to pass into my parameters?
我已经尝试过这样的事情;
I have already tried something like this;
int productID = int.Parse(row.Cells[1].Text);
但是由于第二个参数必须有一个字符串参数,我无法将其插入到我的数据库中(抱怨它不能隐式转换为字符串!)
but since that second parameter HAS to have a string argument, I can't insert it into my DB (Complaining it can't implicitly convert to string!)
推荐答案
您可以在标记上指定参数类型..
you can specify parameter type on markup..
<UpdateParameters >
<asp:Parameter Name="paramName" DbType="String" Type="String" />
并且您可以在后面的代码上设置值.
and you can set value on code behind.
ProductsDataSource.UpdateParameters["paramName"].DefaultValue = "parameter value";
或者,您可以在没有标记定义的情况下使用 Add 方法的重载.
OR you can use overloads of Add method without markup definition.
SqlDataSource1.UpdateParameters.Add("paramName", DbType.String, "parameter value");
这篇关于UpdateCommand 参数值始终为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!