I am trying to build a WebPart using C# that passes the selected value from one WebPart to another WebPart. To do this I am using a Public interface to get the values.

When I try to compile the code it returns the following error...

"A namespace cannot directly contain members such as fields or methods"

Not sure how to expose the interface to the other calling provider/consumer classes without using this. Suggestions?

See each of the classes below for more information...

+++++++++ICustomer.cs++++++++++

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

namespace ProjectCatalog
{
    Public interface ICustomer
    {
        int ID { get; }
        string Name { get; }
    }
}

++++++++ProvidedrCustomer.cs++++++++

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;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProjectCatalog.ProviderCustomer
{
    [ToolboxItemAttribute(false)]
    public class ProviderCustomer : Microsoft.SharePoint.WebPartPages.WebPart, ICustomer
    {
        private string connectionString = ConfigurationSettings.AppSettings["ConnectionString"];

        DropDownList _CustomerPicker = null;
        int ICustomer.Id
        {
            get { return int.Parse(_CustomerPicker.SelectedValue); }
        }

        string ICustomer.Name
        {
            get { return _CustomerPicker.SelectedItem.ToString(); }
        }

        protected override void CreateChildControls()
        {  
        

            _CustomerPicker = new DropDownList();
            string sSQL = "select * from ALI_CustomerProject";

            SqlConnection con = new SqlConnection(connectionString);
            SqlCommand cmd = new SqlCommand(sSQL, con);
            SqlDataReader reader;

            try
            {
                con.Open();
                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    ListItem newItem = new ListItem();
                    newItem.Text = "(" + reader["custid"] + ")" + reader["name"];
                    newItem.Value = reader["custnum"].ToString();
                    _CustomerPicker.Items.Add(newItem);
                }
                reader.Close();
            }
            catch (Exception ex)
            {
                this.Controls.Clear();
                this.Controls.Add(new LiteralControl(ex.Message));
            }
            finally
            {
                con.Close();
            }
        }

        [ConnectionProvider("Customer Name and ID")]
        public ICustomer NameDoesNotMatter()
        {
            return this;
        }

    }

}

+++++++++++CustomerProjects.cs++++++++++++++++++++++++++++

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;

namespace ProjectCatalog.CustomerProject
{
    [ToolboxItemAttribute(false)]
    public class CustomerProject : WebPart
    {
        ICustomer _provider = null;
        Label _lbl = null;

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

            try
            {
                if (_provider != null)
                {
                    if (_provider.ID > 0)
                    {
                        _lbl.Text = _provider.Name + "was selected";
                    }
                    else
                    {
                        _lbl.Text = "Nothing was selected.";
                    }
                }
                else
                {
                    _lbl.Text = "No Provider Web Part Connected.";
                }
                this.Controls.Add(_lbl);
            }
            catch (Exception ex)
            {
                this.Controls.Clear();
                this.Controls.Add(new LiteralControl(ex.Message));
            }

        }
        [ConnectionConsumer("Project Name and ID")]
        public void ThisNameDoesNotMatter(ICustomer providerInterface)
        {
            _provider = providerInterface;
        }


    }
}

Recommended Answers

All 3 Replies

Use code tags, that's impossible to read.

public in Customer.cs should be lower-case. Not sure if this is your problem, but when you get an error in just about any IDE it gives you a filename, and a line number. Why wouldn't you post this as well?

Check the properties of your interface code file.
make sure the build action is set to compile.

and lowercase for 'public' too, as already suggested

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.