ryanfc2
ryanfc2

Reputation: 11

Update command issue

I'm having a problem with an update function I'm trying to create for a group website I'm building. I have code in place that uses a SQL datasource and a list view with edit functionality. Basically I'm having a problem updating the table. I only want to update certain fields within the table in the DB but I only know how to update them all using the ID as a datakey name. Can someone help me/direct me as to what I need to add for the code to just update the fields I want? Basically I just want to be able to edit the name, description and units fields.

 <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:UniString %>"



        SelectCommand="SELECT [module_name], [module_desc], [module_units] 
        FROM [modules] ORDER BY [module_name], [module_desc], [module_units]"

      UpdateCommand="UPDATE [modules] SET [module_name]= @Module_Name, [module_desc]=@Module_Description, 
        [module_units]=@Module_Units"


        >

    <UpdateParameters>
        <asp:Parameter Name="Module_Name" Type="String" />
        <asp:Parameter Name="Module_Description" Type="String" />
        <asp:Parameter Name="Module_Units" Type="Int32" />
    </UpdateParameters>


    </asp:SqlDataSource>

</p>
<asp:ListView ID="ListView1" runat="server" 
    DataSourceID="SqlDataSource1"
    **DataKeyNames= ""**   >


    <AlternatingItemTemplate>
        <span style="">module_name:
        <asp:Label ID="module_nameLabel" runat="server" Text='<%# Eval("module_name") %>' />
        <br />
        module_desc:
        <asp:Label ID="module_descLabel" runat="server" Text='<%# Eval("module_desc") %>' />
        <br />
        module_units:
        <asp:Label ID="module_unitsLabel" runat="server" Text='<%# Eval("module_units") %>' />
        <br />
        <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
<br />
            <br />
            </span>
        </AlternatingItemTemplate>
        <EditItemTemplate>
            <span style="">module_name:
            <asp:TextBox ID="module_nameTextBox" runat="server" Text='<%# Bind("module_name") %>' />
            <br />
            module_desc:
            <asp:TextBox ID="module_descTextBox" runat="server" Text='<%# Bind("module_desc") %>' 
                                        TextMode="MultiLine" Columns="30" Rows="10" />
            <br />
            module_units:
            <asp:TextBox ID="module_unitsTextBox" runat="server" Text='<%# Bind("module_units") %>' />
            <br />
            <asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
            <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
            <br /><br /></span>
        </EditItemTemplate>

I have no back-end code on the CS file as I don't think its needed also I haven't added the whole list view as its long and probably isn't needed to be displayed. I have put ** beside the key names as I think that's where the error is coming from. When I had something such as module name in there, I think it works, but it updates everything to the same name, desciption etc. I want to individually change each of the list edits to be a different module name, description etc.

Upvotes: 1

Views: 80

Answers (1)

Mikhail Vitik
Mikhail Vitik

Reputation: 505

Briefly you should

  1. Change select and select ID column from DB SelectCommand="SELECT [module_id]
  2. Change update "UPDATE [modules] SET [module_name]= @Module_Name, [module_desc]=@Module_Description, [module_units]=@Module_Units where [module_id]=@Module_ID"
  3. Set the datakey column to "module_id" us you mention.
  4. Add a hidden colummn into the listview

Upvotes: 1

Related Questions