Click here to Skip to main content
15,919,341 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have filled one dataset from sql query and that dataset returns the below values.

ID CID
3 7
8 466
23 566
11 700

Now I want to modify CID and after that ID and CID should be bind with Dropdown. Datavaluefield of dropdown would be the below ID's and Datatextfield would be below CID's.

ID CID
3 C0007
8 C0466
23 C0566
11 C0700

I have written the below code but it is not working fine as please check loop may be some issue in loop. Please cross check the below code and help asap.

C#
for (int i = 0; i < dset.Tables["Table"].Rows.Count; i++)
                {
                    foreach (DataRow myRow in dset.Tables["Table"].Rows)
                    {
                        int CCid;
                        CCid = (int)myRow["CID"];

                        string str = String.Format("C{0:0000}", CCid);

                        DataTable table = new DataTable("MyTable");

                        table.Columns.Add("id", typeof(int));
                        table.Columns.Add("name", typeof(string));

                        string[] names = { str };

                        DataRow dr;

                        for (int j = 0; j < dset.Tables["Table"].Rows.Count; j++)
                        {
                            dr = table.NewRow();

                            dr["id"] = j + 1;

                            dr["name"] = Convert.ToString(names[str[0]]);

                            table.Rows.Add(dr);
                        }

                        DataSet ds = new DataSet();

                        ds.Tables.Add(table);

                        ddlContract.DataSource = ds.Tables["MyTable"];
                        ddlContract.DataTextField = "name";
                        ddlContract.DataValueField = "id";
                        ddlContract.DataBind();
                    }
                }
Posted
Updated 1-Jun-12 0:10am
v4
Comments
Nelek 31-May-12 19:38pm    
Code tags added.

You have described what you want to do but... What are the problems? A bit more info would be fine: "not working fine" is not very descriptive.

Please use "improve question" and give a brief description of the issues
vibsg 1-Jun-12 5:13am    
Drop down is not binding with CID .t his is the issue. please help.

Hi are you using the comboBox? If so, try the following code..

C#
ComboBox1.DisplayMember = "name";
ComboBox1.ValueMember = "id";
ComboBox1.DataSource =  ds.Tables["MyTable"];
ComboBox1.Databind();
 
Share this answer
 
v2
Comments
vibsg 1-Jun-12 5:14am    
No, I am usinf dropdown as this above code in c#. thanks.
bhaskarareddy 1-Jun-12 6:26am    
this code is useful for win form application not for web application.
Why do you need three loops? You have loops within loops which is creating major problems for you...

C#
DataTable table = new DataTable("MyTable");
DataRow dr;
table.Columns.Add("id", typeof(int));
table.Columns.Add("name", typeof(string));
 

foreach (DataRow myRow in dset.Tables["Table"].Rows)
                    {
                        int CCid;
                        CCid = (int)myRow["CID"];
 
                        string str = String.Format("C{0:0000}", CCid);
                        string[] names = { str };
                        dr = table.NewRow();
                        dr["id"] = j + 1;
                        dr["name"] = Convert.ToString(names[str[0]]);
                        table.Rows.Add(dr);
                        }
 
                 ds.Tables.Add(table);
                 ddlContract.DataSource = ds.Tables["MyTable"];
                 ddlContract.DataTextField = "name";
                 ddlContract.DataValueField = "id";
                 ddlContract.DataBind();


Still it's no way near effecient code!! but for educational purposes it's ok..
 
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