Click here to Skip to main content
16,018,353 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I binded combox in windows form application
like this

C#
this.cmbPartyTypeType.DisplayMember = "Text";
this.cmbPartyTypeType.ValueMember = "Value";


this.cmbPartyTypeType.Items.Add(new { Text = "Select", Value = "0" });
this.cmbPartyTypeType.Items.Add(new { Text = "Agent", Value = "1" });


I added one selected index change event
Im getting an exception in this line
C#
if (cmbPartyTypeType.SelectedValue.ToString() == "1")


the exception is
"Object reference not set to an instance of an object."
Posted
Updated 24-Feb-14 4:32am
v2

1 solution

If you get this error on that line, convert it to:

C#
if(cmbPartyTypeType.SelectedValue != null && cmbPartyTypeType.SelectedValue.ToString() == "1")
...


But, much more convenient way is to use the debugger. Put a breakpoint (with F9) before where you suspect the error. Then run your application. When the debugger hits the breakpoint, go step by step by pressing F10.
By using the debugger, you can find your answer by yourself, and I assure you, you can find it faster compared to waiting for an answer from someone else.

[updated]

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Test_Cmb
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            InitOthers();
        }

        BindingSource bs = new BindingSource();

        private void InitOthers()
        {
            this.bs.DataSource = typeof(MyComboItem);
            this.bs.Add(new MyComboItem()
            {
                DisplayVal = "Select",
                SelectedVal = "0"
            });
            this.bs.Add(new MyComboItem()
            {
                DisplayVal = "Agent",
                SelectedVal = "1"
            });

            this.comboBox1.DisplayMember = "DisplayVal";
            this.comboBox1.ValueMember = "SelectedVal";
            this.comboBox1.SelectedValueChanged += comboBox1_SelectedValueChanged;

            this.comboBox1.DataSource = this.bs;
        }

        void comboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            this.textBox1.Text = this.comboBox1.SelectedValue == null ?
                "null value" : this.comboBox1.SelectedValue.ToString();

        }
    }

    public class MyComboItem
    {
        public string DisplayVal { get; set; }
        public string SelectedVal { get; set; }
    }
}
 
Share this answer
 
v3
Comments
Member 10296413 24-Feb-14 7:29am    
I'm geting the value feild always null
In the above code If I select Agent the value must be 1 but i'm getting Null
Vedat Ozan Oner 24-Feb-14 7:51am    
Ok. You can use BindingSource. I updated the solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900