Click here to Skip to main content
15,891,976 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Collections;

namespace AutoStatus
{

    
    public partial class Form1 : Form
    {
        
        bool AppAdded=false;
        SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;");

        public Form1()
        {
            InitializeComponent();
            
            getapp();
        }

        private void getapp()
        {
            SqlCommand cmd = new SqlCommand("SELECT app FROM mytable ", conn);
            this.conn.Open();
            SqlDataReader rs = cmd.ExecuteReader();
            ArrayList Authors = new ArrayList();

            while (rs.Read())
            {
                Authors.Add(new AddValue(rs.GetString(0)));

            }
            rs.Close();
            this.conn.Close();

            this.comboBox2.DataSource = Authors;
            this.comboBox2.DisplayMember = "app";

            AppAdded = true;
        }



        private void getcomponents()
        {
            this.comboBox3.Items.Clear();
           SqlCommand cmd = new SqlCommand("SELECT components FROM mytable WHERE app =" + this.comboBox2.SelectedValue, conn);
            this.conn.Open();
            SqlDataReader rs = cmd.ExecuteReader();
            while (rs.Read())
            {
                this.comboBox3.Items.Add(rs.GetString(2));
            }
            rs.Close();
            this.conn.Close();

        }

        /*private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.Items.Add(new ComboBoxItem("item1 text", "item1 value"));
            MessageBox.Show(((ComboBoxItem)comboBox1.Items[0]).Value);
            /*
            Dictionary comboSource = new Dictionary<string,>();
            comboSource.Add("1", "Sunday");
            comboBox2.Items.Add("weekdays");
            comboBox2.Items.Add("year");*/
        

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.AppAdded)
                getcomponents();
        }
    }

   

    public class AddValue
    {
        private string m_Display;
        
        public AddValue(string Display)
        {
            m_Display = Display;
            
        }
        public string Display
        {
            get { return m_Display; }
        }
      
    }
}
Posted
Updated 8-Oct-15 2:13am
v2
Comments
ZurdoDev 8-Oct-15 8:29am    
What is your question exactly? If ExecuteReader() throws an exception the error should be pretty clear.

Here is something that might help you

http://forums.asp.net/t/1097507.aspx[^]
 
Share this answer
 
1. What is the error that is thrown on that line of code?
2. With reference to the link MSDN Link below
MSDN SqlCommand.ExecuteReader Method ()
- You could catch all the different types of exceptions (Pokemon handling)
- This is not recommended if you are not going to do anything about the specific errors.
3. Replace
SqlDataReader rs = cmd.ExecuteReader();
with the following
try
            {
                SqlDataReader rs = cmd.ExecuteReader();
            }
            catch (InvalidCastException exception)
            {
                Debug.WriteLine(exception.Message)
            }
            catch (SqlException exception)
            {
                Debug.WriteLine(exception.Message)
            }
            catch (InvalidOperationException exception) // This handles ObjectDisposedException
            {
                Debug.WriteLine(exception.Message)
            }
            catch (IOException exception)
            {
                Debug.WriteLine(exception.Message)
            }
            catch (Exception exception)
            {
                Debug.WriteLine(exception.Message)
            }


4. Add this to the top of the page
using System.Diagnostics;


This will assist with determining what the exception type is and the error.
 
Share this answer
 

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