2

Got a small problem but don't know how to solve it. Getting the exception:

"Input string was not in a correct format."

I hope someone can take his/her time to read my codes and try to help me solve the problem.

I'm searching through a SharePoint list, listname: "Extern Products" and got the columns: Title, ProductNumber, ListPrice, Color and MoreInformation.

Code Consumer WebPart (SearchWPList.cs:

[ToolboxItemAttribute(false)]
public class SearchWPList : WebPart
{
    // Visual Studio might automatically update this path when you change the Visual Web Part project item.
    private const string _ascxPath = @"~/_CONTROLTEMPLATES/Kingen.Artifacts.WebParts/SearchWPList/SearchWPListUserControl.ascx";
    private Control _control;

    protected override void CreateChildControls()
    {
        Control control = Page.LoadControl(_ascxPath);
        Controls.Add(control);
    }

    protected IProductSearch ProductSearch;

    [ConnectionConsumer("Products")]
    public void SetProduct(IProductSearch product)
    {
        ProductSearch = product;
    }

    protected override void OnPreRender(EventArgs e)
    {
        if(ProductSearch != null)
        {
            ((SearchWPListUserControl)_control).SetProduct(ProductSearch.Name);
            ((SearchWPListUserControl)_control).SetProduct(ProductSearch.MoreInformation);
            ((SearchWPListUserControl)_control).SetProduct(ProductSearch.ProductNumber);
            ((SearchWPListUserControl)_control).SetProduct(ProductSearch.Color);
            ((SearchWPListUserControl)_control).SetProduct(ProductSearch.ListPrice.ToString(CultureInfo.InvariantCulture));
        }
        base.OnPreRender(e);
    }
}

Code SearchWPListUserControl.ascx.cs:

public partial class SearchWPListUserControl : UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    public void SetProduct (string product)
    {
        if (product == "ProductNumber")
        {
            TextBox_ProdNumList.Text = "ProductNumber";
        }
        else if (product == "ListPrice")
        {
            TextBox_ListPriceList.Text = "ListPrice";
        }
        else if (product == "Color")
        {
            TextBox_ColorList.Text = "Color";
        }
        else if (product == "Name")
        {
            TextBox_NameList.Text = "Name";
        }
        else if (product == "MoreInformation")
        {
            TextBox_MoreInfoList.Text = "MoreInformation";
        }
    }
}

Code Provider WebPart (SearchList.cs):

[ToolboxItemAttribute(false)]
public class SearchList : WebPart, IProductSearch
{
    // Visual Studio might automatically update this path when you change the Visual Web Part project item.
    private const string _ascxPath = @"~/_CONTROLTEMPLATES/Kingen.Artifacts.WebParts/SearchList/SearchListUserControl.ascx";
    private readonly Control _prodNum;
    private readonly Control _name;
    private readonly Control _listPrice;
    private readonly Control _moreInformation;
    private readonly Control _color;

    protected override void CreateChildControls()
    {
        Control control = Page.LoadControl(_ascxPath);
        Controls.Add(control);
    }

    public string ProductNumber
    {
        get
        {
            if (_prodNum != null)
                return ((SearchListUserControl) _prodNum).ProductNumber;
            return string.Empty;
        }
    }

    public string Name
    {
        get
        {
            if (_name != null)
                return ((SearchListUserControl)_name).Name;
            return string.Empty;
        }
    }

    public string Color
    {
        get
        {
            if (_color != null)
                return ((SearchListUserControl)_color).Color;
            return string.Empty;
        }
    }

    public decimal ListPrice
    {
        get
        {
            if (_listPrice != null)
                return ((SearchListUserControl)_listPrice).ListPrice;
            return decimal.Parse(string.Empty);
        }
    }

    public string MoreInformation
    {
        get
        {
            if (_moreInformation != null)
                return ((SearchListUserControl)_moreInformation).MoreInformation;
            return string.Empty;
        }
    }

    [ConnectionProvider("Product")]
    public IProductSearch GetProduct()
    {
        return this;
    }
}

Code SearchListUserControl.ascx.cs:

public partial class SearchListUserControl : UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {          
    }

    protected void Button_Search_Click(object sender, EventArgs e)
    {
        ListBox_List.Items.Clear();

        SPWeb web = SPContext.Current.Web;
        SPList prodList = web.Lists.TryGetList("Extern Products");

        if (prodList != null)
        {
            SPQuery query = new SPQuery();
            query.Query = string.Format("<Where><BeginsWith><FieldRef Name='Title' /><Value Type='Text'>{0}</Value></BeginsWith></Where>", TextBox_Search.Text);
            query.ViewFields = "";

            SPListItemCollection items = prodList.GetItems(query);

            foreach (SPListItem listItem in items)
            {
                ListBox_List.Items.Add(listItem.Title);
            }
        }
    }

    public string ProductNumber { get; set; }
    public string Name { get; set; }
    public decimal ListPrice { get; set; }
    public string MoreInformation { get; set; }
    public string Color { get; set; }

    protected void Button_Edit_Click(object sender, EventArgs e)
    {
        try
        {
            if (ListBox_List.SelectedItem != null)
            {
                Name = ListBox_List.SelectedItem.Text;
                ProductNumber = ListBox_List.SelectedItem.Text;
                ListPrice = decimal.Parse(ListBox_List.SelectedItem.Text);
                MoreInformation = ListBox_List.SelectedItem.Text;
                Color = ListBox_List.SelectedItem.Text;
            }
        }
        catch (Exception x)
        {

            Label_ExtensionList.Text = x.Message;
        }
    }
}

My button search control in SearchListUserControl.ascx.cs works without problems. But i cannot get the provider to provide data i my consumer webpart.

Kind regards, Kristian

5
  • Your code looks ok to me, btw you did not provide interface declaration, I guess everything is fine in the interface, still make sure it's properly constructed and interface itself is declared as public, btw i am sure problem is elsewhere but one suggestion is to change the name of the connection consumer method, it kind of conflicts with SearchWPListUserControl class's method name, not good for reading
    – Diptarag
    Commented Nov 29, 2012 at 10:21
  • public interface IProductSearch { string ProductNumber { get; } string Name { get; } decimal ListPrice { get; } string MoreInformation { get; } string Color { get; } }
    – krisal
    Commented Nov 29, 2012 at 10:28
  • Thats my interface, don't really know if its dont correctly, but it seems so. I don't really understand your answer, could you maybe be more specific and type exactly what to change? Kinda confused now after 4h of google. hehe
    – krisal
    Commented Nov 29, 2012 at 10:29
  • I suspect that it is something with my ListPrice to do because it's decimal. If i remove it, and only want to get Name, ProductNum, etc, then i don't get the exception. But i still don't get an result in my consumer web part.
    – krisal
    Commented Nov 29, 2012 at 10:39
  • I already posted the answer, try it and let me know
    – Diptarag
    Commented Nov 29, 2012 at 10:42

3 Answers 3

0

I guess this line is throwing error for your case in SearchList.cs-

public decimal ListPrice
{
    get
    {
        if (_listPrice != null)
            return ((SearchListUserControl)_listPrice).ListPrice;
        ***return decimal.Parse(string.Empty);***
    }
}

change return decimal.Parse(string.Empty), it will never work, change it to return decimal.MinValue instead, and add some checking when you use the value


Though I dont get what you are trying to achieve with this method, but you can use TryParse to distinguish between pure string and number, may be like this -

    public void SetProduct(string product)
    {
        decimal ListPrice;
        if(decimal.TryParse(product, out ListPrice)
        {
            TextBox_ListPriceList.Text="ListPrice";
        }
        else
        {
            if (product == "ProductNumber")
            {
                TextBox_ProdNumList.Text = "ProductNumber";
            }            
            else if (product == "Color")
            {
                TextBox_ColorList.Text = "Color";
            }
            else if (product == "Name")
            {
                TextBox_NameList.Text = "Name";
            }
            else if (product == "MoreInformation")
            {
                TextBox_MoreInfoList.Text = "MoreInformation";
            }
        }            
    }
5
  • Thanks. But do i have to make one more Method for my ListPrice in SearchWPListUserControl.ascx.cs because it is Decimal? All the other columns are String. But when i make the method i get an error: Error Operator '==' cannot be applied to operands of type 'decimal' and 'string'
    – krisal
    Commented Nov 29, 2012 at 10:48
  • no need to create another method, you can use decimal.tryparse (msdn.microsoft.com/en-us/library/9zbda557.aspx)
    – Diptarag
    Commented Nov 29, 2012 at 10:54
  • I don't really get it, how to change my method so i can use decimal.tryparse. The site you gave there doesn't load properly for me so i can't read the whole site. :(
    – krisal
    Commented Nov 29, 2012 at 11:23
  • edited my post,
    – Diptarag
    Commented Nov 29, 2012 at 11:39
  • Tried that, same problem. I'll try be more specific about my project. First of all, i created a webpart so i could search in a Database and save my the info in a SharePoint List called "Extern products", it works. Second, i created one webpart so i could search in my sharepoint list and retrieve the items in a listbox, that also works, next step is to connect the listbox webpart with a third one and when i select my item in the listbox and click on my Button, then i want to get all the information of the product in the third webpart.
    – krisal
    Commented Nov 29, 2012 at 11:49
0

to put it simple your missing the interface class. Its the middle man that connects the two webparts together (provider to the consumer).

the class will look somthing like this:

using System;
using System.Collections.Generic;
using System.Text;

namespace CommunicationInterface
{
   public interface ICommunicationInterface
   {
        int bookID
        {
            get;
        }

        string bookName
        {
            get;
        }
   }
}

build this simple project with the varibles that you want to pass (int, string, bool) and build that project. Then get the dll and add it to the references in the provider and consumer class so you can access those varibles. Also add the dll to GAC :)

for mode detail on this refer to this guide:

http://msdn.microsoft.com/en-us/library/ms469765.aspx

hope it helps :)

0

After ages of googling i found an easier way for my webparts to communicate, you can use connectable webparts, to connect them but i didn't solve that, so i've removed my consumer webpart from the site (not code), and typed this code:

    protected void Button_Edit_Click(object sender, EventArgs e)
    {  
        if (ListBox_List.SelectedItem != null)
        {
            WP_ConsumerSearchListUserControl control = (WP_ConsumerSearchListUserControl)Page.LoadControl(@"~/_CONTROLTEMPLATES/Kingen.Artifacts.WebParts/WP_ConsumerSearchList/WP_ConsumerSearchListUserControl.ascx");
            control.GetProduct(ListBox_List.SelectedValue);
            Controls.Add(control);
        }
    }

Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.