locked
drop down list RRS feed

  • Question

  • User-1085821416 posted

    Employee table

    EmployeeID----PK

    FName

    LName 

    Email

    Address

     

    Tools used

    UsedID    ---PK

    EmployeeID   ----FK

    ToolID     ------FK

     

    Tools

    ToolsID ---PK

    Model

    Used For

            I have the employee table used by form view but I need to be able to have an option to use a drop down list so employees can check out more than one tool. When i run the debugger the applicants table is being populated however  the drop down list  Im using does not populate the tools used table.

     

     

    Please Help

    Friday, May 28, 2010 12:13 PM

Answers

  • User-1460196090 posted

    Here is sample code on how you can do this:

        protected void FormView1_ItemInserted(object sender, FormViewInsertedEventArgs e)
        {
            if (e.AffectedRows != -1) //or e.AffectedRows != 0 if you want
            {
                ListBox lBox = FormView1.FindControl("ListBox3") as ListBox;
                string selected = lBox.SelectedItem.Text;
                SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=MyDatabase;Integrated Security=True");
                SqlCommand cmd = new SqlCommand("INSERT INTO Tools (@EmployeeID, ToolID) VALUES (@EmpId, @ToolId)", con);
                cmd.CommandType = CommandType.Text;            
                cmd.Parameters.AddWithValue("@ToolId", e.Values[0].ToString()); //e.Values[0].ToString() will get the first (zero based index) value that is inserted (probably ID).
                cmd.Parameters.AddWithValue("@EmpId", lBox.SelectedItem.Value); //will get the ListBox selectedItem value, this is the ID of the ToolsID exactly.
                using (con)
                {
                    con.Open();
                    int result = cmd.ExecuteNonQuery();
                    if (result != -1)
                    {
                        //successfully inserted!
                    }
                    else
                    {
                        //not successfull
                    }
                }
            }
        }


    Replace the connection string with yours (probably you have in in the ObjectDatasource method).

    Drop breakpoing inside the code and observe whats really happening. You will success!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 3, 2010 6:19 PM
  • User-1085821416 posted
    I entered the code as recommended however its still not able to input data into the tools used table
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 7, 2010 1:35 PM
  • User-1085821416 posted

    Well i sort of got it working.When i enter the employeeID the first time into the formview the tools used table does not get populated however when i enter the same empoloyeeID i get a duplicate PK error but when i check the tools used table all the info gets populated.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 11, 2010 11:39 AM
  • User-1085821416 posted

    well i fianly got some time to look at the issues and i want to thank Hajan for all the help.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 18, 2010 10:32 AM

All replies

  • User-1802908944 posted

     <asp:FormView ID="fvTax" runat="server" AllowPaging="True" DataKeyNames="Tax_id"
                            DataSourceID="dsTax" Width="100%">

    need to add a formview like this

     <asp:FormView ID="fvTax" runat="server" AllowPaging="True" DataKeyNames="Tax_id"

                            DataSourceID="dsTax" Width="100%">

    and add a dropdownlist in <EditItemTemplate>

       <asp:DropDownList ID="ddlEnquiryMode" runat="server" CssClass="ddcss" TabIndex="2" DataSourceID="dsEnquiry" DataTextField="EQ_Mode" DataValueField="Eq_Mode_Id" >

                                </asp:DropDownList>


    add the two SQLdatasource one for DropDownlist and other for Fromview

      <asp:SqlDataSource ID="dsEnquiry" runat="server" ConnectionString="<%$ ConnectionStrings:dbCIMSConnectionString %>"


                            SelectCommand="SELECT [Eq_Mode_Id], [EQ_Mode] FROM [tblEnquiryMode]"></asp:SqlDataSource>

                        <asp:SqlDataSource ID="dstax" runat="server" ConnectionString="<%$ ConnectionStrings:dbCIMSConnectionString %>"

                            SelectCommand="SELECT [User_Id], [User_Name] FROM [tblUserMaster] WHERE  ([Designation] = 'C') ORDER BY [User_Id]">

                        </asp:SqlDataSource>


    set your query and it will popup

    Friday, May 28, 2010 2:36 PM
  • User-1460196090 posted

    Well, you probably need to populate DropDownList values from Tools table?

    Here are some links in order to help you understand filtering using DropDownLists

    http://www.asp.net/data-access/tutorials/master-detail-filtering-with-a-dropdownlist-cs

    Here you can see how you may programatically populate DropDownList from Database: http://www.dbtutorials.com/advanced/populatingdropdown-csharp.aspx

    Moreover, you can populate values to your DropDownList the same way you have populated for your FormView. Use the smart arrow in your DropDownList's top-right corner - Choose Data Source - then follow the Wizard Steps.

    If there is something else you need to do/know, please provide more details and maybe some codes you use will help us provide better answers.

    Thanks

    Friday, May 28, 2010 2:39 PM
  • User-1085821416 posted

    I added a listbox within the employee formview. I Selected the data field i wanted to be displayed in the list box. When i run the program im able to enter all the employee information and im able to select the tools needed to be checked out.When i check the employee table it get populated from the previous user input however nothing is is being populated in the tools used table.

    Wednesday, June 2, 2010 3:44 PM
  • User-1460196090 posted

    Can you show us how do you do that using code?


    Wednesday, June 2, 2010 6:05 PM
  • User-1085821416 posted

    <%@ Page Language="VB" MasterPageFile="~/Site.master" AutoEventWireup="false" CodeFile="NewApplicants.aspx.vb" Inherits="Employee_Declarative" title="Untitled Page" %>

    <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server">

        <asp:Panel ID="Panel1" runat="server">

        </asp:Panel>

        <asp:FormView ID="FormView1" runat="server" DataKeyNames="EmployeeID"

        DataSourceID="ObjectDataSource1" DefaultMode="Insert" Width="562px">

        <EditItemTemplate>

            EmployeeID:

            <asp:Label ID="EmployeeIDLabel1" runat="server" Text='<%# Eval("EmployeeID") %>' />

            <br />

            MrMs:

            <asp:TextBox ID="MrMsTextBox" runat="server" Text='<%# Bind("MrMs") %>' />

            <br />

            Fname:

            <asp:TextBox ID="FnameTextBox" runat="server" Text='<%# Bind("Fname") %>' />

            <br />

            Lname:

            <asp:TextBox ID="LnameTextBox" runat="server" Text='<%# Bind("Lname") %>' />

            <br />

           

            Telephone:

            <asp:TextBox ID="TelephoneTextBox" runat="server"

                Text='<%# Bind("Telephone") %>' />

            <br />

            DOB:

            <asp:TextBox ID="DOBTextBox" runat="server" Text='<%# Bind("DOB") %>' />

            <br />

           

            <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True"

                CommandName="Update" Text="Update" />

            &nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server"

                CausesValidation="False" CommandName="Cancel" Text="Cancel" />

        </EditItemTemplate>

        <InsertItemTemplate>

            EmployeeID:

            <asp:TextBox ID=" EmployeeIDTextBox" runat="server" Text='<%# Bind("EmployeeID ") %>'

                Height="18px" Width="56px" />

            <br />

            MrMs:

            <asp:DropDownList ID="DropDownList1" runat="server"

                DataSource='<%# Bind("MrMs") %>'

                SelectedValue='<%# Bind("MrMs", "{0}") %>' style="margin-bottom: 0px">

            <asp:ListItem Value="Mr" Text="Mr"/>

             <asp:ListItem Value="Ms" Text="Ms"/>

     

            </asp:DropDownList>

            <br />

            Fname:

            <asp:TextBox ID="FnameTextBox" runat="server" Text='<%# Bind("Fname") %>' />

            LName:<asp:TextBox ID="LnameTextBox" runat="server" Text='<%# Bind("Lname") %>' />

           

            <br />

            Telephone:

            <asp:TextBox ID="TelephoneTextBox" runat="server"

                Text='<%# Bind("Telephone") %>' />

            <br />

     

            <asp:Panel ID="Panel2" runat="server">

                <asp:ListBox ID="ListBox3" runat="server" DataSourceID="ObjectDataSource1"

                    DataTextField="ToolsID" DataValueField="ToolsID"></asp:ListBox>

                <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"

                    OldValuesParameterFormatString="original_{0}"

                    SelectMethod="GetToolsInfoData"

                    TypeName="CheckoutTableAdapters.TableAdapter">

                </asp:ObjectDataSource>

            </asp:Panel>

            <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True"

                CommandName="Insert" Text="Insert" />

    &nbsp;<asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False"

                CommandName="Cancel" Text="Cancel" />

        </InsertItemTemplate>

        <ItemTemplate>

            EmployeeID:

            <asp:Label ID=" EmployeeIDLabel" runat="server" Text='<%# Eval("EmployeeID ") %>' />

            <br />

            MrMs:

            <asp:Label ID="MrMsLabel" runat="server" Text='<%# Bind("MrMs") %>' />

            <br />

            Fname:

            <asp:Label ID="FnameLabel" runat="server" Text='<%# Bind("Fname") %>' />

            <br />

            Lname:

            <asp:Label ID="LnameLabel" runat="server" Text='<%# Bind("Lname") %>' />

            <br />

           

            Telephone:

            <asp:Label ID="TelephoneLabel" runat="server" Text='<%# Bind("Telephone") %>' />

            <br />

            DOB:

            <asp:Label ID="DOBLabel" runat="server" Text='<%# Bind("DOB") %>' />

            <br />

     <asp:LinkButton ID="NewButton" runat="server" CausesValidation="False"

                CommandName="New" Text="New" />

        </ItemTemplate>

    </asp:FormView>

    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"

        InsertMethod="InsertEmployeeQuery" OldValuesParameterFormatString="original_{0}"

        SelectMethod="GetEmployeeData"

        TypeName="CheckoutTableAdapters.EmployeesTableAdapter">

        <InsertParameters>

            <asp:Parameter Name="EmployeeID" Type="Int32" />

            <asp:Parameter Name="MrMs" Type="String" />

            <asp:Parameter Name="Fname" Type="String" />

            <asp:Parameter Name="Lname" Type="String" />

            <asp:Parameter Name="Telephone" Type="String" />

            <asp:Parameter Name="DOB" Type="DateTime" />

        </InsertParameters>

    </asp:ObjectDataSource>

           

        </asp:Content>

    Thursday, June 3, 2010 9:16 AM
  • User-1460196090 posted

    Ahm.

    Because the ListBox is separate from the FormView, which means, FormView will insert its values only, you will need to insert the listView yourself.

    You can do that using the FormView1_ItemInserted event.

    Example:

       protected void FormView1_ItemInserted(object sender, FormViewInsertedEventArgs e)
        {
            if (e.AffectedRows != -1) //or e.AffectedRows != 0 if you want
            {
                ListBox lBox = FormView1.FindControl("ListBox3") as ListBox;
                string selected = lBox.SelectedItem.Text;
            }
        }


    Using above code, you can find the ListBox and get the SelectedValue.

    After that, you can continue doing CustomInsert code to insert the values in your Tools used table.

    Hope this helps.


    Thursday, June 3, 2010 10:19 AM
  • User-1085821416 posted

    well i did the following but no luck


       

     Protected Sub FormView1_ItemInserted(ByVal sender As Object, ByVal e As FormViewInsertedEventArgs)
    
            If e.AffectedRows <> 0 Then 'or e.AffectedRows != 0 if you want     
    
                Dim lBox As ListBox = TryCast(FormView1.FindControl("ListBox3"), ListBox)
    
                Dim selected As String = lBox.SelectedItem.Text
    
            End If
        End Sub
    


     

     

    Thursday, June 3, 2010 1:22 PM
  • User-1460196090 posted

    There you need to work with SqlConnection, SqlCommand and add the values to your database.

    If you are not sure how to do this, construct your 'database query' and reply back. I will guide you to do the INSERT command.

    Thursday, June 3, 2010 1:31 PM
  • User-1085821416 posted

     This is the query from tools used table adapter. Thanks for all your help so far. Its been over 5 years since i last did any code:)

    SELECT DISTINCT ToolsID, Model, UsedFor
    FROM            tools
    
    


    Thursday, June 3, 2010 2:06 PM
  • User-1460196090 posted

    Here is sample code on how you can do this:

        protected void FormView1_ItemInserted(object sender, FormViewInsertedEventArgs e)
        {
            if (e.AffectedRows != -1) //or e.AffectedRows != 0 if you want
            {
                ListBox lBox = FormView1.FindControl("ListBox3") as ListBox;
                string selected = lBox.SelectedItem.Text;
                SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=MyDatabase;Integrated Security=True");
                SqlCommand cmd = new SqlCommand("INSERT INTO Tools (@EmployeeID, ToolID) VALUES (@EmpId, @ToolId)", con);
                cmd.CommandType = CommandType.Text;            
                cmd.Parameters.AddWithValue("@ToolId", e.Values[0].ToString()); //e.Values[0].ToString() will get the first (zero based index) value that is inserted (probably ID).
                cmd.Parameters.AddWithValue("@EmpId", lBox.SelectedItem.Value); //will get the ListBox selectedItem value, this is the ID of the ToolsID exactly.
                using (con)
                {
                    con.Open();
                    int result = cmd.ExecuteNonQuery();
                    if (result != -1)
                    {
                        //successfully inserted!
                    }
                    else
                    {
                        //not successfull
                    }
                }
            }
        }


    Replace the connection string with yours (probably you have in in the ObjectDatasource method).

    Drop breakpoing inside the code and observe whats really happening. You will success!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 3, 2010 6:19 PM
  • User-1085821416 posted
    I entered the code as recommended however its still not able to input data into the tools used table
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 7, 2010 1:35 PM
  • User-1085821416 posted

    Well i sort of got it working.When i enter the employeeID the first time into the formview the tools used table does not get populated however when i enter the same empoloyeeID i get a duplicate PK error but when i check the tools used table all the info gets populated.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 11, 2010 11:39 AM
  • User-1085821416 posted

    well i fianly got some time to look at the issues and i want to thank Hajan for all the help.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 18, 2010 10:32 AM