Is there a way to pick up ID after cascading multiple combobox without using ID as value member of combobox?cascading comboBox in windows form using c#fill textbox on second combobox selection changed in cascading combobox in windows form using c#How do I insert multiple rows WITHOUT repeating the “INSERT INTO dbo.Blah” part of the statement?ComboBox: Adding Text and Value to an Item (no Binding Source)don't see the new database and can't log inExporting Large Amounts of DataCombobox Value Member to currencyProper way to initialize a C# dictionary with values?Using ExpandoObject with ComboBox Items not picking up Display and Value membersCascading Combobox - WinForms with added valuesCombobox show value memberCase query sql command in asp.net webform
How to prevent cables getting intertwined
Have Steve Rogers (Captain America) and a young Erik Lehnsherr (Magneto) interacted during WWII?
How to know whether to write accidentals as sharps or flats?
Can a non-invertible function be inverted by returning a set of all possible solutions?
Print the phrase "And she said, 'But that's his.'" using only the alphabet
What is the color associated with lukewarm?
Do my partner and son need an SSN to be dependents on my taxes?
What is this plant I saw for sale at a Romanian farmer's market?
How to search for Android apps without ads?
Why is gun control associated with the socially liberal Democratic party?
Why is Skinner so awkward in Hot Fuzz?
How can the US president give an order to a civilian?
How can this shape perfectly cover a cube?
Digital signature that is only verifiable by one specific person
What kind of chart is this?
How to sort human readable size
What are the mechanical differences between Adapt and Monstrosity?
Why can't we feel the Earth's revolution?
I have found ports on my Samsung smart tv running a display service. What can I do with it?
Co-worker is now managing my team. Does this mean that I'm being demoted?
How did the European Union reach the figure of 3% as a maximum allowed deficit?
How can I maintain game balance while allowing my player to craft genuinely useful items?
Are there any super-powered aliens in the Marvel universe?
100-doors puzzle
Is there a way to pick up ID after cascading multiple combobox without using ID as value member of combobox?
cascading comboBox in windows form using c#fill textbox on second combobox selection changed in cascading combobox in windows form using c#How do I insert multiple rows WITHOUT repeating the “INSERT INTO dbo.Blah” part of the statement?ComboBox: Adding Text and Value to an Item (no Binding Source)don't see the new database and can't log inExporting Large Amounts of DataCombobox Value Member to currencyProper way to initialize a C# dictionary with values?Using ExpandoObject with ComboBox Items not picking up Display and Value membersCascading Combobox - WinForms with added valuesCombobox show value memberCase query sql command in asp.net webform
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I need to get productID from product table and pass it to a textbox. But I want to select the product ID by cascading comboBox texts as SQL query parameters.
I have tried cascading the comboBoxes. The cascading works fine and displays what I need.
I have done some research in cascading comboBox. Here are two useful links I have found out.
cascading comboBox in windows form using c#
fill textbox on second combobox selection changed in cascading combobox in windows form using c#
In both links I did not find answer to my question.
Here is a demo table
ProductID Category SubCategory Item
--------- ------------------------------ -------------------- ----------------
1 Pen Ballpoint Matadoor
2 Pencil HB Natraz
I want get ProductID values by selecting Category, SubCategory, Item using cascaded comboBoxes.
Here is some demo code that takes care of the cascading.
1st ComboBox:
public InventroyAddForm()
InitializeComponent();
string maincon = ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString;
string query1 = "select * from Product";
string query2 = "select * from PartySpecificPrice";
SqlConnection con = new SqlConnection(maincon);
SqlCommand com = new SqlCommand(query1, con);
//con.Open();
SqlDataAdapter adp = new SqlDataAdapter(com);
DataTable dt = new DataTable();
adp.Fill(dt);
comboBox2.ValueMember = "Category";
comboBox2.DisplayMember = "Category";
comboBox2.DataSource = dt;
//con.Close();
comboBox3.Enabled = false;
comboBox4.Enabled = false;
2nd ComboBox:
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
if(comboBox2.SelectedValue.ToString() != null)
string maincon = ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString;
string query1 = "select * from Product where Category=@Category";
SqlConnection con = new SqlConnection(maincon);
SqlCommand com1 = new SqlCommand(query1, con);
//con.Open();
com1.Parameters.AddWithValue("@Category",comboBox2.SelectedValue.ToString());
SqlDataAdapter adp1 = new SqlDataAdapter(com1);
DataTable dt1 = new DataTable();
adp1.Fill(dt1);
comboBox3.ValueMember = "Category";
comboBox3.DisplayMember = "SubCategory";
comboBox3.DataSource = dt1;
comboBox3.Enabled = true;
comboBox4.Enabled = false;
//con.Close();
3rd ComboBox:
private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
if (comboBox3.SelectedValue.ToString() != null)
string maincon = ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString;
string query2 = "select * from Product where Category=@Category";
SqlConnection con = new SqlConnection(maincon);
SqlCommand com2 = new SqlCommand(query2, con);
//con.Open();
com2.Parameters.AddWithValue("@Category", comboBox3.SelectedValue.ToString());
SqlDataAdapter adp2 = new SqlDataAdapter(com2);
DataTable dt2 = new DataTable();
adp2.Fill(dt2);
comboBox4.ValueMember = "Category";
comboBox4.DisplayMember = "Item";
comboBox4.DataSource = dt2;
comboBox3.Enabled = true;
comboBox4.Enabled = true;
//con.Close();
textBox2.Text = Convert.ToString(comboBox3.SelectedIndex);
c# sql-server winforms
add a comment |
I need to get productID from product table and pass it to a textbox. But I want to select the product ID by cascading comboBox texts as SQL query parameters.
I have tried cascading the comboBoxes. The cascading works fine and displays what I need.
I have done some research in cascading comboBox. Here are two useful links I have found out.
cascading comboBox in windows form using c#
fill textbox on second combobox selection changed in cascading combobox in windows form using c#
In both links I did not find answer to my question.
Here is a demo table
ProductID Category SubCategory Item
--------- ------------------------------ -------------------- ----------------
1 Pen Ballpoint Matadoor
2 Pencil HB Natraz
I want get ProductID values by selecting Category, SubCategory, Item using cascaded comboBoxes.
Here is some demo code that takes care of the cascading.
1st ComboBox:
public InventroyAddForm()
InitializeComponent();
string maincon = ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString;
string query1 = "select * from Product";
string query2 = "select * from PartySpecificPrice";
SqlConnection con = new SqlConnection(maincon);
SqlCommand com = new SqlCommand(query1, con);
//con.Open();
SqlDataAdapter adp = new SqlDataAdapter(com);
DataTable dt = new DataTable();
adp.Fill(dt);
comboBox2.ValueMember = "Category";
comboBox2.DisplayMember = "Category";
comboBox2.DataSource = dt;
//con.Close();
comboBox3.Enabled = false;
comboBox4.Enabled = false;
2nd ComboBox:
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
if(comboBox2.SelectedValue.ToString() != null)
string maincon = ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString;
string query1 = "select * from Product where Category=@Category";
SqlConnection con = new SqlConnection(maincon);
SqlCommand com1 = new SqlCommand(query1, con);
//con.Open();
com1.Parameters.AddWithValue("@Category",comboBox2.SelectedValue.ToString());
SqlDataAdapter adp1 = new SqlDataAdapter(com1);
DataTable dt1 = new DataTable();
adp1.Fill(dt1);
comboBox3.ValueMember = "Category";
comboBox3.DisplayMember = "SubCategory";
comboBox3.DataSource = dt1;
comboBox3.Enabled = true;
comboBox4.Enabled = false;
//con.Close();
3rd ComboBox:
private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
if (comboBox3.SelectedValue.ToString() != null)
string maincon = ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString;
string query2 = "select * from Product where Category=@Category";
SqlConnection con = new SqlConnection(maincon);
SqlCommand com2 = new SqlCommand(query2, con);
//con.Open();
com2.Parameters.AddWithValue("@Category", comboBox3.SelectedValue.ToString());
SqlDataAdapter adp2 = new SqlDataAdapter(com2);
DataTable dt2 = new DataTable();
adp2.Fill(dt2);
comboBox4.ValueMember = "Category";
comboBox4.DisplayMember = "Item";
comboBox4.DataSource = dt2;
comboBox3.Enabled = true;
comboBox4.Enabled = true;
//con.Close();
textBox2.Text = Convert.ToString(comboBox3.SelectedIndex);
c# sql-server winforms
1
if you are going to deal withstrings
(which I would not recommend) then why not create another query to fetch theProductID
from theproduct
table with awhere
condition ofCategory=<SomeCategoryValue> and SubCategory=<SomeSubCategoryValue> and Item=<SomeItemValue>;
– vikscool
Mar 25 at 4:36
I would have but I need to fetch the ProductID and Products table will have a large number of data with many categories, subcategories and items. To offer a simpler way to select the ProductID cascading comboBox is the way to go in my opinion. Because to use your suggested query I have use either exact or approximate(using wildcards) SQL parameters value which is quite hard to do where the database has lots of products.I would like to hear about your recommendation about not using strings
@vikscool
– Khandkar Asif Hossain
Mar 25 at 12:07
1
Sorry for the delayed response,1stAs for why I would not recommend the String?
What if you have twoitems
with the same name in differentsubcategories
and the way you are getting theitems
it would be hard for you to identify whichitem
belongs to whichsubcategory
(as your query is based onCategory
and notCategory+SubCategory
).
– vikscool
Mar 27 at 4:10
1
2nd Your demo table looks like its aJoined Result
. So, if you have other master tables forCategory
andSubCategory
, it would be easier to make the cascading where theitem
table will have its parentSubCategoryId
and the SubCategory would have its parentCategoryId
. Hence, the relation would become something like this:Category->get SubCategory based on the categoryId->get Items based on the subcategoryId
.
– vikscool
Mar 27 at 4:12
Thanks @vikscool. First of all your assumption of the table being join table is incorrect. The table i provided is in fact a single table. And you are suggesting to break up the table. But can I retrieve the product ID if I break up the table? if so can you post an answer based on your suggestion.
– Khandkar Asif Hossain
Mar 28 at 8:34
add a comment |
I need to get productID from product table and pass it to a textbox. But I want to select the product ID by cascading comboBox texts as SQL query parameters.
I have tried cascading the comboBoxes. The cascading works fine and displays what I need.
I have done some research in cascading comboBox. Here are two useful links I have found out.
cascading comboBox in windows form using c#
fill textbox on second combobox selection changed in cascading combobox in windows form using c#
In both links I did not find answer to my question.
Here is a demo table
ProductID Category SubCategory Item
--------- ------------------------------ -------------------- ----------------
1 Pen Ballpoint Matadoor
2 Pencil HB Natraz
I want get ProductID values by selecting Category, SubCategory, Item using cascaded comboBoxes.
Here is some demo code that takes care of the cascading.
1st ComboBox:
public InventroyAddForm()
InitializeComponent();
string maincon = ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString;
string query1 = "select * from Product";
string query2 = "select * from PartySpecificPrice";
SqlConnection con = new SqlConnection(maincon);
SqlCommand com = new SqlCommand(query1, con);
//con.Open();
SqlDataAdapter adp = new SqlDataAdapter(com);
DataTable dt = new DataTable();
adp.Fill(dt);
comboBox2.ValueMember = "Category";
comboBox2.DisplayMember = "Category";
comboBox2.DataSource = dt;
//con.Close();
comboBox3.Enabled = false;
comboBox4.Enabled = false;
2nd ComboBox:
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
if(comboBox2.SelectedValue.ToString() != null)
string maincon = ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString;
string query1 = "select * from Product where Category=@Category";
SqlConnection con = new SqlConnection(maincon);
SqlCommand com1 = new SqlCommand(query1, con);
//con.Open();
com1.Parameters.AddWithValue("@Category",comboBox2.SelectedValue.ToString());
SqlDataAdapter adp1 = new SqlDataAdapter(com1);
DataTable dt1 = new DataTable();
adp1.Fill(dt1);
comboBox3.ValueMember = "Category";
comboBox3.DisplayMember = "SubCategory";
comboBox3.DataSource = dt1;
comboBox3.Enabled = true;
comboBox4.Enabled = false;
//con.Close();
3rd ComboBox:
private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
if (comboBox3.SelectedValue.ToString() != null)
string maincon = ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString;
string query2 = "select * from Product where Category=@Category";
SqlConnection con = new SqlConnection(maincon);
SqlCommand com2 = new SqlCommand(query2, con);
//con.Open();
com2.Parameters.AddWithValue("@Category", comboBox3.SelectedValue.ToString());
SqlDataAdapter adp2 = new SqlDataAdapter(com2);
DataTable dt2 = new DataTable();
adp2.Fill(dt2);
comboBox4.ValueMember = "Category";
comboBox4.DisplayMember = "Item";
comboBox4.DataSource = dt2;
comboBox3.Enabled = true;
comboBox4.Enabled = true;
//con.Close();
textBox2.Text = Convert.ToString(comboBox3.SelectedIndex);
c# sql-server winforms
I need to get productID from product table and pass it to a textbox. But I want to select the product ID by cascading comboBox texts as SQL query parameters.
I have tried cascading the comboBoxes. The cascading works fine and displays what I need.
I have done some research in cascading comboBox. Here are two useful links I have found out.
cascading comboBox in windows form using c#
fill textbox on second combobox selection changed in cascading combobox in windows form using c#
In both links I did not find answer to my question.
Here is a demo table
ProductID Category SubCategory Item
--------- ------------------------------ -------------------- ----------------
1 Pen Ballpoint Matadoor
2 Pencil HB Natraz
I want get ProductID values by selecting Category, SubCategory, Item using cascaded comboBoxes.
Here is some demo code that takes care of the cascading.
1st ComboBox:
public InventroyAddForm()
InitializeComponent();
string maincon = ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString;
string query1 = "select * from Product";
string query2 = "select * from PartySpecificPrice";
SqlConnection con = new SqlConnection(maincon);
SqlCommand com = new SqlCommand(query1, con);
//con.Open();
SqlDataAdapter adp = new SqlDataAdapter(com);
DataTable dt = new DataTable();
adp.Fill(dt);
comboBox2.ValueMember = "Category";
comboBox2.DisplayMember = "Category";
comboBox2.DataSource = dt;
//con.Close();
comboBox3.Enabled = false;
comboBox4.Enabled = false;
2nd ComboBox:
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
if(comboBox2.SelectedValue.ToString() != null)
string maincon = ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString;
string query1 = "select * from Product where Category=@Category";
SqlConnection con = new SqlConnection(maincon);
SqlCommand com1 = new SqlCommand(query1, con);
//con.Open();
com1.Parameters.AddWithValue("@Category",comboBox2.SelectedValue.ToString());
SqlDataAdapter adp1 = new SqlDataAdapter(com1);
DataTable dt1 = new DataTable();
adp1.Fill(dt1);
comboBox3.ValueMember = "Category";
comboBox3.DisplayMember = "SubCategory";
comboBox3.DataSource = dt1;
comboBox3.Enabled = true;
comboBox4.Enabled = false;
//con.Close();
3rd ComboBox:
private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
if (comboBox3.SelectedValue.ToString() != null)
string maincon = ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString;
string query2 = "select * from Product where Category=@Category";
SqlConnection con = new SqlConnection(maincon);
SqlCommand com2 = new SqlCommand(query2, con);
//con.Open();
com2.Parameters.AddWithValue("@Category", comboBox3.SelectedValue.ToString());
SqlDataAdapter adp2 = new SqlDataAdapter(com2);
DataTable dt2 = new DataTable();
adp2.Fill(dt2);
comboBox4.ValueMember = "Category";
comboBox4.DisplayMember = "Item";
comboBox4.DataSource = dt2;
comboBox3.Enabled = true;
comboBox4.Enabled = true;
//con.Close();
textBox2.Text = Convert.ToString(comboBox3.SelectedIndex);
c# sql-server winforms
c# sql-server winforms
asked Mar 25 at 4:10
Khandkar Asif HossainKhandkar Asif Hossain
507
507
1
if you are going to deal withstrings
(which I would not recommend) then why not create another query to fetch theProductID
from theproduct
table with awhere
condition ofCategory=<SomeCategoryValue> and SubCategory=<SomeSubCategoryValue> and Item=<SomeItemValue>;
– vikscool
Mar 25 at 4:36
I would have but I need to fetch the ProductID and Products table will have a large number of data with many categories, subcategories and items. To offer a simpler way to select the ProductID cascading comboBox is the way to go in my opinion. Because to use your suggested query I have use either exact or approximate(using wildcards) SQL parameters value which is quite hard to do where the database has lots of products.I would like to hear about your recommendation about not using strings
@vikscool
– Khandkar Asif Hossain
Mar 25 at 12:07
1
Sorry for the delayed response,1stAs for why I would not recommend the String?
What if you have twoitems
with the same name in differentsubcategories
and the way you are getting theitems
it would be hard for you to identify whichitem
belongs to whichsubcategory
(as your query is based onCategory
and notCategory+SubCategory
).
– vikscool
Mar 27 at 4:10
1
2nd Your demo table looks like its aJoined Result
. So, if you have other master tables forCategory
andSubCategory
, it would be easier to make the cascading where theitem
table will have its parentSubCategoryId
and the SubCategory would have its parentCategoryId
. Hence, the relation would become something like this:Category->get SubCategory based on the categoryId->get Items based on the subcategoryId
.
– vikscool
Mar 27 at 4:12
Thanks @vikscool. First of all your assumption of the table being join table is incorrect. The table i provided is in fact a single table. And you are suggesting to break up the table. But can I retrieve the product ID if I break up the table? if so can you post an answer based on your suggestion.
– Khandkar Asif Hossain
Mar 28 at 8:34
add a comment |
1
if you are going to deal withstrings
(which I would not recommend) then why not create another query to fetch theProductID
from theproduct
table with awhere
condition ofCategory=<SomeCategoryValue> and SubCategory=<SomeSubCategoryValue> and Item=<SomeItemValue>;
– vikscool
Mar 25 at 4:36
I would have but I need to fetch the ProductID and Products table will have a large number of data with many categories, subcategories and items. To offer a simpler way to select the ProductID cascading comboBox is the way to go in my opinion. Because to use your suggested query I have use either exact or approximate(using wildcards) SQL parameters value which is quite hard to do where the database has lots of products.I would like to hear about your recommendation about not using strings
@vikscool
– Khandkar Asif Hossain
Mar 25 at 12:07
1
Sorry for the delayed response,1stAs for why I would not recommend the String?
What if you have twoitems
with the same name in differentsubcategories
and the way you are getting theitems
it would be hard for you to identify whichitem
belongs to whichsubcategory
(as your query is based onCategory
and notCategory+SubCategory
).
– vikscool
Mar 27 at 4:10
1
2nd Your demo table looks like its aJoined Result
. So, if you have other master tables forCategory
andSubCategory
, it would be easier to make the cascading where theitem
table will have its parentSubCategoryId
and the SubCategory would have its parentCategoryId
. Hence, the relation would become something like this:Category->get SubCategory based on the categoryId->get Items based on the subcategoryId
.
– vikscool
Mar 27 at 4:12
Thanks @vikscool. First of all your assumption of the table being join table is incorrect. The table i provided is in fact a single table. And you are suggesting to break up the table. But can I retrieve the product ID if I break up the table? if so can you post an answer based on your suggestion.
– Khandkar Asif Hossain
Mar 28 at 8:34
1
1
if you are going to deal with
strings
(which I would not recommend) then why not create another query to fetch the ProductID
from the product
table with a where
condition of Category=<SomeCategoryValue> and SubCategory=<SomeSubCategoryValue> and Item=<SomeItemValue>;
– vikscool
Mar 25 at 4:36
if you are going to deal with
strings
(which I would not recommend) then why not create another query to fetch the ProductID
from the product
table with a where
condition of Category=<SomeCategoryValue> and SubCategory=<SomeSubCategoryValue> and Item=<SomeItemValue>;
– vikscool
Mar 25 at 4:36
I would have but I need to fetch the ProductID and Products table will have a large number of data with many categories, subcategories and items. To offer a simpler way to select the ProductID cascading comboBox is the way to go in my opinion. Because to use your suggested query I have use either exact or approximate(using wildcards) SQL parameters value which is quite hard to do where the database has lots of products.
I would like to hear about your recommendation about not using strings
@vikscool– Khandkar Asif Hossain
Mar 25 at 12:07
I would have but I need to fetch the ProductID and Products table will have a large number of data with many categories, subcategories and items. To offer a simpler way to select the ProductID cascading comboBox is the way to go in my opinion. Because to use your suggested query I have use either exact or approximate(using wildcards) SQL parameters value which is quite hard to do where the database has lots of products.
I would like to hear about your recommendation about not using strings
@vikscool– Khandkar Asif Hossain
Mar 25 at 12:07
1
1
Sorry for the delayed response,1st
As for why I would not recommend the String?
What if you have two items
with the same name in different subcategories
and the way you are getting the items
it would be hard for you to identify which item
belongs to which subcategory
(as your query is based on Category
and not Category+SubCategory
).– vikscool
Mar 27 at 4:10
Sorry for the delayed response,1st
As for why I would not recommend the String?
What if you have two items
with the same name in different subcategories
and the way you are getting the items
it would be hard for you to identify which item
belongs to which subcategory
(as your query is based on Category
and not Category+SubCategory
).– vikscool
Mar 27 at 4:10
1
1
2nd Your demo table looks like its a
Joined Result
. So, if you have other master tables for Category
and SubCategory
, it would be easier to make the cascading where the item
table will have its parent SubCategoryId
and the SubCategory would have its parent CategoryId
. Hence, the relation would become something like this: Category->get SubCategory based on the categoryId->get Items based on the subcategoryId
.– vikscool
Mar 27 at 4:12
2nd Your demo table looks like its a
Joined Result
. So, if you have other master tables for Category
and SubCategory
, it would be easier to make the cascading where the item
table will have its parent SubCategoryId
and the SubCategory would have its parent CategoryId
. Hence, the relation would become something like this: Category->get SubCategory based on the categoryId->get Items based on the subcategoryId
.– vikscool
Mar 27 at 4:12
Thanks @vikscool. First of all your assumption of the table being join table is incorrect. The table i provided is in fact a single table. And you are suggesting to break up the table. But can I retrieve the product ID if I break up the table? if so can you post an answer based on your suggestion.
– Khandkar Asif Hossain
Mar 28 at 8:34
Thanks @vikscool. First of all your assumption of the table being join table is incorrect. The table i provided is in fact a single table. And you are suggesting to break up the table. But can I retrieve the product ID if I break up the table? if so can you post an answer based on your suggestion.
– Khandkar Asif Hossain
Mar 28 at 8:34
add a comment |
1 Answer
1
active
oldest
votes
Based on @vikschool comments. I have broken up the tables as follows:
CID Category
----------- --------------------------------------------------
1 Pen
2 Pencil
SID CID SubCategory
----------- ----------- --------------------------------------------------
1 1 BallPoint
2 2 HB
ID SID CID Item
----------- ----------- ----------- --------------------------------------------------
1 1 1 Matadoor
2 2 2 Natraz
Category SubCategory Item
-------------- -------------------------------------------------- -----------------------
Pen BallPoint Matadoor
Pencil HB Natraz
Here is the code that worked for me.
FormLoad:
private void Form1_Load(object sender, EventArgs e)
string query2 = "select * from Category";
string maincon = ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString;
SqlCommand com1 = new SqlCommand(query2, con);
con.Open();
SqlDataAdapter adp1 = new SqlDataAdapter(com1);
DataTable dt1 = new DataTable();
adp1.Fill(dt1);
comboBox1.ValueMember = "CID";
comboBox1.DisplayMember = "Category";
comboBox1.DataSource = dt1;
comboBox3.Enabled = false;
comboBox2.Enabled = false;
First ComboBox:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
if (comboBox1.SelectedValue != null)
string query2 = "select * from SubCategory where CID=@CID";
SqlCommand com1 = new SqlCommand(query2, con);
com1.Parameters.AddWithValue("@CID", comboBox1.SelectedValue);
SqlDataAdapter adp1 = new SqlDataAdapter(com1);
DataTable dt1 = new DataTable();
adp1.Fill(dt1);
comboBox2.ValueMember = "SID";
comboBox2.DisplayMember = "SubCategory";
comboBox2.DataSource = dt1;
comboBox3.Enabled = false;
comboBox2.Enabled = true;
Second ComboBox:
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
if (comboBox2.SelectedValue != null)
string query2 = "select * from Item where SID=@ID";
SqlCommand com1 = new SqlCommand(query2, con);
com1.Parameters.AddWithValue("@SID", comboBox2.SelectedValue);
SqlDataAdapter adp1 = new SqlDataAdapter(com1);
DataTable dt1 = new DataTable();
adp1.Fill(dt1);
comboBox3.ValueMember = "ID";
comboBox3.DisplayMember = "Item";
comboBox3.DataSource = dt1;
con.Close();
comboBox3.Enabled = true;
comboBox2.Enabled = true;
Third ComboBox:
private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
textBox1.Text = Convert.ToString(comboBox3.SelectedValue);
Note: I have tried this with Access database. Sql should work with above code. Please let me know if anyone find any problem using this piece of code.
Happy Coding.
Thanks to @vikschool.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55331159%2fis-there-a-way-to-pick-up-id-after-cascading-multiple-combobox-without-using-id%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Based on @vikschool comments. I have broken up the tables as follows:
CID Category
----------- --------------------------------------------------
1 Pen
2 Pencil
SID CID SubCategory
----------- ----------- --------------------------------------------------
1 1 BallPoint
2 2 HB
ID SID CID Item
----------- ----------- ----------- --------------------------------------------------
1 1 1 Matadoor
2 2 2 Natraz
Category SubCategory Item
-------------- -------------------------------------------------- -----------------------
Pen BallPoint Matadoor
Pencil HB Natraz
Here is the code that worked for me.
FormLoad:
private void Form1_Load(object sender, EventArgs e)
string query2 = "select * from Category";
string maincon = ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString;
SqlCommand com1 = new SqlCommand(query2, con);
con.Open();
SqlDataAdapter adp1 = new SqlDataAdapter(com1);
DataTable dt1 = new DataTable();
adp1.Fill(dt1);
comboBox1.ValueMember = "CID";
comboBox1.DisplayMember = "Category";
comboBox1.DataSource = dt1;
comboBox3.Enabled = false;
comboBox2.Enabled = false;
First ComboBox:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
if (comboBox1.SelectedValue != null)
string query2 = "select * from SubCategory where CID=@CID";
SqlCommand com1 = new SqlCommand(query2, con);
com1.Parameters.AddWithValue("@CID", comboBox1.SelectedValue);
SqlDataAdapter adp1 = new SqlDataAdapter(com1);
DataTable dt1 = new DataTable();
adp1.Fill(dt1);
comboBox2.ValueMember = "SID";
comboBox2.DisplayMember = "SubCategory";
comboBox2.DataSource = dt1;
comboBox3.Enabled = false;
comboBox2.Enabled = true;
Second ComboBox:
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
if (comboBox2.SelectedValue != null)
string query2 = "select * from Item where SID=@ID";
SqlCommand com1 = new SqlCommand(query2, con);
com1.Parameters.AddWithValue("@SID", comboBox2.SelectedValue);
SqlDataAdapter adp1 = new SqlDataAdapter(com1);
DataTable dt1 = new DataTable();
adp1.Fill(dt1);
comboBox3.ValueMember = "ID";
comboBox3.DisplayMember = "Item";
comboBox3.DataSource = dt1;
con.Close();
comboBox3.Enabled = true;
comboBox2.Enabled = true;
Third ComboBox:
private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
textBox1.Text = Convert.ToString(comboBox3.SelectedValue);
Note: I have tried this with Access database. Sql should work with above code. Please let me know if anyone find any problem using this piece of code.
Happy Coding.
Thanks to @vikschool.
add a comment |
Based on @vikschool comments. I have broken up the tables as follows:
CID Category
----------- --------------------------------------------------
1 Pen
2 Pencil
SID CID SubCategory
----------- ----------- --------------------------------------------------
1 1 BallPoint
2 2 HB
ID SID CID Item
----------- ----------- ----------- --------------------------------------------------
1 1 1 Matadoor
2 2 2 Natraz
Category SubCategory Item
-------------- -------------------------------------------------- -----------------------
Pen BallPoint Matadoor
Pencil HB Natraz
Here is the code that worked for me.
FormLoad:
private void Form1_Load(object sender, EventArgs e)
string query2 = "select * from Category";
string maincon = ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString;
SqlCommand com1 = new SqlCommand(query2, con);
con.Open();
SqlDataAdapter adp1 = new SqlDataAdapter(com1);
DataTable dt1 = new DataTable();
adp1.Fill(dt1);
comboBox1.ValueMember = "CID";
comboBox1.DisplayMember = "Category";
comboBox1.DataSource = dt1;
comboBox3.Enabled = false;
comboBox2.Enabled = false;
First ComboBox:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
if (comboBox1.SelectedValue != null)
string query2 = "select * from SubCategory where CID=@CID";
SqlCommand com1 = new SqlCommand(query2, con);
com1.Parameters.AddWithValue("@CID", comboBox1.SelectedValue);
SqlDataAdapter adp1 = new SqlDataAdapter(com1);
DataTable dt1 = new DataTable();
adp1.Fill(dt1);
comboBox2.ValueMember = "SID";
comboBox2.DisplayMember = "SubCategory";
comboBox2.DataSource = dt1;
comboBox3.Enabled = false;
comboBox2.Enabled = true;
Second ComboBox:
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
if (comboBox2.SelectedValue != null)
string query2 = "select * from Item where SID=@ID";
SqlCommand com1 = new SqlCommand(query2, con);
com1.Parameters.AddWithValue("@SID", comboBox2.SelectedValue);
SqlDataAdapter adp1 = new SqlDataAdapter(com1);
DataTable dt1 = new DataTable();
adp1.Fill(dt1);
comboBox3.ValueMember = "ID";
comboBox3.DisplayMember = "Item";
comboBox3.DataSource = dt1;
con.Close();
comboBox3.Enabled = true;
comboBox2.Enabled = true;
Third ComboBox:
private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
textBox1.Text = Convert.ToString(comboBox3.SelectedValue);
Note: I have tried this with Access database. Sql should work with above code. Please let me know if anyone find any problem using this piece of code.
Happy Coding.
Thanks to @vikschool.
add a comment |
Based on @vikschool comments. I have broken up the tables as follows:
CID Category
----------- --------------------------------------------------
1 Pen
2 Pencil
SID CID SubCategory
----------- ----------- --------------------------------------------------
1 1 BallPoint
2 2 HB
ID SID CID Item
----------- ----------- ----------- --------------------------------------------------
1 1 1 Matadoor
2 2 2 Natraz
Category SubCategory Item
-------------- -------------------------------------------------- -----------------------
Pen BallPoint Matadoor
Pencil HB Natraz
Here is the code that worked for me.
FormLoad:
private void Form1_Load(object sender, EventArgs e)
string query2 = "select * from Category";
string maincon = ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString;
SqlCommand com1 = new SqlCommand(query2, con);
con.Open();
SqlDataAdapter adp1 = new SqlDataAdapter(com1);
DataTable dt1 = new DataTable();
adp1.Fill(dt1);
comboBox1.ValueMember = "CID";
comboBox1.DisplayMember = "Category";
comboBox1.DataSource = dt1;
comboBox3.Enabled = false;
comboBox2.Enabled = false;
First ComboBox:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
if (comboBox1.SelectedValue != null)
string query2 = "select * from SubCategory where CID=@CID";
SqlCommand com1 = new SqlCommand(query2, con);
com1.Parameters.AddWithValue("@CID", comboBox1.SelectedValue);
SqlDataAdapter adp1 = new SqlDataAdapter(com1);
DataTable dt1 = new DataTable();
adp1.Fill(dt1);
comboBox2.ValueMember = "SID";
comboBox2.DisplayMember = "SubCategory";
comboBox2.DataSource = dt1;
comboBox3.Enabled = false;
comboBox2.Enabled = true;
Second ComboBox:
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
if (comboBox2.SelectedValue != null)
string query2 = "select * from Item where SID=@ID";
SqlCommand com1 = new SqlCommand(query2, con);
com1.Parameters.AddWithValue("@SID", comboBox2.SelectedValue);
SqlDataAdapter adp1 = new SqlDataAdapter(com1);
DataTable dt1 = new DataTable();
adp1.Fill(dt1);
comboBox3.ValueMember = "ID";
comboBox3.DisplayMember = "Item";
comboBox3.DataSource = dt1;
con.Close();
comboBox3.Enabled = true;
comboBox2.Enabled = true;
Third ComboBox:
private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
textBox1.Text = Convert.ToString(comboBox3.SelectedValue);
Note: I have tried this with Access database. Sql should work with above code. Please let me know if anyone find any problem using this piece of code.
Happy Coding.
Thanks to @vikschool.
Based on @vikschool comments. I have broken up the tables as follows:
CID Category
----------- --------------------------------------------------
1 Pen
2 Pencil
SID CID SubCategory
----------- ----------- --------------------------------------------------
1 1 BallPoint
2 2 HB
ID SID CID Item
----------- ----------- ----------- --------------------------------------------------
1 1 1 Matadoor
2 2 2 Natraz
Category SubCategory Item
-------------- -------------------------------------------------- -----------------------
Pen BallPoint Matadoor
Pencil HB Natraz
Here is the code that worked for me.
FormLoad:
private void Form1_Load(object sender, EventArgs e)
string query2 = "select * from Category";
string maincon = ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString;
SqlCommand com1 = new SqlCommand(query2, con);
con.Open();
SqlDataAdapter adp1 = new SqlDataAdapter(com1);
DataTable dt1 = new DataTable();
adp1.Fill(dt1);
comboBox1.ValueMember = "CID";
comboBox1.DisplayMember = "Category";
comboBox1.DataSource = dt1;
comboBox3.Enabled = false;
comboBox2.Enabled = false;
First ComboBox:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
if (comboBox1.SelectedValue != null)
string query2 = "select * from SubCategory where CID=@CID";
SqlCommand com1 = new SqlCommand(query2, con);
com1.Parameters.AddWithValue("@CID", comboBox1.SelectedValue);
SqlDataAdapter adp1 = new SqlDataAdapter(com1);
DataTable dt1 = new DataTable();
adp1.Fill(dt1);
comboBox2.ValueMember = "SID";
comboBox2.DisplayMember = "SubCategory";
comboBox2.DataSource = dt1;
comboBox3.Enabled = false;
comboBox2.Enabled = true;
Second ComboBox:
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
if (comboBox2.SelectedValue != null)
string query2 = "select * from Item where SID=@ID";
SqlCommand com1 = new SqlCommand(query2, con);
com1.Parameters.AddWithValue("@SID", comboBox2.SelectedValue);
SqlDataAdapter adp1 = new SqlDataAdapter(com1);
DataTable dt1 = new DataTable();
adp1.Fill(dt1);
comboBox3.ValueMember = "ID";
comboBox3.DisplayMember = "Item";
comboBox3.DataSource = dt1;
con.Close();
comboBox3.Enabled = true;
comboBox2.Enabled = true;
Third ComboBox:
private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
textBox1.Text = Convert.ToString(comboBox3.SelectedValue);
Note: I have tried this with Access database. Sql should work with above code. Please let me know if anyone find any problem using this piece of code.
Happy Coding.
Thanks to @vikschool.
answered Mar 28 at 9:19
Khandkar Asif HossainKhandkar Asif Hossain
507
507
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55331159%2fis-there-a-way-to-pick-up-id-after-cascading-multiple-combobox-without-using-id%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
if you are going to deal with
strings
(which I would not recommend) then why not create another query to fetch theProductID
from theproduct
table with awhere
condition ofCategory=<SomeCategoryValue> and SubCategory=<SomeSubCategoryValue> and Item=<SomeItemValue>;
– vikscool
Mar 25 at 4:36
I would have but I need to fetch the ProductID and Products table will have a large number of data with many categories, subcategories and items. To offer a simpler way to select the ProductID cascading comboBox is the way to go in my opinion. Because to use your suggested query I have use either exact or approximate(using wildcards) SQL parameters value which is quite hard to do where the database has lots of products.
I would like to hear about your recommendation about not using strings
@vikscool– Khandkar Asif Hossain
Mar 25 at 12:07
1
Sorry for the delayed response,1st
As for why I would not recommend the String?
What if you have twoitems
with the same name in differentsubcategories
and the way you are getting theitems
it would be hard for you to identify whichitem
belongs to whichsubcategory
(as your query is based onCategory
and notCategory+SubCategory
).– vikscool
Mar 27 at 4:10
1
2nd Your demo table looks like its a
Joined Result
. So, if you have other master tables forCategory
andSubCategory
, it would be easier to make the cascading where theitem
table will have its parentSubCategoryId
and the SubCategory would have its parentCategoryId
. Hence, the relation would become something like this:Category->get SubCategory based on the categoryId->get Items based on the subcategoryId
.– vikscool
Mar 27 at 4:12
Thanks @vikscool. First of all your assumption of the table being join table is incorrect. The table i provided is in fact a single table. And you are suggesting to break up the table. But can I retrieve the product ID if I break up the table? if so can you post an answer based on your suggestion.
– Khandkar Asif Hossain
Mar 28 at 8:34