Friday, November 9, 2012

How to over come this exception...

Unable To Evaluate Expression Because Code Is Optimized Or Native Frame Is On Top Of Stack

Just do this..

catch (System.Threading.ThreadAbortException)
{

}

Show Data in combobox like a Grid

public void ShowComboBox()
        {
            try
            {
                SqlConnection con = new SqlConnection("Data Source=ANONYMOUS;Initial Catalog=ABC;Integrated Security=True");
                con.Open();
                SqlCommand cmd = new SqlCommand("select * from employee_test", con);
                DataTable dt = new DataTable();
                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);
                comboBox1.DataSource = dt;
               
                comboBox1.DisplayMember = "emp_id";

                comboBox1.ValueMember = "emp_id";
                comboBox1.DrawMode = DrawMode.OwnerDrawFixed;
                comboBox1.DrawItem += delegate(object cmb, DrawItemEventArgs args)
                {
                    args.DrawBackground();
                    DataRowView drv = (DataRowView)comboBox1.Items[args.Index];

                    // Retrieve the value of each column.
                    string id = drv["emp_id"].ToString();
                    string name = drv["emp_name"].ToString();
                    string salary = drv["emp_salary"].ToString();
                   
                    // Get the bounds for the first column
                   
                    Rectangle r1 = args.Bounds;
                    r1.Width /= 3;

                    // Draw the text on the first column
                    using (SolidBrush sb = new SolidBrush(args.ForeColor))
                    {
                        args.Graphics.DrawString(id, args.Font, sb, r1);
                    }

                    // Draw a line to isolate the columns
                    using (Pen p = new Pen(Color.DarkBlue))
                    {
                        args.Graphics.DrawLine(p, r1.Right, 0, r1.Right, r1.Bottom);
                    }

                    // Get the bounds for the second column
                    Rectangle r2 = args.Bounds;
                    r2.X = args.Bounds.Width / 3;

                    r2.Width /= 2;

                    // Draw the text on the second column
                    using (SolidBrush sb = new SolidBrush(args.ForeColor))
                    {
                        args.Graphics.DrawString(name, args.Font, sb, r2);
                    }
                    using (Pen p = new Pen(Color.DarkBlue))
                    {
                        args.Graphics.DrawLine(p, r2.Right, 0, r2.Right, r2.Bottom);
                        //args.Graphics.DrawLine(p, r2.Left,0, r2.Left, r2.Bottom);
                    }


                    Rectangle r3 = args.Bounds;
                    r3.X = (args.Bounds.Width * 2) / 3;
                    r3.Width = (r3.Width) / 2;

                    // Draw the text on the first column
                    using (SolidBrush sb = new SolidBrush(args.ForeColor))
                    {
                        args.Graphics.DrawString(salary, args.Font, sb, r3);
                    }

                };

            }
            catch (Exception ex)
            {
                MessageBox.Show("Error : " + ex);
            }
        }