0

I want to add data to datagridview from another form in c# but I got stuck in this error. I'm still practicing so i just got this code here in stackoverflow but I cant run the code. My code is like this

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ProgrammingLanguage5
{
    public partial class frmCreate : Form
    {
        private frmRetrieve frmRet;
        public frmCreate(frmRetrieve frmRet)
        {
            InitializeComponent();
            this.frmRet = frmRet;
        }
        public frmCreate()
        {
            InitializeComponent();
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            object[] row = new object[]{txtNo.Text,txtEnrollNumber.Text,txtStudentName.Text,txtStudentNumber.Text,
                txtSchoolYear.Text,txtSectionNumber.Text,txtLevel};
            frmRet.datagridview1.Rows.Add(row);
        }



    }
}

when i click the create button after inputing some data in the textboxes this error pops up NullReferenceException

I'm still a student so please don't get mad at me if I'm being so stupid. Thanks in advance.

  • If you build your frmCreate using the default constructor frmRet is null and you cannot use it in your button code – Steve Jan 23 '16 at 15:06
  • @Steve I been reading the link you gave but I still don't get it. I'm really sorry. I'm just new with c# and I'm just exploring some codes. Can you correct my code so that I could see what's wrong? That's if it is ok with you. Thanks – john paul fernando Jan 23 '16 at 15:41
  • _if(frmRet == null) return_ before executing anything that uses frmRet. In any case you should remove the parameterless constructor and check where your code calls that empty constructor. Then you should pass a valid reference to a form of type frmRetrieve – Steve Jan 23 '16 at 16:01
  • @Steve Thank you so much. Even though I really don't get the concept atleast I did understand some of the point your explaining. My program is working now without error. Thanks a lot. – john paul fernando Jan 23 '16 at 16:54

0 Answers0