Click here to Skip to main content
15,886,748 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This is my code:
C#
private void cbCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
string cat = cbCategory.Text;

            SqlConnection con = new SqlConnection();
            con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\New.mdf;Integrated Security=True;User Instance=True";
            con.Open();
            SqlCommand cmd = new SqlCommand("select Category From tblItem where Category='" + cat + "'", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                cat1 = dt.Rows[0][0].ToString();
            }
            con.Close();
            if (cat == cat1)
            {
                //string cat = cbCategory.Text;
                SqlConnection conn = new SqlConnection();
                conn.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\New.mdf;Integrated Security=True;User Instance=True";
                conn.Open();
                SqlCommand cmd1 = new SqlCommand("select sum(quantity) from tblItem where Category='" + cat + "'", conn);
                SqlDataAdapter daaa = new SqlDataAdapter(cmd1);
                DataTable dt1 = new DataTable();
                daaa.Fill(dt1);
                if (dt1.Rows.Count > 0)
                {
                    quantit = Convert.ToInt32(dt1.Rows[0][0].ToString());
                }
                conn.Close();
                textBox1.Text = Convert.ToString(quantit);

                SqlConnection con4 = new SqlConnection();
                con4.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\New.mdf;Integrated Security=True;User Instance=True";
                con4.Open();
                SqlCommand cmd4 = new SqlCommand("select Category From tblSalesEntry where Category='" + cat + "'", con4);
                SqlDataAdapter da4 = new SqlDataAdapter(cmd4);
                DataTable dt4 = new DataTable();
                da4.Fill(dt4);
                if (dt4.Rows.Count > 0)
                {
                    cat1 = dt4.Rows[0][0].ToString();
                }
                con4.Close();
                if (cat == cat1)
                {
                    SqlConnection con1 = new SqlConnection();
                    con1.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\New.mdf;Integrated Security=True;User Instance=True";
                    con1.Open();
                    SqlCommand cmdd = new SqlCommand("select sum(Quantity) from tblSalesEntry where Category='" + cat + "'", con1);
                    SqlDataAdapter daa = new SqlDataAdapter(cmdd);
                    DataTable dtt = new DataTable();
                    daa.Fill(dtt);
                    if (dtt.Rows.Count > 0)
                    {
                        quanti = Convert.ToInt32(dtt.Rows[0][0].ToString());
                    }
                    con1.Close();
                }
                /*else
                {
                    MessageBox.Show("THIS CATEGORY IS NOT SALED");
                }*/

                int stoc = quantit - quanti;
                textBox1.Text = Convert.ToString(stoc);
                
            }
            else
            {
                MessageBox.Show("CATEGORY DOES NOT EXIST IN TABLE");
                this.Close();
                
            }
}


It throws this exception:
System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'ComboBox'.
   at System.Windows.Forms.Control.CreateHandle()
   at System.Windows.Forms.ComboBox.CreateHandle()
   at System.Windows.Forms.Control.get_Handle()
   at System.Windows.Forms.Control.SendMessage(Int32 msg, Int32 wparam, Int32 lparam)
   at System.Windows.Forms.Control.EndUpdateInternal(Boolean invalidate)
   at System.Windows.Forms.ComboBox.EndUpdate()
   at System.Windows.Forms.ComboBox.OnDataSourceChanged(EventArgs e)
   at System.Windows.Forms.ListControl.SetDataConnection(Object newDataSource, BindingMemberInfo newDisplayMember, Boolean force)
   at System.Windows.Forms.ListControl.DataSourceDisposed(Object sender, EventArgs e)
   at System.ComponentModel.Component.Dispose(Boolean disposing)
   at System.Windows.Forms.BindingSource.Dispose(Boolean disposing)
   at System.ComponentModel.Component.Dispose()
   at System.ComponentModel.Container.Dispose(Boolean disposing)
   at System.ComponentModel.Container.Dispose()
   at Bill.StockList.Dispose(Boolean disposing) in C:\Users\Ravu\Desktop\Bill\Bill\StockList.Designer.cs:line 18
   at System.ComponentModel.Component.Dispose()
   at System.Windows.Forms.Form.WmClose(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
Posted
Updated 21-Oct-12 22:24pm
v2
Comments
Sergey Alexandrovich Kryukov 22-Oct-12 1:57am    
Not enough information. How can you hope for the answer without showing any code sample?
--SA
ravuravu 22-Oct-12 2:29am    
hey please check my coding
Sergey Alexandrovich Kryukov 22-Oct-12 11:18am    
Now, remaining part is to bind that two pieces together: indicate in what line of code this exception is thrown.
Excuse me, but... isn't this obvious?
--SA
ravuravu 22-Oct-12 2:08am    
please see the below coding
ravuravu 22-Oct-12 2:09am    
solutio1 coding

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