none
Combobox control return System.NullReferenceException: Object reference not set to an instance of an object RRS feed

  • Question

  • Hello Everyone

    I am using Visual Studio 2013 to work a project at school. But when i call to combobox to assign data, it always throw Null Exception before data will be assign

    namespace Bank_Management_System.GUI
    {
        public partial class frmStaff : Form
        {
            ControlFormNhanVien ctrNhanVien;
            ControlFormPhongGiaoDich ctrPGD = new ControlFormPhongGiaoDich();
            NhanVien m_NhanVien;
            private DataTable m_dataTable;
     
     
            public frmStaff()
            {
                LoadDeadlingRoom();
     
                InitializeComponent();
            }
     
            private void btnSave_Click(object sender, EventArgs e)
            {
                ctrNhanVien = new ControlFormNhanVien();
                m_NhanVien = new NhanVien(txtStaffCode.Text.Trim(), txtName.Text.Trim(), dtpBirthday.Value, txtAddresse.Text.Trim(), mtbPhone.Text.Trim(), cboDealingRoom.SelectedValue.ToString());
                ctrNhanVien.AddNewStaff(m_NhanVien);
            }
     
            private void LoadDeadlingRoom()
            {
                try
                {
                    m_dataTable = ctrPGD.GetAllDealingRoom();
                    if (m_dataTable.Rows.Count > 0)
                    {
                        if (cboDealingRoom.DataSource != null)//Exception is thrown at here
                        {
                            cboDealingRoom.DataSource = m_dataTable;
                            cboDealingRoom.DisplayMember = "TenPhong";
                            cboDealingRoom.ValueMember = "MaPGD";
                        }
                    }
                }
                catch (NullReferenceException nex)
                {
                    MessageBox.Show(nex.ToString());
                }
            }
        }
    }

    what happen with combobox control ???

    thanks in advance

    Monday, January 29, 2018 10:04 AM

Answers

  • Move the initial call to LoadDeadlingRoom from the form's new constructor to form load event or form shown event, see if that resolves the issue. Also, no code should be placed prior to InitializeComponent.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Monday, January 29, 2018 10:33 AM
    Moderator

All replies

  • So is the exception thrown is on DataSource and not cboDealingRoom control itself, is this correct? I ask this because the code is fine if you have added the ComboBox by placing one on the form from the IDE toolbox would not throw an exception yet if it was added via code w/o specifying new ComboBox() would throw an exception on the control itself, not the DataSource.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, January 29, 2018 10:12 AM
    Moderator
  • My combobox is drag from visual studio tool box and exception always throw before datasource is assigned.

    I try to work with code and it not work, too

    m_dataTable = ctrPGD.GetAllDealingRoom();                 if (m_dataTable.Rows.Count >0)                 {

    for(int i =0; i<m_dataTable.Count; i++)

    {

    cboDealingRoom.Items.Add(m_dataTable[i][TenPhong]);//exception is thrown here

    }

    }

     


    • Edited by Thanh98 Monday, January 29, 2018 10:31 AM
    Monday, January 29, 2018 10:25 AM
  • Move the initial call to LoadDeadlingRoom from the form's new constructor to form load event or form shown event, see if that resolves the issue. Also, no code should be placed prior to InitializeComponent.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Monday, January 29, 2018 10:33 AM
    Moderator
  • Yes, it works now. Thank you very much
    Monday, January 29, 2018 10:36 AM