hello i have one formview control n i want to retrive values from that n add save them in local variables

<asp:FormView ID="FormView1" runat="server" DataSourceID="SqlDataSource1" Style="z-index: 100;
        left: 210px; position: absolute; top: 173px" Width="299px" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" GridLines="Both" DataKeyNames="pcode">
        <ItemTemplate>
            pcode:
            <asp:Label ID="pcodeLabel" runat="server" Text='<%# Eval("pcode") %>'></asp:Label><br />
            <br />
            pname:
            <asp:Label ID="pnameLabel" runat="server" Text='<%# Eval("pname") %>'></asp:Label><br />            
            <br />
            <asp:ImageButton ID="ImageButton1" runat="server" PostBackUrl='<%# Eval("purl","display.aspx?purl=items/{0}") %>' ImageUrl='<%# Eval("purl","items/{0}") %>' Height="30%" Width="30%" />&nbsp;<br />

            <br />
            <asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="~/icon/button_addtocart.gif" Width="60px" OnClick="ImageButton2_Click"/>
            <br />

            price:
            <asp:Label ID="priceLabel" runat="server" Text='<%# Eval("price", "{0:c}") %>'></asp:Label>
            Rs.
        </ItemTemplate>
        <FooterTemplate>
            <a href="ipods.aspx">Return to the main shop</a>
        </FooterTemplate>     
        
    </asp:FormView>



[B]this is .cs file code for imagebutton's click event:[/B]

 protected void ImageButton2_Click(object sender, System.Web.UI.ImageClickEventArgs e)
    {
        string pname = ((Label)FormView1.FindControl("pnameLabel")).Text;
        string price = ((Label)FormView1.FindControl("priceLabel")).Text;
        double price = double.Parse(p.ToString());
    }

i get error while retriving values of labels :

Error 1 'Label' is an ambiguous reference between 'System.Web.UI.WebControls.Label' and 'System.Windows.Forms.Label'

n for double.Parse: like : "Input string was not in a correct format"

Dani AI

Generated

Two things were happening in this thread: a compile-time name clash and a runtime parse failure. The compile error comes from having both WinForms and WebForms label types available in the same file. As pointed out, removing the Windows Forms namespace import removes the ambiguity; as suggested, another option is to explicitly reference the web control type (System.Web.UI.WebControls.Label) when casting. confirmed that removing the extra namespace / fully qualifying the type fixed the compile error.

The runtime "Input string was not in a correct format" is caused by trying to parse a UI-formatted string (currency symbols, thousands separators, culture-specific decimals) or by reusing the same variable name for different types. Best practices:

  • Use decimal for money, not double.
  • Parse with culture-aware settings (NumberStyles.Currency + a CultureInfo) or, better, avoid parsing visible, formatted text at all.
  • Prefer retrieving the raw numeric value from the data layer (DataKey, DataItem, a hidden field bound to the raw value, or from ItemCommand/ItemDataBound) instead of parsing label.Text.

Example (safe parsing fallback):

using System.Globalization;

// safe retrieval and parsing
var lbl = FormView1.Row.FindControl("priceLabel") as System.Web.UI.WebControls.Label;
if (lbl != null)
{
    string raw = lbl.Text.Trim();
    if (decimal.TryParse(raw, NumberStyles.Currency, CultureInfo.CurrentCulture, out decimal priceValue))
    {
        // priceValue is a usable decimal
    }
}

Additional tips: don’t declare the same identifier twice in one method (e.g., string price then double price), null-check casts (as + null test), and consider binding an unformatted price into a HiddenField or including it in DataKeyNames so the code can access the numeric value directly without parsing formatted UI text.

Recommended Answers

All 4 Replies

Use fully qualified class names and it will solve the problem, or post your project in its entirety.

I think you have added System.Windows.Forms namespace in you class file. Check whether the line "using System.Windows.Forms" exist on top of your class file and remove it.

ya u r correct.. i have added using System.Windows.Forms namespace.. by writing fully qualified class name it works.

thanx..!

mark as solved please

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.