简体   繁体   English

在LINQ dat source / gridview中使用文本框值作为参数:“String”类型的值无法转换为“Double”类型

[英]Using textbox value as parameter in LINQ dat source/gridview : A value of type 'String' cannot be converted to type 'Double'

it's a New Year but you're still left with a thick Mr Dean!! 这是新的一年,但你仍然留下了厚厚的Dean先生!

Ok, the scenario - I have a textbox, a two radio buttons, a button and a gridview. 好的,场景 - 我有一个文本框,两个单选按钮,一个按钮和一个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 &lt;= 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>

What I am attempting to achieve is the following: A user types in a number into the textbox, the linq data source parameter is changed and uses this number. 我试图实现的目标如下:用户在文本框中输入数字,更改linq数据源参数并使用此数字。 When the button is clicked, the gridview is displayed. 单击该按钮时,将显示gridview。

Now currently, I have the following in the code behind: 现在,我在后面的代码中有以下内容:

<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>

However, when I attempt to run this, the binding fails as "a value of type string' cannot be converted to type 'double' 但是,当我尝试运行它时,绑定失败,因为“类型字符串的值”无法转换为类型'double'

How do I go about converting the textbox value within the btnReturnWeight_Click so that a double is used and, hopefully, the gridview is produced. 如何在btnReturnWeight_Click中转换文本框值以便使用double,并且希望生成gridview。

PS: I realise the if and else if conditions will produce the same results at present, I'll work on inserting additional parameters next!! PS:我意识到if和else if条件目前会产生相同的结果,我将继续插入额外的参数!!

Any help would be gratefully received. 我们将非常感激地提供任何帮助。

To convert a string to a double you can use; 要将字符串转换为双精度,您可以使用;

   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.

If you want to check that a string is a valid double you can use try parse; 如果要检查字符串是否为有效的双精度,可以使用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.   

More details are accesible from all the major search engines, search them all at once here: http://www.liquidjelly.co.uk/supersearch/?q=double.tryparse()&lang=en-GB 所有主要搜索引擎都可以访问更多详细信息,请在此处一次搜索: http//www.liquidjelly.co.uk/supersearch/? q = double.tryparse()&lang = en-GB

HTH, HTH,
Mark 标记

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 来自数据源的String类型的给定值不能转换为指定目标列的varbinary类型 - The given value of type String from the data source cannot be converted to type varbinary of the specified target column 数据源中String类型的给定值无法转换为指定目标列的int类型 - The given value of type String from the data source cannot be converted to type int of the specified target column SqlBulkCopy - 来自数据源的 String 类型的给定值无法转换为指定目标列的货币类型 - SqlBulkCopy - The given value of type String from the data source cannot be converted to type money of the specified target column 来自数据源的 String 类型的给定值无法转换为指定目标列的 int 类型。 - The given value of type String from the data source cannot be converted to type int of the specified target column.' 数据源中String类型的给定值无法转换为指定目标列的float类型 - The given value of type String from the data source cannot be converted to type float of the specified target column 来自数据源的 String 类型的给定值无法转换为指定目标列的 nvarchar 类型 - The given value of type String from the data source cannot be converted to type nvarchar of the specified target column 类型“ double”的值不能转换为“ System.Windows.Forms.DataGridViewCell” - Value of type 'double' cannot be converted to 'System.Windows.Forms.DataGridViewCell “T”类型的值无法转换为 - Value of type 'T' cannot be converted to 为什么类型为null的值不能用作double类型的默认参数? - Why a value of type null cannot be used as a default parameter with type double? SQL批量复制“使用ASP.NET无法将数据源中String类型的给定值转换为指定目标列的类型日期时间” - SQL Bulk Copy “The given value of type String from the data source cannot be converted to type datetime of the specified target column” using ASP.NET
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM