![](/img/trans.png)
[英]The given value of type String from the data source cannot be converted to type varbinary of the specified target column
[英]Using textbox value as parameter in LINQ dat source/gridview : A value of type 'String' cannot be converted to type 'Double'
这是新的一年,但你仍然留下了厚厚的Dean先生!
好的,场景 - 我有一个文本框,两个单选按钮,一个按钮和一个gridview。
<code>
<body>
<form id="form1" name="form1" runat="server">
<asp:TextBox ID="tbxHowMany" runat="server"
style="z-index: 1; left: 245px; top: 105px; position: absolute; height: 20px; width: 345px;"
CssClass="completionList2"></asp:TextBox>
<asp:RadioButton ID="radGlass" runat="server" GroupName="WeightSearch"
style="z-index: 1; left: 655px; top: 150px; position: absolute" />
<asp:RadioButton ID="radPaper" runat="server" GroupName="WeightSearch"
style="z-index: 1; left: 655px; top: 105px; position: absolute"/>
<asp:Button ID="btnReturnWeight" runat="server" Text="Return Selected Weights"
onclick="btnReturnWeight_Click"
style="z-index: 1; left: 245px; top: 155px; position: absolute; right: 375px"
Height="25px" Width="350px" />
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False"
DataSourceID="LQTOPDS" CellPadding="4" Font-Size="X-Small"
ForeColor="#333333" GridLines="None"
style="z-index: 1; left: 0px; top: 530px; position: absolute; height: 295px; width: 1370px; text-align: center;" DataKeyNames="PriKey"
>
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:BoundField DataField="UnitId" HeaderText="UnitId"
SortExpression="UnitId" />
<asp:BoundField DataField="UnitDescription" HeaderText="UnitDescription"
SortExpression="UnitDescription" />
<asp:BoundField DataField="SaleQty" HeaderText="SaleQty"
SortExpression="SaleQty" />
<asp:BoundField DataField="LevelNo" HeaderText="LevelNo"
SortExpression="LevelNo" />
<asp:BoundField DataField="MaterialId" HeaderText="MaterialId"
SortExpression="MaterialId" />
<asp:BoundField DataField="PackagingTypeCode" HeaderText="PackagingTypeCode"
SortExpression="PackagingTypeCode" />
<asp:BoundField DataField="UnitWeight" HeaderText="UnitWeight"
SortExpression="UnitWeight" />
<asp:BoundField DataField="WeightUnitCode" HeaderText="WeightUnitCode"
SortExpression="WeightUnitCode" />
<asp:BoundField DataField="WeightStatus" HeaderText="WeightStatus"
SortExpression="WeightStatus" />
<asp:BoundField DataField="ProductPercentage" HeaderText="ProductPercentage"
SortExpression="ProductPercentage" />
<asp:BoundField DataField="Comment" HeaderText="Comment"
SortExpression="Comment" />
<asp:BoundField DataField="IDDesc" HeaderText="IDDesc"
SortExpression="IDDesc" />
<asp:BoundField DataField="PriKey" HeaderText="PriKey" ReadOnly="True"
SortExpression="PriKey" />
</Columns>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
<asp:LinqDataSource ID="LQTOPDS" runat="server"
ContextTypeName="CompleteWeightsDataContext"
TableName="tblOnlineReportingCOMPLETEWeights"
Where="ProductPercentage <= Double(@ProductPercentage)"
onselecting="LQTOPDS_Selecting" OrderBy="ProductPercentage desc">
<WhereParameters>
<asp:ControlParameter ControlID="tbxHowMany" Name="ProductPercentage"
PropertyName="Text" Type="Double" />
</WhereParameters>
</asp:LinqDataSource>
</form>
</body>
</code>
我试图实现的目标如下:用户在文本框中输入数字,更改linq数据源参数并使用此数字。 单击该按钮时,将显示gridview。
现在,我在后面的代码中有以下内容:
<code>
public partial class TOP___In_Development : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["MemberKey"] = "FM00012";
}
}
protected void btnReturnWeight_Click(object sender, EventArgs e)
{
LQTOPDS.WhereParameters.Clear();
ControlParameter cp = new ControlParameter();
cp.Type = TypeCode.String;
{
if (radPaper.Checked)
{
cp.ControlID = "tbxHowMany";
cp.PropertyName = "Text";
cp.Name = "ProductPercentage";
LQTOPDS.WhereParameters.Add(cp);
GridView1.DataSourceID = "LQTOPDS";
GridView1.DataBind();
}
else if (radGlass.Checked)
{
Convert.ToDouble(tbxHowMany.Text);
cp.ControlID = "tbxHowMany";
cp.PropertyName = "Text";
cp.Name = "ProductPercentage";
LQTOPDS.WhereParameters.Add(cp);
GridView1.DataSourceID = "LQTOPDS";
GridView1.DataBind();
}
}
}
protected void LQTOPDS_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
foreach (KeyValuePair<string, object> kvp in e.WhereParameters)
{
if (kvp.Value == null)
{
e.Cancel = true;
return;
}
}
}
}
</code>
但是,当我尝试运行它时,绑定失败,因为“类型字符串的值”无法转换为类型'double'
如何在btnReturnWeight_Click中转换文本框值以便使用double,并且希望生成gridview。
PS:我意识到if和else if条件目前会产生相同的结果,我将继续插入额外的参数!!
我们将非常感激地提供任何帮助。
要将字符串转换为双精度,您可以使用;
string value;
value = Double.MinValue.ToString();
try {
Console.WriteLine(Double.Parse(value));
}
catch (OverflowException) {
Console.WriteLine("{0} is outside the range of the Double type.", value);
}
value = Double.MaxValue.ToString();
try {
Console.WriteLine(Double.Parse(value));
}
catch (OverflowException) {
Console.WriteLine("{0} is outside the range of the Double type.", value);
}
// The example displays the following output:
// -1.79769313486232E+308 is outside the range of the Double type.
// 1.79769313486232E+308 is outside the range of the Double type.
如果要检查字符串是否为有效的双精度,可以使用try parse;
string value;
double number;
value = Double.MinValue.ToString();
if (Double.TryParse(value, out number))
Console.WriteLine(number);
else
Console.WriteLine("{0} is outside the range of a Double.", value);
value = Double.MaxValue.ToString();
if (Double.TryParse(value, out number))
Console.WriteLine(number);
else
Console.WriteLine("{0} is outside the range of a Double.", value);
// The example displays the following output:
// -1.79769313486232E+308 is outside the range of the Double type.
// 1.79769313486232E+308 is outside the range of the Double type.
所有主要搜索引擎都可以访问更多详细信息,请在此处一次搜索: http : //www.liquidjelly.co.uk/supersearch/? q = double.tryparse()&lang = en-GB
HTH,
标记
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.