Dropdownlist Problem

Discussion in 'ASP .Net Web Controls' started by Glenn L., Mar 30, 2005.

  1. Glenn L.

    Glenn L. Guest

    I am using the VS 2005 beta1. I am trying to add a Dropdownlist
    control that gets its data from one table and when selected saves the
    ID into another table. I have 2 tables TBLSTAFF and TBLRACE. TBLSTAFF
    has an integer field called RACEID. TBLRACE has the foreign key RACEID
    and RACEDESC(Description field). I set up a dropdown list as follows.


    <asp:DropDownList ID="DropStaff" Runat="server" Height="25px"
    Width="203px" DataSourceID="TBLRACE" DataValueField="RACEID"
    DataTextField="RACEDESC" >
    </asp:DropDownList>

    What I can't seem to figure out is how to bind this control to the
    other SQLDataSource called "TBLSTAFF" and its Field "RACEID".

    If any one can help me I would sure appreciate it very much. I imagine
    this has been done before but I cannot seem to find an example of it.

    Thanks,
    Glenn
     
    Glenn L., Mar 30, 2005
    #1
    1. Advertising

  2. Glenn L.

    Brock Allen Guest

    You should add a second DataSource control to your page that uses a ControlParameter
    for its SelectParameters whose source is from the SelectedItem from the first
    table (GridView I presume).

    -Brock
    DevelopMentor
    http://staff.develop.com/ballen



    > I am using the VS 2005 beta1. I am trying to add a Dropdownlist
    > control that gets its data from one table and when selected saves the
    > ID into another table. I have 2 tables TBLSTAFF and TBLRACE. TBLSTAFF
    > has an integer field called RACEID. TBLRACE has the foreign key
    > RACEID and RACEDESC(Description field). I set up a dropdown list as
    > follows.
    >
    > <asp:DropDownList ID="DropStaff" Runat="server" Height="25px"
    > Width="203px" DataSourceID="TBLRACE" DataValueField="RACEID"
    > DataTextField="RACEDESC" >
    > </asp:DropDownList>
    > What I can't seem to figure out is how to bind this control to the
    > other SQLDataSource called "TBLSTAFF" and its Field "RACEID".
    >
    > If any one can help me I would sure appreciate it very much. I imagine
    > this has been done before but I cannot seem to find an example of it.
    >
    > Thanks,
    > Glenn
     
    Brock Allen, Mar 30, 2005
    #2
    1. Advertising

  3. Glenn L.

    Glenn L. Guest

    I am not sure exactly what you are telling me to do. I already have 2
    SQLDataSources. I am trying to use a FormView. Below is the code for
    what I have done so far but it doesnt work as of yet I am missing
    something.

    <asp:FormView ID="FormView1" Runat="server" DataSourceID="TBLSTAFF"
    DataKeyNames="STAFFID">
    <EditItemTemplate>
    SSN:
    <asp:TextBox Text='<%# Bind("SSN") %>' Runat="server"
    ID="SSNTextBox"></asp:TextBox><br />
    FIRSTNAME:
    <asp:TextBox Text='<%# Bind("FIRSTNAME") %>'
    Runat="server" ID="FIRSTNAMETextBox"></asp:TextBox><br />
    LASTNAME:
    <asp:TextBox Text='<%# Bind("LASTNAME") %>'
    Runat="server" ID="LASTNAMETextBox"></asp:TextBox><br />
    MIDDLE:
    <asp:TextBox Text='<%# Bind("MIDDLE") %>'
    Runat="server" ID="MIDDLETextBox1"></asp:TextBox><br />
    <br />
    RACEID:
    <br />
    <asp:DropDownList ID="DropRace" Runat="server"
    Height="25px" Width="203px"
    DataSourceID="TBLRACE" DataValueField="RACEID"
    DataTextField="RACEDESC" >
    </asp:DropDownList>
    SEX:
    <asp:TextBox Text='<%# Bind("SEX") %>' Runat="server"
    ID="SEXTextBox"></asp:TextBox><br />
    STAFFID:
    <asp:Label Text='<%# Eval("STAFFID") %>' Runat="server"
    ID="STAFFIDLabel1">
    </asp:Label><br />
    <br />
    <asp:LinkButton ID="LinkButton2" Runat="server"
    CommandName="update">Save</asp:LinkButton>
    </EditItemTemplate>
    <ItemTemplate>
    SSN:
    <asp:Label Text='<%# Bind("SSN") %>' Runat="server"
    ID="SSNLabel">
    </asp:Label><br />
    FIRSTNAME:
    <asp:Label Text='<%# Bind("FIRSTNAME") %>'
    Runat="server" ID="FIRSTNAMELabel">
    </asp:Label><br />
    LASTNAME:
    <asp:Label Text='<%# Bind("LASTNAME") %>'
    Runat="server" ID="LASTNAMELabel">
    </asp:Label><br />
    MIDDLE:
    <asp:Label Text='<%# Bind("MIDDLE") %>' Runat="server"
    ID="MIDDLELabel">
    </asp:Label><br />
    RACEID:
    <asp:Label Text='<%# Bind("RACEID") %>' Runat="server"
    ID="RACEIDLabel">
    </asp:Label><br />
    SEX:
    <asp:Label Text='<%# Bind("SEX") %>' Runat="server"
    ID="SEXLabel">
    </asp:Label><br />
    STAFFID:
    <asp:Label Text='<%# Bind("STAFFID") %>' Runat="server"
    ID="STAFFIDLabel">
    </asp:Label><br />
    <br />
    <asp:LinkButton ID="LinkButton3" Runat="server"
    CommandName="Edit">Edit</asp:LinkButton>
    </ItemTemplate>
    </asp:FormView><br />
    <asp:SqlDataSource ID="TBLRACE" Runat="server"
    SelectCommand="SELECT RACEID, RACEDESC, RACECODE FROM dbo.TBLRACE"
    ConnectionString="<%$ ConnectionStrings:SalaryCNN %>">
    </asp:SqlDataSource>
    <asp:SqlDataSource ID="TBLSTAFF" Runat="server"
    ConnectionString="<%$ ConnectionStrings:SalaryCNN %>"
    SelectCommand="SELECT [SSN], [FIRSTNAME], [LASTNAME],
    [MIDDLE], [RACEID], [SEX], [STAFFID] FROM [TBLSTAFF] WHERE ([STAFFID] =
    @STAFFID)"
    UpdateCommand="UPDATE [TBLSTAFF] SET [SSN] = @SSN,
    [FIRSTNAME] = @FIRSTNAME, [LASTNAME] = @LASTNAME, [MIDDLE] = @MIDDLE,
    [RACEID] = @RACEID, [SEX] = @SEX WHERE [STAFFID] = @original_STAFFID"
    InsertCommand="INSERT INTO [TBLSTAFF] ([SSN], [FIRSTNAME],
    [LASTNAME], [MIDDLE], [RACEID], [SEX]) VALUES (@SSN, @FIRSTNAME,
    @LASTNAME, @MIDDLE, @RACEID, @SEX)"
    DeleteCommand="DELETE FROM [TBLSTAFF] WHERE [STAFFID] =
    @original_STAFFID">
    <DeleteParameters>
    <asp:parameter Type="Int32"
    Name="STAFFID"></asp:parameter>
    </DeleteParameters>
    <UpdateParameters>
    <asp:parameter Type="String"
    Name="SSN"></asp:parameter>
    <asp:parameter Type="String"
    Name="FIRSTNAME"></asp:parameter>
    <asp:parameter Type="String"
    Name="LASTNAME"></asp:parameter>
    <asp:parameter Type="String"
    Name="MIDDLE"></asp:parameter>
    <asp:parameter Type="Int32"
    Name="RACEID"></asp:parameter>
    <asp:parameter Type="Int32" Name="SEX"></asp:parameter>
    <asp:parameter Type="Int32"
    Name="STAFFID"></asp:parameter>
    </UpdateParameters>
    <SelectParameters>
    <asp:SessionParameter Name="STAFFID" Type="Int32"
    SessionField="STAFFID"></asp:SessionParameter>
    </SelectParameters>
    <InsertParameters>
    <asp:parameter Type="String"
    Name="SSN"></asp:parameter>
    <asp:parameter Type="String"
    Name="FIRSTNAME"></asp:parameter>
    <asp:parameter Type="String"
    Name="LASTNAME"></asp:parameter>
    <asp:parameter Type="String"
    Name="MIDDLE"></asp:parameter>
    <asp:parameter Type="Int32"
    Name="RACEID"></asp:parameter>
    <asp:parameter Type="Int32" Name="SEX"></asp:parameter>
    </InsertParameters>
    </asp:SqlDataSource>
     
    Glenn L., Mar 30, 2005
    #3
  4. Glenn L.

    Glenn L. Guest

    After futher review I believe I understand. There is still a problem.
    I need the ControlParamater for the Update Command not the Select
    command. When I try to add one for the dropdown box the FormView is the
    only control I can see. If I put in the following code in the Update
    parameters it gives an error saying it cant find the control.

    <asp:ControlParameter Name="RACEID" Type="Int32" ControlID= "DropRace"
    PropertyName="SelectedValue"></asp:ControlParameter>

    I am assume that the FormView is the only control in scope, and because
    the Dropdownlist is in the FormView the ControlParameter Statement
    cannot see it. If this is the case how do I specify the
    ControlParameter. But then again I may be going about this the wrong
    way. Please advise!
     
    Glenn L., Mar 31, 2005
    #4
  5. Glenn L.

    Glenn L. Guest

    After much messing around with the control I found that the following
    code binds it ok and seems to work.


    <asp:DropDownList ID="DropRace" Runat="server" Height="25px"
    Width="203px" DataSourceID="TBLRACE" DataValueField="RACEID"
    DataTextField="RACEDESC"

    SelectedValue = '<%# Bind("RACEID") %>'

    >

    </asp:DropDownList>
     
    Glenn L., Mar 31, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mr2_93
    Replies:
    1
    Views:
    3,644
    mr2_93
    Oct 2, 2005
  2. tshad
    Replies:
    8
    Views:
    2,252
    tshad
    Oct 19, 2005
  3. =?Utf-8?B?SnVhbmpv?=

    GridView: Filtr DropDownList from another DropDownList

    =?Utf-8?B?SnVhbmpv?=, Dec 23, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    1,888
    =?Utf-8?B?SnVhbmpv?=
    Dec 23, 2005
  4. =?Utf-8?B?SnVhbmpv?=

    GridView: Filter DropDownList for another DropDownList

    =?Utf-8?B?SnVhbmpv?=, Dec 29, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    1,323
    =?Utf-8?B?SnVhbmpv?=
    Dec 29, 2005
  5. keithb
    Replies:
    1
    Views:
    12,124
    shauns1
    Nov 1, 2006
Loading...

Share This Page