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
public interface IProductSearch { string ProductNumber { get; } string Name { get; } decimal ListPrice { get; } string MoreInformation { get; } string Color { get; } }