Click here to Skip to main content
15,903,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I'm new to asp.net and I need some help, please. I'm using c# and sql.

What I want to do is the following: I have a details view where I can use it to add Employees and save it to my database in Employee table. Within the details view, I'v created a template field for dept_id (this field is a foreign key). I want to make it as a drop down list where you can choose the department name (which is in the Department table) instead of entering the department id. I'm very stick in this. Any help will be appreciated.

This is the .aspx
ASP.NET
<asp:TemplateField HeaderText="Dept_id" SortExpression="Dept_id">
    <EditItemTemplate>
        <asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="ddlSqlDataSource"
        DataTextField="dept_name" DataValueField="dept_id" SelectedValue='<%# Bind("dept_id") %>'>
        </asp:DropDownList>
    </EditItemTemplate>


    <InsertItemTemplate>
        <asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="ddlSqlDataSource"
        DataTextField="dept_name" DataValueField="dept_id" SelectedValue='<%# Bind("dept_id") %>'>
        </asp:DropDownList>
    </InsertItemTemplate>
    <ItemTemplate>
        <asp:Label ID="Label5" runat="server" Text='<%# Bind("Dept_id") %>'></asp:Label>
    </ItemTemplate>
</asp:TemplateField>


I'm not sure what should I write inside the dropdownlist control and what to write in its SqlDataSource (ddlSqlDataSource)

ASP.NET
<asp:SqlDataSource ID="ddlSqlDataSource" runat="server" 
                ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
                SelectCommand="SELECT [dept_name] FROM [Department]">
            </asp:SqlDataSource>


Could any one help please!
Thanks in advance.

----------

Issues Solved!
Thanks all!

Here is the modified code:

<asp:TemplateField HeaderText="Dept_id" SortExpression="Dept_id">
                          <ItemTemplate>
                          <%#Eval("dept_id") %>
                          </ItemTemplate>

                           <InsertItemTemplate>
                               <asp:DropDownList ID="ddlDept" runat="server" DataSourceID="ddlSqlDataSource"
                               DataTextField="dept_name" DataValueField="dept_id" SelectedValue='<%# Bind("Dept_id") %>'>
                               </asp:DropDownList>
                                <asp:SqlDataSource ID="ddlSqlDataSource" runat="server"
                  ConnectionString="<%$ ConnectionStrings:ConnectionString %>"

                   SelectCommand="SELECT dept_id, dept_name FROM Department ">

               </asp:SqlDataSource>
                           </InsertItemTemplate>
                           <ItemTemplate>
                               <asp:Label ID="Label5" runat="server" Text='<%# Bind("Dept_id") %>'></asp:Label>
                           </ItemTemplate>
                       </asp:TemplateField>


And I added these to lines to code behind:

C#
protected void DetailsView2_DataBound(object sender, EventArgs e)
        {
            DropDownList ddlDept = (DropDownList)DetailsView2.FindControl("ddlDept");

            ddlDept.Items.Insert(0, new ListItem("Select department"));

        }
Posted
Updated 12-Jan-19 7:24am
v2
Comments
Gokulprasad05 7-Nov-15 1:55am    
you have two diferent tables one is employee table and another one is department table.. which value did u want to bind in dropdown list
Dana.S 7-Nov-15 3:55am    
I want to bind the department table so I can choose the department name from the dropdownlist , then save the dept_id to the employee table based on the chosen name. Is it clear ?
Now the dropddownlist is getting bound or not?
Dana.S 7-Nov-15 3:56am    
No it's not
Gokulprasad05 7-Nov-15 4:02am    
analysis both tables, anyone of the column name or gid value should be same both the table in your db. Then put left join

You can try the technique of binding the dropdown in DataBound[^] Event. Refer - Bind DropDownList in ItemTemplate of TemplateField in ASP.Net GridView[^]. See how a dropdownlist is bound inside the RowDataBound Event of GridView. You need to do something similar.
 
Share this answer
 
Comments
Dana.S 7-Nov-15 3:36am    
Thanks a lot for your reply.
Actually I'v seen this link before and I'v tried to implement it. However I am stuck with this part:

if (e.Row.RowType == DataControlRowType.DataRow)

I don't know how to apply this for a detailsview.
Do you have any idea?

Thanks again. Appreciate your response.
That check is not required actually. You can start with FindControl directly.

Also execute that query in Sql Studio to check if it correctly returns any record or not.

You can check another blog, which uses SqlDataSource - Bind a DetailsView control with a DropDownList control.
Dana.S 7-Nov-15 8:38am    
Thanks a lot!
I'v solved the problem and it works properly now!
Links were so helpful!
I'v posted the code after modifications.
Many thanks. Appreciate your help!
Great. Congrats !!! :)
What I did is instead of writing code, I changed the data source's SQL that fills the DropDownList allowing me to either select or empty the value.

SelectCommand="SELECT NULL AS dept_id, NULL AS dept_name UNION SELECT dept_id, dept_name FROM Department "
 
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