Quantcast
Channel: SharePoint 2010 - Development and Programming forum
Viewing all articles
Browse latest Browse all 11571

Connectable WebParts

$
0
0

Hi..

        I Created 2 Custom Web Parts and Connecting them and in that we are using Checkbox list in Consumer based on the selection made from DropDownlist in Provider..but if we remove Checkbox list, its working fine  and if i uncomment the code for Checkboxlist we are getting error...

Sending the Code:-

Provider

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace Sample.EmployeeInfo
{
    [ToolboxItemAttribute(false)]
    public class EmployeeInfo : WebPart, IEmployee
    {
        Label _lblEmployee = null;
        DropDownList _ddlEmployee = null;
        Button _btnOK = null; 

        protected override void CreateChildControls()
        {
            try
            {
                _lblEmployee = new Label();
                _lblEmployee.Text = "Employee";

                _ddlEmployee = new DropDownList();

                string connectionString = @"Data Source=SP2010;Initial Catalog=Sample;User ID=sa;Password=2sritcs@sa";
                SqlConnection sqlCon = new SqlConnection(connectionString);
                sqlCon.Open();
                SqlDataAdapter adapter = new SqlDataAdapter("Select EmpId, CAST(EmpId as varchar(5)) + '  ' + EmpName as Emp from Employee", sqlCon);
                DataSet ds = new DataSet();
                adapter.Fill(ds);

                _ddlEmployee.DataSource = ds;
                _ddlEmployee.DataTextField = "Emp";
                _ddlEmployee.DataValueField = "EmpId";
                _ddlEmployee.DataBind();

                
                _btnOK = new Button();
                _btnOK.Text = "Submit";

                this.Controls.Add(_lblEmployee);


                this.Controls.Add(_ddlEmployee);
                this.Controls.Add(new LiteralControl("<br />"));
                this.Controls.Add(_btnOK);

            }
            catch (Exception ex)
            {
                this.Controls.Clear();
                this.Controls.Add(new LiteralControl("Error: " + ex.Message));
            }
        }

        public string EmployeeId
        {
            get { return _ddlEmployee.SelectedItem.Value; }
        }

        [ConnectionProvider("Get Data")]
        public IEmployee ConnProviderMethod()
        {
            // This method will expose the shared data to the Consumer.

            return this;


        }
    }
}

Consumer

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace Sample.Technology
{
    [ToolboxItemAttribute(false)]
    public class Technology : WebPart
    {
        Label _lbl = null;
        IEmployee _employee = null;
        CheckBoxList _chklist = null;

        //Receive the data from the Provider web part here
        [ConnectionConsumer("EmployeeId")]

        public void ConsumerMethod(IEmployee employee)
        {
            _employee = employee;
        }

        protected override void CreateChildControls()
        {
            try
            {
                _lbl = new Label();




                _chklist = new CheckBoxList();
                //_chklist.Items.Clear();
                _chklist.ID = "chklist";
                _chklist.AutoPostBack = false;

                string connectionString = @"Data Source=SP2010;Initial Catalog=Sample;User ID=sa;Password=2sritcs@sa";
                SqlConnection sqlCon = new SqlConnection(connectionString);
                sqlCon.Open();
                SqlDataAdapter adapter = new SqlDataAdapter("Select ID,Technology from Technology", sqlCon);
                DataSet ds = new DataSet();
                adapter.Fill(ds);

                if (ds.Tables != null)
                {
                    _chklist.DataSource = ds;
                    _chklist.DataTextField = "Technology";
                    _chklist.DataValueField = "ID";
                    _chklist.DataBind();
                }

                
                // Here we utilize the data received from the Provider

                // In this simplied example, we are just displaying the received data.

                // However, you can take this further

                // query databases/SP Lists/other manipulations of the data

                // and provide an enriched result back to the user!

                if (_employee != null)
                {

                    if (_employee.EmployeeId != "")
                    {
                        _lbl.Text = "fg" + _employee.EmployeeId;

                        //adapter = new SqlDataAdapter("select Technology from Technology as t Inner Join  Employee e  on t.ID = e.TechnologyId where EmpId=" + _employee.EmployeeId, sqlCon);
                        //ds = new DataSet();
                        //adapter.Fill(ds);

                        //foreach (ListItem li in _chklist.Items)
                        //{
                        //    if (li.Text == Convert.ToString(ds.Tables[0].Rows[0][0]))
                        //    {
                        //        li.Selected = true;
                        //    }
                        //}

                    }
                }

                else

                    _lbl.Text = "No provider connected";

                this.Controls.Add(_lbl);
               this.Controls.Add(_chklist);

            }

            catch (Exception ex)
            {

                this.Controls.Clear();

                this.Controls.Add(new LiteralControl("Error: " + ex.Message));

            }

        }


    }
}

Interface

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

namespace Sample
{
    public interface IEmployee
    {
        string EmployeeId { get; }
    }
}


Ravindranath


Viewing all articles
Browse latest Browse all 11571

Trending Articles