views:

314

answers:

1

Hello all - it's a New Year but you're still left with a thick Mr Dean!!

Ok, the scenario - I have a textbox, a two radio buttons, a button and a 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. When the button is clicked, the gridview is displayed.

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'

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.

PS: I realise the if and else if conditions will produce the same results at present, I'll work on inserting additional parameters next!!

Any help would be gratefully received.

A: 

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;

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()&amp;lang=en-GB

HTH,
Mark

Mark Cooper
Thanks Mark, greatly appreciated.
MrDean