Click here to Skip to main content
15,912,205 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hello there,
I want to use generic list in my project instead of array.can anyone do modification in my code.Here is my code.
C#
ClsConnection.Conn.Open();
               SqlCommand cmd1 = new SqlCommand("SELECT * FROM gen_Master", ClsConnection.Conn);
               //SqlCommand cmd1 = new SqlCommand("SELECT * FROM gen_Master", con);
               SqlDataReader dr1 = cmd1.ExecuteReader();

               //comboBox2.Items.Clear();
               int cnt1 = 0;
               string[,] arr1 = new string[100, 100];
               ArrayList subjects1 = new ArrayList();
               subjects1.Add(new AddValue("select ", 0));
               while (dr1.Read())
               {
                   arr1[cnt1, 0] = dr1[0].ToString();
                   arr1[cnt1, 1] = dr1[1].ToString();
                   subjects1.Add(new AddValue(arr1[cnt1, 1].ToString(), Convert.ToInt32(arr1[cnt1, 0])));
                   cnt1++;
               }
               comboBox2.DataSource = subjects1;
               // dataGridView1.DataSource = subjects1;
               this.comboBox2.DisplayMember = "Display";
               this.comboBox2.ValueMember = "Value";
               if (comboBox2.Items.Count == 2)
               {
                   comboBox2.Text = "";
                   comboBox2.SelectedText = (arr1[0, 2]);
               }
               comboBox2.Refresh();
               ClsConnection.Conn.Close();

Please help.
Thanks
Posted
Updated 5-Feb-12 19:06pm
v2
Comments
Andreas Gieriet 6-Feb-12 3:04am    
The 2-dim array seems to me an overkill. See Solution #3 for more details.

Cheers

Andi
BillWoodruff 6-Feb-12 19:13pm    
Without your showing us what the code for 'AddValue is (must be a Class ?), can't make sense of this code. Since SQL has an 'addValue operator, you might consider renaming it in this context.

This is an example of a question where a brief example of the data coming in would be as valuable, or more valuable, than just dumping a bunch of incomplete code.

For example, if the first item in your (apparently) pairs of data is always a unique value, it could be a key in a Dictionary, and the solution proposed by Abhinav S, below, could be quite relevant.

You can implement the functionality of the two-dimensional array of strings using the list System.Collections.Generic.List<System.Collections.Generic.List<string>>.

However, this type will be more flexible then string[,]. First of, all, it will represent "jagged array", more like string[][] where each instance of an inner array could be of a different length, but you can also easily insert and delete elements.

See http://en.wikipedia.org/wiki/Jagged_array[^].

—SA
 
Share this answer
 
Comments
Abhinav S 6-Feb-12 0:59am    
My 5.
I got univoted by a platinum member. Don't know why.
Sergey Alexandrovich Kryukov 6-Feb-12 1:28am    
Good question. That was me; I needed a break and did not get time to comment it so far; will do it. If you fix you answer, I would gladly re-vote.

Thank you.
--SA
SantoshRohinSantosh 6-Feb-12 1:16am    
Can you edit in my code I am that perfect sir.
Sergey Alexandrovich Kryukov 6-Feb-12 1:38am    
No I cannot, sorry. This is not how this is can be done. First, it would take too much time for a Quick Questions & Answers. In interesting case, I uses to spent more time though, but this is not the case. You can redo your code by yourself.

More importantly, you did not explain us your ultimate goals, what the code supposed to do, what's on input, what's expected on output, and why. Given that, it's really much better and faster if you write your code (you can use "Improve question") and ask a follow-up question if you face further problems.

Anyway, I answered your question about the use of the list.
--SA
Espen Harlinn 6-Feb-12 8:49am    
5'ed!
As far as I can see in this code snippet, the string[,] arr1 is a complete overkill: you only use [...,0], [...,1], and [...,2].

In addition, the [...,2] is never assigned, but you use it in setting the selection - won't work neither.

Forget about this two-dimensional array and use plain simple instances for the currently read items. You might remember the first for selection, but this could be done by simply select the first element in the combo box.

[EDIT]
Now that you have tired your version, here a possible approach:

C#
List<Tuple<string, int>> data = new List<Tuple<string, int>>()
                  { new Tuple<string, int>("select", 0) };
...
var conn = ClsConnection.Conn;
using (SqlCommand cmd = new SqlCommand("SELECT * FROM gen_Master", conn))
using (SqlDataReader dr = cmd.ExecuteReader())
{
    while (dr.Read())
    {
        data.Add(new Tuple<string, int>(dr[1].ToString(),
                                        int.Parse(dr[0].ToString())));
    }
}
comboBox1.DataSource = data;
comboBox1.DisplayMember = "Item1";
comboBox1.ValueMember = "Item2";
comboBox1.SelectedIndex = comboBox1.Items.Count > 1 ? 1 : 0;


Cheers

Andi
 
Share this answer
 
v3
Comments
BillWoodruff 6-Feb-12 19:17pm    
+5 for creativity in using 'Tuple !
Andreas Gieriet 7-Feb-12 2:04am    
Thanks for your 5!
Cheers
Andi
if you coding with .Net3.5(or higher),how about using Linq?

C#
using System.Linq;
using System.Collections.Generic;
//...
ClsConnection.Conn.Open();
SqlCommand cmd1 = new SqlCommand("SELECT * FROM gen_Master", clsConnection.Conn);
DataTable dt = new DataTable();
dt.Load(cmd1.ExecuteReader());
List<AddValue> datas = new List<AddValue>();
datas.Add(new AddValue("Select", 0));
datas.AddRange(dt.AsEnumerable().Select(x => new AddValue(x[0].ToString(), Convert.ToInt32(x[1]))));
comboBox2.DataSource = datas;


othrewise

C#
ClsConnection.Conn.Open();
SqlCommand cmd1 = new SqlCommand("SELECT * FROM gen_Master", ClsConnection.Conn);
SqlDataReader dr1 = cmd1.ExecuteReader();
//int cnt1 = 0;
//string[,] arr1 = new string[100, 100];
//ArrayList subjects1 = new ArrayList();
//instead of ArrayList
List<addvalue> subjects1 = new List<addvalue>();
subjects1.Add(new AddValue("select ", 0));
while (dr1.Read())
{
    //arr1[cnt1, 0] = dr1[0].ToString();
    //arr1[cnt1, 1] = dr1[1].ToString();
    //subjects1.Add(new AddValue(arr1[cnt1, 1].ToString(), Convert.ToInt32(arr1[cnt1, 0])));
    subjects1.Add(new AddValue(dr1[1].ToString(), Convert.ToInt32(dr1[0].ToString())));
    //cnt1++;
}
comboBox2.DataSource = subjects1;
 
Share this answer
 
v2
You cannot use a List directly here because a list can contain only one value while you are using a multidimensioal array.
If you are getting unique values in the first array column, you can use a dictionary.
Dictionary<string,int> arr1 = new Dictionary<string,int>();
whi
 
Share this answer
 
v3
Comments
SantoshRohinSantosh 6-Feb-12 0:47am    
Thanks Abhinav. I want to use list or any other option then array because now it is working fine because the data is limited in database but when data will increase in database then array will not be an good option.So can you suggest me any other option?
Sergey Alexandrovich Kryukov 6-Feb-12 1:34am    
Abhinav,

You've written non existing class List<string, int>. Did you mean "dictionary"? But why the string is key and why int is value. OP did not say anything about uniqueness by string key. At least you need to explain it. The text is incomplete.

Also, your first statement is wrong. In my answer, I explained how to use list of lists to represent 2D array. Isn't it "directly enough"?

So far, the question is absolutely confusing. I don't think you can repair it, but if you replace this answer with something good, I'll gladly re-vote. Or -- I don't know -- remove it...

--SA
Abhinav S 6-Feb-12 10:45am    
Oh so sorry. I meant Dictionary. I wrote dictionary in my answer but forgot to change the code.
Whoops. My mistake. A well-deserved one. None the less - I will fix my answer now.
SantoshRohinSantosh 6-Feb-12 2:31am    
Okay thank you for the explanation. I'l try by self.
Abhinav S 6-Feb-12 10:47am    
Please try Dictionary and not List. List was written by mistake.
If you're just using it for data binding, read it into a DataSet with a DataAdapter. Look up SqlDataAdapter and you'll find many examples of how to read a table into a C# program.
 
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