locked
How to pass data to an Access Data Source to update a table RRS feed

  • Question

  • User1177026165 posted

    Hi

    This should, I hope, be a simple question to answer.

    I have a drop down list, which when a user has made a selection and clicked a button will trigger an update to a table. So, my question is how do you pass a selected value from a drop down list to an Access Data Source to update a table?

    Thanks

    Saturday, January 31, 2009 10:51 AM

Answers

  • User-821857111 posted

    Seems to me that there is no event to fire the UpdateCommand.  Go to design view, then click once on the DropDownList to select it.  Then hit F4 to bring up its properties.  Click the lightning bolt icon to get to its events, then double click the SelectedIndexChanged event.  That will create an event handler in code behind.  Within that, add

    SqlDataSource3.Update()

    See if that works.  You may also need to add GridView2.DataBind() to refresh the GridView to see any changes.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 3, 2009 2:31 PM

All replies

  • User-821857111 posted

    You set the DropDownList as the source of the value of a ControlParameter

     

    Saturday, January 31, 2009 1:14 PM
  • User1177026165 posted

    Hi

    I have already to do something already. To give you a better idea what I am doing, my drop down list is already bound to an Access Data Source, and this works. The problem I am having is taking the selected item and passing it to a second Access Data Source run an update query on the table. I can grab the selected item - no problem at all, but when I use my second Access Data Source to run my Update Query, I can't get the Access Data Source to execute the Update Query. Therefore, I think I must be missing a step some where along the line.

    Can you advise me?

    Thanks

    Saturday, January 31, 2009 4:41 PM
  • User-821857111 posted

    If you show your existing code, someone might be able to identify the problem.  Otherwise it's pure guesswork.

     

    Saturday, January 31, 2009 4:59 PM
  • User1177026165 posted

    Hi

    I have been trying to crack this myself, but made no head way! Below is my snippet of code:

    <asp:SqlDataSource ID="SqlDataSource1" runat="server"

    ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"

    ProviderName="<%$ ConnectionStrings:NorthwindConnectionString.ProviderName %>"

    SelectCommand="SELECT [EmployeeID], [LastName] FROM [Employees]">

    </asp:SqlDataSource>

     

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

    DataSourceID="SqlDataSource1" DataTextField="LastName"

    DataValueField="EmployeeID" AutoPostBack="true">

    </asp:DropDownList>

     

    <asp:SqlDataSource ID="SqlDataSource3" runat="server"

    ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"

    ProviderName="<%$ ConnectionStrings:NorthwindConnectionString.ProviderName %>"

    UpdateCommand="UPDATE [Employees] SET [NotActive]=@NotActive WHERE [EmployeeID]=@EmployeeID">

    <UpdateParameters>

    <asp:Parameter Name="NotActive" DefaultValue="YES" Type="String" />

    <asp:ControlParameter ControlID="DropDownList1" Name="EmployeeID" PropertyName="SelectedValue" />

    </UpdateParameters>

    </asp:SqlDataSource>

    <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"

    DataSourceID="SqlDataSource3" Visible="true">

    <Columns>

    <asp:BoundField DataField="LastName" HeaderText="LastName"

    SortExpression="LastName" />

    <asp:BoundField DataField="Active" HeaderText="Active"

    SortExpression="Active" />

    <asp:BoundField DataField="NotActive" HeaderText="NotActive"

    SortExpression="NotActive" />

    </Columns>

    </asp:GridView>

    I have tried to use a parameter tag to hold one value to for the set part of the Update Query, which I hope will be passed when the value from the drop down list is passed to the where clause. Unfortunately, when I check the table it hasn't updated.

    Can someone tell me what I am doing wrong, please?

    Monday, February 2, 2009 2:00 PM
  • User-821857111 posted

    Try changing the UpdateCommand to

    UPDATE [Employees] SET [NotActive]=1 WHERE [EmployeeID]=@EmployeeID

    then remove the parameter for NotActive


    Monday, February 2, 2009 3:41 PM
  • User1177026165 posted

    Hi

    Tried this already, and that didn't work either. Any ideas?

    Tuesday, February 3, 2009 2:23 PM
  • User-821857111 posted

    Seems to me that there is no event to fire the UpdateCommand.  Go to design view, then click once on the DropDownList to select it.  Then hit F4 to bring up its properties.  Click the lightning bolt icon to get to its events, then double click the SelectedIndexChanged event.  That will create an event handler in code behind.  Within that, add

    SqlDataSource3.Update()

    See if that works.  You may also need to add GridView2.DataBind() to refresh the GridView to see any changes.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 3, 2009 2:31 PM