Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hallo I have added 2 classes to my project witch use array list i would like to display those array lists in an listbox on my form here are my classes


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.Sql;
using System.Collections;
using System.Data.SqlTypes;

namespace SqlServer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
           public class SqlServerList : IComparable, ICloneable
        {
            public SqlServerList()
            {
                ServerName = string.Empty;
                InstanceName = string.Empty;
                IsClustered = string.Empty;
                Version = string.Empty;
            }

            #region ICloneable Members

            public object Clone()
            {
                try
                {
                    if (this == null)
                    {
                        return null;
                    }
                    SqlServerList SqlSL = new SqlServerList { ServerName = ServerName, InstanceName = InstanceName, IsClustered = IsClustered, Version = Version };
                    return SqlSL;
                }
                catch
                {
                    throw new NotImplementedException();
                }
            }

            #endregion
           
            #region IComparable Members

            public int CompareTo(object obj)
            {
                try
                {
                    if (!(obj is SqlServerList))
                    {
                        throw new Exception("obj is not an instance of SqlServerList");
                    }
                    if (this == null)
                    {
                        return -1;
                    }
                    return ServerName.CompareTo((obj as SqlServerList).ServerName);
                }
                catch
                {
                    throw new NotImplementedException();
                }
            }

            #endregion

            public string ServerName { get; set; }
            public string InstanceName { get; set; }
            public string IsClustered { get; set; }
            public string Version { get; set; }
        }
         public class GetInstance
        {
            public static ArrayList GetInstanceName()
            {
                try
                {
                    SqlServerList SqlSL = new SqlServerList();
                    SqlDataSourceEnumerator instance = SqlDataSourceEnumerator.Instance;
                    DataTable table = instance.GetDataSources();
                    ArrayList list = new ArrayList();
                    foreach (DataRow row in table.Rows)
                    {
                        SqlSL = new SqlServerList();
                        SqlSL.ServerName = row[0].ToString();
                        SqlSL.InstanceName = row[1].ToString();
                        SqlSL.IsClustered = row[2].ToString();
                        SqlSL.Version = row[3].ToString();
                        list.Add(SqlSL);
                        
                    }
                    return list;
                }
                catch
                {
                    return null;
                }

            }

        }

        private void button1_Click(object sender, EventArgs e)
        {

            listBox1.DataSource = GetInstance.GetInstanceName();        
            

        }
    }
}
Posted
Updated 27-Aug-12 5:03am
v5
Comments
[no name] 27-Aug-12 6:51am    
"i would like to display"... okay so go ahead and do that... what is the problem?
Sandeep Mewara 27-Aug-12 6:58am    
And the issue is?
Kenneth Haugland 27-Aug-12 6:58am    
How about ListView.ItemsSource = MyArray ?
mrDivan 27-Aug-12 7:39am    
How would I display it in an list view
mrDivan 27-Aug-12 8:13am    
listBox2.DataSource = AllInstanceSqlserver; this is what i did but it does not add the items in the list box

1 solution

Why your created two identical classes?
XML
listBox.DataSource = GetInstance.GetInstanceName();
listBox.DataMember = "ServerName";


Hope it helps in any way.
 
Share this answer
 
v2
Comments
mrDivan 27-Aug-12 10:55am    
thanks i tried it the problem is it only returns SqlServer.Form1 + sqlserverlist
mrDivan 27-Aug-12 10:57am    
i have updated the code
Thanks
Christian Amado 27-Aug-12 11:05am    
You need to concatenate values in order to bring Server list details.
mrDivan 30-Aug-12 2:49am    
Ok thank you

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