簡體   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'

這是新的一年,但你仍然留下了厚厚的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 &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>

我試圖實現的目標如下:用戶在文本框中輸入數字,更改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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM