Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
Hello everyone,
It is a difficult problem for me, Actually I have TWO TABLES in SQL Server Database,

1. "Category" Table:
----------------------
Cate_id	  Category
  1	   Books
  2	   Cd's
  3	   DVD's
  4	   Files
----------------------

2. "Sub_ Category" Table:
-----------------------------------------
Sub_Cate_id	Sub_Category	Cate_id
  1       	 I.T Books	  1
  2	         Asp.Net Book	  1
  3	         Java Books	  1
  4	         Music CD	  2
  5	         Movie CD	  2
  6	         Songs CD	  2
-----------------------------------------

/* Selet Query*/
select cat_name, sub_name from catagories c inner join catagories_sub sc on c.cat_id = sc.cat_id

for suppose, Selected category is Books, So, I.T Books, Asp.Net Books & Java Books are "Sub-Categories" of "Books"

Problem:
The Problem is that, In Visual Studio, when I finish adding "Category" to the SQL database,
Now, I found form to add sub-category, also there is a DropDownList (which Previously Selected Categories from SQL "Category Table"). When user write a new 'Sub_Category' in textbox and click to add it, HOW can I get the selected Item, Id(Saved in "Category" SQL table), and then save it in sub-category table

I don't know I explain my question properly or not, Sorry for mistakes.

Thanks for Helping Me!
Posted
Updated 16-Oct-12 13:48pm
v2
Comments
fjdiewornncalwe 16-Oct-12 19:47pm    
You do explain your question properly, but we are not going to do your homework for you. We will help you if you have a specific coding question, but we will NOT do the work for you.

If you have load data in DropDownList as below,
DropDownList1.DataSource = DtCategory
DropDownList1.ValueMember = "Cate_id"
DropDownList1.DisplayMember = "Category"

then,dropdownlist will have value as below
dropdownIndex ValueMember Displaymember
0              1	   Books
1              2	   Cd's
2              3	   DVD's
3              4	   Files

so,when you select DVD's,
DropDownList1.SelectedIndex will be 2

and
DropDownList1.SelectedValue will be 3

and you need second value to save in database
so, use DropDownList1.SelectedValue for fetch cate_Id and save it in database while adding Subcategory

Happy coding!
:)
 
Share this answer
 
v2
For getting the CategoryID you have to specify the name of your Category Dropdownlist, like as follows

C#
ddlCategory.SelectedValue


You will get the value of your category and save it in database.

Thanks
 
Share this answer
 
Thank you all for Answers & Suggestions,

But, I also want to share one more Answer of my question, which I found at other Asp Forum, If any one also have to face that problem or beginner to Asp follow that Answer,

First Create a Method for filling DropDownList:
C#
private void FillCatagoryList()
    {
        // Filling the Drop DownListItems //
        try
        {       
            // Using Connection String from web.config File //
            SqlConnection sqlcon = new SqlConnection (ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString); //    
            sqlcon.Open();
            DataSet ds = new DataSet();
            SqlCommand cmd = new SqlCommand("select * from catagories", sqlcon);
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            adapter.Fill(ds);
            List_Catagories.DataSource = ds;
            List_Catagories.DataTextField = "cat_name";
            List_Catagories.DataValueField = "cat_id";
            List_Catagories.DataBind();
        }
        catch (Exception err)
        {
            lblResults.Text = "Error reading list of names. ";
            lblResults.Text += err.Message;
            // The Code below Shows the Alert Box
            // Response.Write("<script>alert('No Image Selected!')</script>");
        }
        finally
        {
            sqlcon.Close();
        }

Previous Code will save temprory both Category Id & Category Name, and shows Category Name in "DropDownList" from "List_Catagories.DataTextField" and save "Cat_id" in "List_Catagories.DataValueField".

Then Save the New Sub-Category Item(User Input) and its code(From DropDownList Selected Item) using the following code:

C#
try
{
// Using Connection String from web.config File //
SqlConnection sqlcon = new SqlConnection (ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString); //    
sqlcon.Open();
string nam = txtSubCategory.Text.Trim();
int Sel_id = Convert.ToInt32(List_Catagories.SelectedValue);
SqlCommand cmd  = new SqlCommand("INSERT INTO catagories_sub (category_id, Category_Name) values ("+ Sel_id +", '"+ nam +"')", sqlcon);
cmd.ExecuteNonQuery();
}
catch (Exception err)
{
lblResults.Text = "Error reading list of names. ";
lblResults.Text += err.Message;
}
finally
{
sqlcon.Close();
}


So, That's the Solution, and it will save a Category id of Category table to the sub category, if we select through inner join, the result will give us the category and sub-category matched record, I am pasting my SQL Query so you can execute it easiy,
SQL
select cat_name, sub_name from category c inner join sub_category sc on c.cate_id = sc.cate_id


Thanks,
Regards,
Mustafa Iqbal.
 
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