0

I am trying to insert data into database by using windows from application . I hosted it into console application . I am using .net remoting to invoke the method . My host is running without any problem and i also can run the windows form application without any problem . But problem is when i clicked the submit button to insert data i got error.I do not know why i am getting this error .

null reference exception.

Here is the Interface .

namespace IHelloRemotingService
{
    public interface IHelloRemotingService
    {

        void Insert(string Name, string Address, string Email, string Mobile)
    }


}

Here is the Implementation of the interface ..

public class HelloRemotingService : MarshalByRefObject , IHelloRemotingService.IHelloRemotingService
{
    public void Insert(string Name, string Address, string Email, string Mobile)
        {
            string constr = ConfigurationManager.ConnectionStrings["StudentConnectionString"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand("AddNewStudent", con))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Name", Name);
                    cmd.Parameters.AddWithValue("@Address", Address);
                    cmd.Parameters.AddWithValue("@EmailID", Email);
                    cmd.Parameters.AddWithValue("@Mobile", Mobile);
                    cmd.Connection = con;
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();

                }
            }
        }
    }
    }

Here is the screen shot of application .

enter image description here

Code for windows form application ..

namespace HelloRemotingServiceClient
{
    public partial class InsertStudentData : Form
    {

        IHelloRemotingService.IHelloRemotingService client;
        public InsertStudentData()
        {
            InitializeComponent();
            TcpChannel channel = new TcpChannel();
            ChannelServices.RegisterChannel(channel);
            client = (IHelloRemotingService.IHelloRemotingService)Activator.GetObject(
                typeof(IHelloRemotingService.IHelloRemotingService), "tcp://localhost:500/Insert");
        }



        private void button1_Click(object sender, EventArgs e)
        {


            client.Insert(textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text);

                label5.Text = "Data is inserted ";

        }


    }
}

Here is the screen shot of errors messages .

enter image description here

MickyD
  • 14,343
  • 6
  • 43
  • 67
Mohammad
  • 1,019
  • 8
  • 24
  • 1
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Ron Beyer Apr 13 '18 at 02:51
  • You are pretty close, just look in the window at the bottom (or mouse over a part of your code) and you can spot which aspect is `null` – MickyD Apr 13 '18 at 04:35
  • as you can see from the screen shot the text boxes are able to catch the values but why its throwing null reference exception ? – Mohammad Apr 14 '18 at 00:21
  • Has `client` been initialized? – Ian Aug 26 '18 at 00:20

0 Answers0