Windowsapplication1.form1.fillcombobox--system.NullReference.Exception

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sivakrishna546
    New Member
    • Sep 2008
    • 4

    Windowsapplication1.form1.fillcombobox--system.NullReference.Exception

    hai, when i working with windows apllication i got following runtime error:
    Object reference is not to set an instance of object :
    at cmb_eno.Items.A dd(dr[0]).can any one help me?


    Code:
    namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
            SqlConnection con;
            SqlDataAdapter da;
            DataSet ds;
            DataTable dt;
            public Form1()
            {
                con = new SqlConnection();
                con.ConnectionString = " Data Source=SIVA-496EA1B239\\SQLEXPRESS;Initial Catalog=Inventory;Persist Security Info=True;User ID=sa;Password=abcd1234";
    
                da = new SqlDataAdapter("select * from emp", con);
                ds = new DataSet();
                da.Fill(ds, "emp");
               dt = ds.Tables[0];
                fillcombobox();         
                InitializeComponent();
            }
             void fillcombobox()
            {
               
                DataRow[] drs = dt.Select(null);
                foreach (DataRow dr in drs)
                {
    
                 cmb_eno.Items.Add(dr[0].ToString());
             
                }
            }
    
            public void bt_get_Click(object sender, EventArgs e)
            {
                
                DataRow[] drs = dt.Select(null);
                string str = " ";
                foreach (DataRow dr in drs)
                {
                    str += dr[0] + "\t";
                    str += dr[1] + "\t";
                    str += dr[2] + "\n";
    
                }
                MessageBox.Show(str);
            }
    
            private void eno_cmb_SelectedIndexChanged(object sender, EventArgs e)
            {
                DataRow[] drs = dt.Select("Empno = " + eno_cmb.Text);
                DataRow dr =  drs[0];
                ename_txt.Text= dr["empname"].ToString();
                esalary_txt.Text = dr["empsalary"].ToString();
            
                
            }
            
    
        }
        
    }
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    Try using a messagebox to display the value and debug from there.

    Comment

    Working...