Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
2.30/5 (3 votes)
Dear All,

I want to select the item from the database through arraylist but the item not showing in the combobox. what is the error in my program please tell me.


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        
        public Form1()
        {
            InitializeComponent();
            cmboItemType();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }
        private void cmboItemType()    //for combobox 
        {
            string connstr = @"Server=.\SQLEXPRESS ;Initial Catalog=RPSJDB;Integrated Security=True; Max Pool Size=100";
            SqlConnection conn = new SqlConnection(connstr);
            SqlCommand cmd = new SqlCommand("SELECT cmbItemType FROM ItemTable", conn);
            conn.Open();
            SqlDataReader sdr = cmd.ExecuteReader();
            ArrayList ItemStore = new ArrayList();
            while (sdr.Read())
            {
                ItemStore.Add(sdr.ToString());
            }
            sdr.Close();
            conn.Close();
        }
}
}
        }
Posted
Comments
Sergey Alexandrovich Kryukov 7-Sep-13 1:31am    
There is no a single mention of the type ComboBox in this code sample. Don't you think it's weird to ask about ComboBox functionality problem if you don't show any?
—SA

C#
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        
        public Form1()
        {
            InitializeComponent();
            cmboItemType();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }
        private void cmboItemType()    //for combobox 
        {
            string connstr = @"Server=.\SQLEXPRESS ;Initial Catalog=RPSJDB;Integrated Security=True; Max Pool Size=100";
            SqlConnection conn = new SqlConnection(connstr);
            SqlCommand cmd = new SqlCommand("SELECT cmbItemType FROM ItemTable", conn);
            conn.Open();
            SqlDataReader sdr = cmd.ExecuteReader();
            ArrayList ItemStore = new ArrayList();
int i=0;
            while (sdr.Read())
            {

                ItemStore.Add(sdr[i].ToString());
i++;
            }
            sdr.Close();
            conn.Close();
        }
    }
}
}

Check this..............
http://stackoverflow.com/questions/256832/c-sharp-fill-a-combo-box-with-a-datatable[^]
 
Share this answer
 
v5
i am not sure but first check ur query in sql server that u have given in sqlCommand weather its returning values

if not check the column name and table name


if all this are right then check cataloge name in connection string means ur database name
 
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