.NET Remoting Part Four

In this session you will learn how to Insert data into sql database table by invoking .NET Remoting service .Here is the from design look like ..



Go to the design part of this form and change this piece of code ..

   protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                foreach (IChannel channel in ChannelServices.RegisteredChannels)
                {
                    try
                    {
                        ChannelServices.UnregisterChannel(channel);
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine("Client.Dispose exception: " + ex);
                    }
                }

                if (components != null)
                    components.Dispose();
            }
            base.Dispose(disposing);
        }

Double click the submit button i generate event handler and copy ,paste following codes ..

 namespace StudentRemotingServiceClient
{
    public partial class InsertStudentData : Form
    {
        public InsertStudentData()
        {
            InitializeComponent();
            RegisterBinaryTcpClientChannel();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                var remService = (StudentRemotingService.IStudentRemotingService)Activator.GetObject(typeof(StudentRemotingService.IStudentRemotingService), "tcp://localhost:500/Insert.rem");
                remService.Insert(textName.Text, textAddress.Text, textEmail.Text, textBox1.Text);
                label5.Text = "Recored Is inserted";
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void RegisterBinaryTcpClientChannel(string name = "tcp client")
        {
            IClientChannelSinkProvider firstClientProvider;
            IServerChannelSinkProvider firstServerProvider;

            var channelProperties = new Hashtable();
            channelProperties["name"] = name;
            channelProperties["typeFilterLevel"] = TypeFilterLevel.Full;
            channelProperties["machineName"] = Environment.MachineName;
            channelProperties["port"] = 0; // auto

            // create client format provider
            var clientProperties = new Hashtable();
            clientProperties["typeFilterLevel"] = TypeFilterLevel.Full;
            var clientFormatProvider = new BinaryClientFormatterSinkProvider(clientProperties, null);
            firstClientProvider = clientFormatProvider;

            // create server format provider
            var serverFormatProvider = new BinaryServerFormatterSinkProvider(null, null);
            serverFormatProvider.TypeFilterLevel = TypeFilterLevel.Full;
            firstServerProvider = serverFormatProvider;

            TcpChannel tcp = new TcpChannel(channelProperties, firstClientProvider, firstServerProvider);
            ChannelServices.RegisterChannel(tcp, false);
        }
    }
}

If you done everything correctly you will be able to insert the data when submit button is clicked before that make sure the host is running .

No comments:

Post a Comment