Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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";
            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 21-Oct-12 20:46pm
Edited 21-Oct-12 23:24pm
v2
Comments
Sergey Alexandrovich Kryukov at 22-Oct-12 1:57am
   
Not enough information. How can you hope for the answer without showing any code sample?
--SA
ravuravu at 22-Oct-12 2:29am
   
hey please check my coding
Sergey Alexandrovich Kryukov at 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 at 22-Oct-12 2:08am
   
please see the below coding
ravuravu at 22-Oct-12 2:09am
   
solutio1 coding
ravuravu at 22-Oct-12 2:20am
   
please help me
Milind Thakkar at 22-Oct-12 2:29am
   
Try to debug step-by-step and let us know at which line it is throwing exception !
Daxa Rughani at 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 at 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
0 OriginalGriff 384
1 Maciej Los 290
2 Sergey Alexandrovich Kryukov 230
3 CPallini 170
4 Shweta N Mishra 165
0 OriginalGriff 7,760
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,785


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 22 Oct 2012
Copyright © CodeProject, 1999-2014
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