Foreign Key insert - getting value from user and want to insert identity value

Posted by Nerdanalysis under Sql Server on 4/17/2012 | Points: 10 | Views : 1044 | Status : [Member] | Replies : 3
Hi,

I have 3 tables "category, subcategory and article" in the database. I designed a page where the user can select the category from the dropdown list and on the basis of that dropdown list2 show their subcategories. I want to save that captured information into the database. Now the problem is that...I am getting the subcategory name rather than subcategoryID through the dropdown and then how to save the subcategory name?

Should I make the stored procedure and get the subcategory id on the basis of subcat. name....or should i swap subcategoryid column to subcategoryname column...

Table :- Article has following fields

articleId int Unchecked
subCategoryId int Unchecked
articleTitle varchar(500) Unchecked
articleContent ntext Unchecked
templateId int Unchecked
upperContent ntext Unchecked

Table SubCategory has
subCategoryId int Unchecked
categoryId int Unchecked
name varchar(150) Unchecked

table Category has
categoryId int Unchecked
name varchar(150) Unchecked

Thanks

Kind regards
Nerd




Responses

Posted by: dhirenkaunar-15094 on: 4/18/2012 [Member] Starter | Points: 25

Up
0
Down
Hi Nerdanalysis,

Please assign your subcategoryID in the value attribute and subcategoryName in the Text attibute then you will get the corresponding subcategoryID if you select any sub category .. Please find the below sample

<asp:DropDownList ID="ColorList" AutoPostBack="True" OnSelectedIndexChanged="Selection_Change"
runat="server">
<asp:ListItem Selected="True" Value="White"> White </asp:ListItem>
<asp:ListItem Value="Silver"> Silver 1 </asp:ListItem>
<asp:ListItem Value="DarkGray"> Dark Gray 1 </asp:ListItem>
<asp:ListItem Value="Khaki"> Khaki 1</asp:ListItem>
<asp:ListItem Value="DarkKhaki"> Dark Khaki1 </asp:ListItem>
</asp:DropDownList>

If you are populating value from the data base on the OnSelectedIndexChanged event of category list then assign subcategoryName to DataTextField attribute and subcategoryID to DataValueField @ the time of binding.

<asp:DropDownList id="DropDownList1" runat="server"
DataSource="<% databindingexpression %>"
DataTextField="DataSourceField"
DataValueField="DataSourceField"
AutoPostBack="True|False"
OnSelectedIndexChanged="OnSelectedIndexChangedMethod">

Bind using code behind :
subCatList.DataTextField = "subcategoryName ";
subCatList.DataValueField = "subcategoryID";

No need to write SP for getting sub category id..
Please reply me if this helps you or you need some more clarification.
If this satisfy your requirement please mark as answer.


Thanks & Rgards,
Dhiren Kumar Kaunar

Nerdanalysis, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nerdanalysis on: 4/18/2012 [Member] Starter | Points: 25

Up
0
Down
Hi Dhiren,

Thanks for your reply... I couldnt get what is :- DataSource="<% databindingexpression %>" .

I wrote the stored procedure by declaring the id variable and find the id on the basis of category.

Kind regards
Nerd

Nerdanalysis, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: dhirenkaunar-15094 on: 4/18/2012 [Member] Starter | Points: 25

Up
0
Down
Welcome Nerd,

If you don't mind could you please share the stored procedure which you have write and what's your exact problem .

DataSource="<% databindingexpression %>" this is just a example : How to bind a DataSource in Drop down,

Thanks & Rgards,
Dhiren Kumar Kaunar

Nerdanalysis, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response