Click here to Skip to main content
13,050,450 members (78,329 online)
Rate this:
Please Sign up or sign in to vote.
See more:
This is my code:
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";
            SqlCommand cmd = new SqlCommand("select Category From tblItem where Category='" + cat + "'", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            if (dt.Rows.Count > 0)
                cat1 = dt.Rows[0][0].ToString();
            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";
                SqlCommand cmd1 = new SqlCommand("select sum(quantity) from tblItem where Category='" + cat + "'", conn);
                SqlDataAdapter daaa = new SqlDataAdapter(cmd1);
                DataTable dt1 = new DataTable();
                if (dt1.Rows.Count > 0)
                    quantit = Convert.ToInt32(dt1.Rows[0][0].ToString());
                textBox1.Text = Convert.ToString(quantit);
                SqlConnection con4 = new SqlConnection();
                con4.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\New.mdf;Integrated Security=True;User Instance=True";
                SqlCommand cmd4 = new SqlCommand("select Category From tblSalesEntry where Category='" + cat + "'", con4);
                SqlDataAdapter da4 = new SqlDataAdapter(cmd4);
                DataTable dt4 = new DataTable();
                if (dt4.Rows.Count > 0)
                    cat1 = dt4.Rows[0][0].ToString();
                if (cat == cat1)
                    SqlConnection con1 = new SqlConnection();
                    con1.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\New.mdf;Integrated Security=True;User Instance=True";
                    SqlCommand cmdd = new SqlCommand("select sum(Quantity) from tblSalesEntry where Category='" + cat + "'", con1);
                    SqlDataAdapter daa = new SqlDataAdapter(cmdd);
                    DataTable dtt = new DataTable();
                    if (dtt.Rows.Count > 0)
                        quanti = Convert.ToInt32(dtt.Rows[0][0].ToString());
                    MessageBox.Show("THIS CATEGORY IS NOT SALED");
                int stoc = quantit - quanti;
                textBox1.Text = Convert.ToString(stoc);
                MessageBox.Show("CATEGORY DOES NOT EXIST IN TABLE");

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 21-Oct-12 19:46pm
Updated 21-Oct-12 22:24pm
Not enough information. How can you hope for the answer without showing any code sample?
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?
ravuravu 22-Oct-12 2:08am
please see the below coding
ravuravu 22-Oct-12 2:09am
solutio1 coding
ravuravu 22-Oct-12 2:20am
please help me
Milind Thakkar 22-Oct-12 2:29am
Try to debug step-by-step and let us know at which line it is throwing exception !
Daxa Rughani 23-Oct-12 15:56pm
Some pointers:-
1. Check SelectedIndex is Valid on entry
2. What does this.Close() after the Message.Show.... DO!
geekbond 28-Oct-12 5:56am
What does your form do after this.Close(); You should put breakpoints in your code to determine the line where the error is thrown. We cannot see your screen.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 22 Oct 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100