Add Item In Combobox
-
I am working in vb.net with c# - 2008 Hear Some Problem to fill Combobox using recursion in Duplication Problem in my combobox please help me my table and coding like this In table using Self join *************** Table****************** Id Parent_Id Name 1 Null Sales 2 1 Item1 3 1 Item2 4 2 Item3 5 3 Item4 6 4 Item5 7 Null Purchase 8 7 Item1 9 7 Item1 My table data are look like that 1) in my application i want to fill only the Sales and that's child to releted with them in combobox 2) in my application i use to recursion that's work but that show many time the purticulr some items 3) if any budy have the solution of my problem please help me and if possible to give me code releted with otherwise give some example releted with it please
-
I am working in vb.net with c# - 2008 Hear Some Problem to fill Combobox using recursion in Duplication Problem in my combobox please help me my table and coding like this In table using Self join *************** Table****************** Id Parent_Id Name 1 Null Sales 2 1 Item1 3 1 Item2 4 2 Item3 5 3 Item4 6 4 Item5 7 Null Purchase 8 7 Item1 9 7 Item1 My table data are look like that 1) in my application i want to fill only the Sales and that's child to releted with them in combobox 2) in my application i use to recursion that's work but that show many time the purticulr some items 3) if any budy have the solution of my problem please help me and if possible to give me code releted with otherwise give some example releted with it please
-
So the data is duplicating in the combobox? Could you post the code you have currently. Without it, it's quite hard to say where the problem lies.
The need to optimize rises from a bad design.My articles[^]
- public void FillCombo() { DataTable dt = new DataTable(); StrSql = "select Level_Id,Level_Name from UserLevel"; StrSql += " where Level_Id = '2'"; dt = qry.FillDataSet(StrSql).Tables[0]; if (dt.Rows.Count > 0) { if (ChildExistOrNotInCombo(2) == true) { AddChildCombo(2, ref dt); } } this.cmbParentLevel.DataSource = dt; this.cmbParentLevel.DisplayMember = "Level_Name"; this.cmbParentLevel.ValueMember = "Level_Id"; } 2) private void AddChildCombo(int objid,ref DataTable Table) { DataTable Objdt = new DataTable(); int Objval = 0; StrSql = "select Level_Id,Level_Name from UserLevel"; StrSql += " where Parent_LevelId = " + objid + ""; Objdt = qry.FillDataSet(StrSql).Tables[0]; if (Objdt.Rows.Count > 0) { for ( int i = 0; i <= Objdt.Rows.Count - 1; i++) { Table.ImportRow(Objdt.Rows[i]); Objval = Convert.ToInt32(Objdt.Rows[i].ItemArray[0]); if (ChildExistOrNotInCombo(Objval) == true) { AddChildCombo(Objval, ref Objdt); } } } } 3) private bool ChildExistOrNotInCombo(int ObjParentId) { StrSql = "select * from userlevel where Parent_LevelId = " + ObjParentId + ""; if (qry.CheckExist(StrSql) == true) { return true; } else { return false; } } in my database this item has only 12 record but in this that retrun the 77 record in some are wrong but i dont't know please help me on this problem as soon as possible
-
- public void FillCombo() { DataTable dt = new DataTable(); StrSql = "select Level_Id,Level_Name from UserLevel"; StrSql += " where Level_Id = '2'"; dt = qry.FillDataSet(StrSql).Tables[0]; if (dt.Rows.Count > 0) { if (ChildExistOrNotInCombo(2) == true) { AddChildCombo(2, ref dt); } } this.cmbParentLevel.DataSource = dt; this.cmbParentLevel.DisplayMember = "Level_Name"; this.cmbParentLevel.ValueMember = "Level_Id"; } 2) private void AddChildCombo(int objid,ref DataTable Table) { DataTable Objdt = new DataTable(); int Objval = 0; StrSql = "select Level_Id,Level_Name from UserLevel"; StrSql += " where Parent_LevelId = " + objid + ""; Objdt = qry.FillDataSet(StrSql).Tables[0]; if (Objdt.Rows.Count > 0) { for ( int i = 0; i <= Objdt.Rows.Count - 1; i++) { Table.ImportRow(Objdt.Rows[i]); Objval = Convert.ToInt32(Objdt.Rows[i].ItemArray[0]); if (ChildExistOrNotInCombo(Objval) == true) { AddChildCombo(Objval, ref Objdt); } } } } 3) private bool ChildExistOrNotInCombo(int ObjParentId) { StrSql = "select * from userlevel where Parent_LevelId = " + ObjParentId + ""; if (qry.CheckExist(StrSql) == true) { return true; } else { return false; } } in my database this item has only 12 record but in this that retrun the 77 record in some are wrong but i dont't know please help me on this problem as soon as possible
The first thing is that why are you checking from database if item exists in combo in ChildExistOrNotInCombo? Shouldn't you be checking if the datatable you're filling contains the item? Also it seems that you check from database if the row has children? I think it's unnecessary since you could simply call recursion. If there are no children, nothing's added. You don't check how many rows your query returns in FillCombo. You use it like it returns 1, but if it returns several rows, I think you'll have extra data in the datatable. And when you do the recursion, you select based on the row level_ID, is this correct? Shouldn't you select using Parent_ID like Parent_ID = ID (based on the data you posted in the first post)? So is Level_id same as ID in the first post and Parent_LevelId is the same as Parent_Id in the first post. So basically it could be something like:
public void FillCombo()
{
DataTable dt = new DataTable();
StrSql = "select Level_Id,Level_Name from UserLevel";
StrSql += " where Level_Id = '2'";
dt = qry.FillDataSet(StrSql).Tables[0];
if (dt.Rows.Count > 0)
{
MessageBox.Show("More than one row");
}
AddChildCombo(2, ref dt);
this.cmbParentLevel.DataSource = dt;
this.cmbParentLevel.DisplayMember = "Level_Name";
this.cmbParentLevel.ValueMember = "Level_Id";
}private void AddChildCombo(int objid,ref DataTable Table)
{
DataTable Objdt;
int Objval;
StrSql = "select Level_Id,Level_Name from UserLevel";
StrSql += " where Parent_LevelId = " + objid + "";
Objdt = qry.FillDataSet(StrSql).Tables[0];
for ( int i = 0; i <= Objdt.Rows.Count - 1; i++)
{
Table.ImportRow(Objdt.Rows[i]);
Objval = Convert.ToInt32(Objdt.Rows[i].ItemArray[0]);
AddChildCombo(Objval, ref Objdt);
}
}The need to optimize rises from a bad design.My articles[^]
-
The first thing is that why are you checking from database if item exists in combo in ChildExistOrNotInCombo? Shouldn't you be checking if the datatable you're filling contains the item? Also it seems that you check from database if the row has children? I think it's unnecessary since you could simply call recursion. If there are no children, nothing's added. You don't check how many rows your query returns in FillCombo. You use it like it returns 1, but if it returns several rows, I think you'll have extra data in the datatable. And when you do the recursion, you select based on the row level_ID, is this correct? Shouldn't you select using Parent_ID like Parent_ID = ID (based on the data you posted in the first post)? So is Level_id same as ID in the first post and Parent_LevelId is the same as Parent_Id in the first post. So basically it could be something like:
public void FillCombo()
{
DataTable dt = new DataTable();
StrSql = "select Level_Id,Level_Name from UserLevel";
StrSql += " where Level_Id = '2'";
dt = qry.FillDataSet(StrSql).Tables[0];
if (dt.Rows.Count > 0)
{
MessageBox.Show("More than one row");
}
AddChildCombo(2, ref dt);
this.cmbParentLevel.DataSource = dt;
this.cmbParentLevel.DisplayMember = "Level_Name";
this.cmbParentLevel.ValueMember = "Level_Id";
}private void AddChildCombo(int objid,ref DataTable Table)
{
DataTable Objdt;
int Objval;
StrSql = "select Level_Id,Level_Name from UserLevel";
StrSql += " where Parent_LevelId = " + objid + "";
Objdt = qry.FillDataSet(StrSql).Tables[0];
for ( int i = 0; i <= Objdt.Rows.Count - 1; i++)
{
Table.ImportRow(Objdt.Rows[i]);
Objval = Convert.ToInt32(Objdt.Rows[i].ItemArray[0]);
AddChildCombo(Objval, ref Objdt);
}
}The need to optimize rises from a bad design.My articles[^]
- But sir in my database there is no extra data my database data is show below Hear is my Database 2 NULL Account 3 2 Perment 4 2 Tempray 11 NULL Sales 12 11 tttttt 13 11 pppp 14 12 qqqqq 15 13 jjjjj 16 15 asd 17 16 sasda 20 17 fffff 23 NULL Purchase 28 23 Pur1 30 20 qqqqqqq 31 14 aaaa 32 15 bbbbbbbb 33 32 iiiiiii 34 3 Company Employees 35 4 Contract Base Employee 2) i am trying as u say but that's are return the same thing as they return early 3) in my application i want to just add Parent data with the all child in my combobox if there are any other way to fill the combobox so please tell me 4) Example : The Sales is Parent Node and the that releted all child are fill in to the combobox with the Parnet Node 5) Hear the NULL is define that that is Parent Node.
-
- But sir in my database there is no extra data my database data is show below Hear is my Database 2 NULL Account 3 2 Perment 4 2 Tempray 11 NULL Sales 12 11 tttttt 13 11 pppp 14 12 qqqqq 15 13 jjjjj 16 15 asd 17 16 sasda 20 17 fffff 23 NULL Purchase 28 23 Pur1 30 20 qqqqqqq 31 14 aaaa 32 15 bbbbbbbb 33 32 iiiiiii 34 3 Company Employees 35 4 Contract Base Employee 2) i am trying as u say but that's are return the same thing as they return early 3) in my application i want to just add Parent data with the all child in my combobox if there are any other way to fill the combobox so please tell me 4) Example : The Sales is Parent Node and the that releted all child are fill in to the combobox with the Parnet Node 5) Hear the NULL is define that that is Parent Node.
I didn't say that the problem is in the data in the database, but how you use it. One problem is that I don't know what your method qry.FillDataSet does. Perhaps it adds rows to an existing datatable? In that case you would have too much data. Lets make it a bit simpler (I corrected few typos I made earlier):
Data
Level_Id ParentId Level_Name2 NULL Account
3 2 Perment
4 2 Tempray
11 NULL Sales
12 11 tttttt
13 11 pppp
14 12 qqqqq
15 13 jjjjj
16 15 asd
17 16 sasda
20 17 fffff
23 NULL Purchase
28 23 Pur1
30 20 qqqqqqq
31 14 aaaa
32 15 bbbbbbbb
33 32 iiiiiii
34 3 Company Employees
35 4 Contract Base Employeepublic void FillCombo()
{
DataTable dt = new DataTable();
string sql = "select Level_Id, Level_Name from UserLevel"
+ " where Level_Name = 'Sales'";
dt = qry.FillDataSet(sql).Tables[0];
if (dt.Rows.Count > 1)
{
MessageBox.Show("Problem: More than one row");
}
AddChildCombo((int)dt.Rows[0]["Level_Id"], ref dt);
this.cmbParentLevel.DataSource = dt;
this.cmbParentLevel.DisplayMember = "Level_Name";
this.cmbParentLevel.ValueMember = "Level_Id";
}
private void AddChildCombo(int parentLevelId,ref DataTable Table)
{
DataTable childTable;
string sql = "select Level_Id,Level_Name from UserLevel"
+ " where Parent_LevelId = " + parentLevelId + "";
childTable = qry.FillDataSet(sql).Tables[0];
-- Check with debugger that rows are correctly
for ( int i = 0; i <= childTable.Rows.Count - 1; i++)
{
Table.ImportRow(childTable.Rows[i]);
AddChildCombo((int)childTable.Rows[i]["Level_Id"], ref Table);
}
}Now use the debugger to see if row amounts and the data is correct in the FillCombo and also in the recursion.
The need to optimize rises from a bad design.My articles[^]
-
I didn't say that the problem is in the data in the database, but how you use it. One problem is that I don't know what your method qry.FillDataSet does. Perhaps it adds rows to an existing datatable? In that case you would have too much data. Lets make it a bit simpler (I corrected few typos I made earlier):
Data
Level_Id ParentId Level_Name2 NULL Account
3 2 Perment
4 2 Tempray
11 NULL Sales
12 11 tttttt
13 11 pppp
14 12 qqqqq
15 13 jjjjj
16 15 asd
17 16 sasda
20 17 fffff
23 NULL Purchase
28 23 Pur1
30 20 qqqqqqq
31 14 aaaa
32 15 bbbbbbbb
33 32 iiiiiii
34 3 Company Employees
35 4 Contract Base Employeepublic void FillCombo()
{
DataTable dt = new DataTable();
string sql = "select Level_Id, Level_Name from UserLevel"
+ " where Level_Name = 'Sales'";
dt = qry.FillDataSet(sql).Tables[0];
if (dt.Rows.Count > 1)
{
MessageBox.Show("Problem: More than one row");
}
AddChildCombo((int)dt.Rows[0]["Level_Id"], ref dt);
this.cmbParentLevel.DataSource = dt;
this.cmbParentLevel.DisplayMember = "Level_Name";
this.cmbParentLevel.ValueMember = "Level_Id";
}
private void AddChildCombo(int parentLevelId,ref DataTable Table)
{
DataTable childTable;
string sql = "select Level_Id,Level_Name from UserLevel"
+ " where Parent_LevelId = " + parentLevelId + "";
childTable = qry.FillDataSet(sql).Tables[0];
-- Check with debugger that rows are correctly
for ( int i = 0; i <= childTable.Rows.Count - 1; i++)
{
Table.ImportRow(childTable.Rows[i]);
AddChildCombo((int)childTable.Rows[i]["Level_Id"], ref Table);
}
}Now use the debugger to see if row amounts and the data is correct in the FillCombo and also in the recursion.
The need to optimize rises from a bad design.My articles[^]
Qry is a obj of my class FillDataSet is one Common Function that's only fill the dataset or datatabel In That the Data Will come is proper but Sir that Are Duplicate in to the combobox Just Example the 1) 'qqqqq' That's are show in combo ManyTime that's way the some other data are also come the many times Hear Sir Any Other Way to Fill the Combo My one Friend Sid that that's to fillcombo with use Recursion is not give Proper Result and also say that that it is not proper way. So if U know to the Other Way to Fill Combo so Please Tell Me That's Request Please
-
Qry is a obj of my class FillDataSet is one Common Function that's only fill the dataset or datatabel In That the Data Will come is proper but Sir that Are Duplicate in to the combobox Just Example the 1) 'qqqqq' That's are show in combo ManyTime that's way the some other data are also come the many times Hear Sir Any Other Way to Fill the Combo My one Friend Sid that that's to fillcombo with use Recursion is not give Proper Result and also say that that it is not proper way. So if U know to the Other Way to Fill Combo so Please Tell Me That's Request Please
aajignesh wrote:
In That the Data Will come is proper but Sir that Are Duplicate in to the combobox
Maybe a dumb question, but do you clear the combobox at any time? If not, you should add Clear to the FillCombo. Also you you may want to clear bindings. Like:
public void FillCombo()
{
this.cmbParentLevel.DataSource = null;
DataTable dt;
strinf sql = "select Level_Id, Level_Name from UserLevel"
+ " where Level_Name = 'Sales'";
dt = qry.FillDataSet(sql).Tables[0];
if (dt.Rows.Count > 1)
{
MessageBox.Show("Problem: More than one row");
}
AddChildCombo((int)dt.Rows[0]["Level_Id"], ref dt);
this.cmbParentLevel.Items.Clear();
this.cmbParentLevel.DataSource = dt;
this.cmbParentLevel.DisplayMember = "Level_Name";
this.cmbParentLevel.ValueMember = "Level_Id";
}aajignesh wrote:
My one Friend Sid that that's to fillcombo with use Recursion is not give Proper Result and also say that that it is not proper way.
Recursion is just a technique to handle data which is in tree format. It can be used for many situations, like filling a combo. There's no reason why filling a combo with a recursion wouldn't work. The problem is in the implementation.
aajignesh wrote:
So if U know to the Other Way to Fill Combo so Please Tell Me That's Request Please
This depends where the data is coming from. Are you querying for example SQL Server database?
The need to optimize rises from a bad design.My articles[^]
-
aajignesh wrote:
In That the Data Will come is proper but Sir that Are Duplicate in to the combobox
Maybe a dumb question, but do you clear the combobox at any time? If not, you should add Clear to the FillCombo. Also you you may want to clear bindings. Like:
public void FillCombo()
{
this.cmbParentLevel.DataSource = null;
DataTable dt;
strinf sql = "select Level_Id, Level_Name from UserLevel"
+ " where Level_Name = 'Sales'";
dt = qry.FillDataSet(sql).Tables[0];
if (dt.Rows.Count > 1)
{
MessageBox.Show("Problem: More than one row");
}
AddChildCombo((int)dt.Rows[0]["Level_Id"], ref dt);
this.cmbParentLevel.Items.Clear();
this.cmbParentLevel.DataSource = dt;
this.cmbParentLevel.DisplayMember = "Level_Name";
this.cmbParentLevel.ValueMember = "Level_Id";
}aajignesh wrote:
My one Friend Sid that that's to fillcombo with use Recursion is not give Proper Result and also say that that it is not proper way.
Recursion is just a technique to handle data which is in tree format. It can be used for many situations, like filling a combo. There's no reason why filling a combo with a recursion wouldn't work. The problem is in the implementation.
aajignesh wrote:
So if U know to the Other Way to Fill Combo so Please Tell Me That's Request Please
This depends where the data is coming from. Are you querying for example SQL Server database?
The need to optimize rises from a bad design.My articles[^]
I am use FillCombo to Form Load Event to FillCombo Box Sir Hear Some Mistact Of my Coding But Hear I Dont't Find That. If Sir U Have Any Other Idea to FillCombo so Plese Tell Me Please
-
I am use FillCombo to Form Load Event to FillCombo Box Sir Hear Some Mistact Of my Coding But Hear I Dont't Find That. If Sir U Have Any Other Idea to FillCombo so Plese Tell Me Please
aajignesh wrote:
If Sir U Have Any Other Idea to FillCombo so Plese Tell Me Please
As I said, it depends where the data is coming from. Is it coming from SQL Server 2005 database? If it is one possibility could be to use recursive CTE (Common Table Expression). However, I made a little sample on your data. Since I don't have your datasource I had to make it differently so the implementation is a bit different. Try this. As far as can see, it fills a combo named cmbTest as expected. If the result is what you want, modify it in little pieces to implement it the way that's suitable for you: Start in form load
private void Form2\_Load(object sender, EventArgs e) { FillCombo(); }
Method to always get the same data
private DataTable GetDataTable() { DataTable data = new DataTable(); data.Columns.Add("Id", typeof(int)); data.Columns.Add("ParentId", typeof(int)); data.Columns.Add("LevelName", typeof(string)); data.Rows.Add(2, null, "Account"); data.Rows.Add(3, 2, "Perment"); data.Rows.Add(4, 2, "Tempray"); data.Rows.Add(11, null, "Sales"); data.Rows.Add(12, 11, "tttttt"); data.Rows.Add(13, 11, "pppp"); data.Rows.Add(14, 12, "qqqqq"); data.Rows.Add(15, 13, "jjjjj"); data.Rows.Add(16, 15, "asd"); data.Rows.Add(17, 16, "sasda"); data.Rows.Add(20, 17, "fffff"); data.Rows.Add(23, null, "Purchase"); data.Rows.Add(28, 23, "Pur1"); data.Rows.Add(30, 20, "qqqqqqq"); data.Rows.Add(31, 14, "aaaa"); data.Rows.Add(32, 15, "bbbbbbbb"); data.Rows.Add(33, 32, "iiiiiii"); data.Rows.Add(34, 3, "Company Employees"); data.Rows.Add(35, 4, "Contract Base Employee"); return data; }
Main point for recursion
public void FillCombo() { DataTable dt = GetDataTable().Clone(); DataRow startRow = GetDataTable().Select("LevelName = 'Sales'")\[0\]; AddChildren((int)startRow\["Id"\], ref dt); this.cmbTest.DisplayMember = "LevelName"; this.cmbTest.ValueMember = "Id"; this.cmbTest.DataSource = dt; }
And the recursion
private void AddChildren(int parentId, ref DataTable TableToFill) { DataRow\[\] children = GetDataTable().Select("ParentId = " + parentId.ToString()); for ( in
-
aajignesh wrote:
If Sir U Have Any Other Idea to FillCombo so Plese Tell Me Please
As I said, it depends where the data is coming from. Is it coming from SQL Server 2005 database? If it is one possibility could be to use recursive CTE (Common Table Expression). However, I made a little sample on your data. Since I don't have your datasource I had to make it differently so the implementation is a bit different. Try this. As far as can see, it fills a combo named cmbTest as expected. If the result is what you want, modify it in little pieces to implement it the way that's suitable for you: Start in form load
private void Form2\_Load(object sender, EventArgs e) { FillCombo(); }
Method to always get the same data
private DataTable GetDataTable() { DataTable data = new DataTable(); data.Columns.Add("Id", typeof(int)); data.Columns.Add("ParentId", typeof(int)); data.Columns.Add("LevelName", typeof(string)); data.Rows.Add(2, null, "Account"); data.Rows.Add(3, 2, "Perment"); data.Rows.Add(4, 2, "Tempray"); data.Rows.Add(11, null, "Sales"); data.Rows.Add(12, 11, "tttttt"); data.Rows.Add(13, 11, "pppp"); data.Rows.Add(14, 12, "qqqqq"); data.Rows.Add(15, 13, "jjjjj"); data.Rows.Add(16, 15, "asd"); data.Rows.Add(17, 16, "sasda"); data.Rows.Add(20, 17, "fffff"); data.Rows.Add(23, null, "Purchase"); data.Rows.Add(28, 23, "Pur1"); data.Rows.Add(30, 20, "qqqqqqq"); data.Rows.Add(31, 14, "aaaa"); data.Rows.Add(32, 15, "bbbbbbbb"); data.Rows.Add(33, 32, "iiiiiii"); data.Rows.Add(34, 3, "Company Employees"); data.Rows.Add(35, 4, "Contract Base Employee"); return data; }
Main point for recursion
public void FillCombo() { DataTable dt = GetDataTable().Clone(); DataRow startRow = GetDataTable().Select("LevelName = 'Sales'")\[0\]; AddChildren((int)startRow\["Id"\], ref dt); this.cmbTest.DisplayMember = "LevelName"; this.cmbTest.ValueMember = "Id"; this.cmbTest.DataSource = dt; }
And the recursion
private void AddChildren(int parentId, ref DataTable TableToFill) { DataRow\[\] children = GetDataTable().Select("ParentId = " + parentId.ToString()); for ( in
Thaks You .....
-
Thaks You .....