locked
Update command not working in gridview RRS feed

  • Question

  • User-80244955 posted
     

     hii

    the problem is i'm trying to use a grid view to show some data and then give the ability to edit that data

    I'm able to delete the data i.e. delete command is working but update is failing with an exception "No value given for one or more required parameters"

    plz observe that i'm trying to edit even the preimarykey also (filepath)

     

    plz help

     

    here's my code

    1    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
    2 AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="filepath"
    3 DataSourceID="AccessDataSource1" >
    4
    5 <Columns>
    6
    7 <asp:TemplateField>
    8 <ItemTemplate>
    9 <asp:Label ID="l2" runat="server" Text='<%# Eval("uploadedby") %>'></asp:Label>
    10 </ItemTemplate>
    11 <EditItemTemplate>
    12 <asp:TextBox ID="txt3" runat="server" Text='<%# Eval("uploadedby") %>'></asp:TextBox>
    13 </EditItemTemplate>
    14 </asp:TemplateField>
    15
    16 <asp:TemplateField >
    17 <ItemTemplate>
    18 <asp:Label ID="l1" runat="server" Text='<%# Eval("filepath") %>'></asp:Label>
    19 </ItemTemplate>
    20 <EditItemTemplate>
    21 <asp:TextBox ID="txt2" runat="server" Text='<%# Eval("filepath") %>'></asp:TextBox>
    22 </EditItemTemplate>
    23 </asp:TemplateField>
    24
    25
    26
    27 <asp:HyperLinkField DataTextField="filepath" DataTextFormatString="Download" DataNavigateUrlFields="filepath" DataNavigateUrlFormatString="~/notes/files/{0}" HeaderText="Download Link" SortExpression="filepath" />
    28
    29 <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
    30 </Columns>
    31
    32 </asp:GridView>
    33
    34 <asp:AccessDataSource ID="AccessDataSource1" runat="server" OldValuesParameterFormatString="old_{0}"
    35 DataFile="~/App_Data/test2.mdb"
    36 SelectCommand="SELECT [filepath], [uploadedby] FROM [files]"
    37 DeleteCommand="DELETE FROM [files] WHERE [filepath] = ?"
    38
    39 UpdateCommand="UPDATE [files] SET [uploadedby] = ? , [filepath]= ? where [filepath]=?" >
    40 <DeleteParameters>
    41 <asp:Parameter Name="old_filepath" Type="String" />
    42 </DeleteParameters>
    43 <UpdateParameters>
    44 <asp:Parameter Name="uploadedby" Type="String" />
    45 <asp:Parameter Name="filepath" Type="String" />
    46 <asp:Parameter Name="old_filepath" Type="String" />
    47 </UpdateParameters>
    48
    49 </asp:AccessDataSource>
     

     

     

    Friday, August 8, 2008 3:25 PM

Answers

  • User-821857111 posted

    Remove:

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

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 30, 2008 4:24 PM
  • User-1199946673 posted

    Do I understand you correctly that filepath is the primary key in the table files, since the property DataKeyNames of the gridview is 'filepath'. I would advice you to add an extra autonumber column fileID to the table, and use that as the primary key.

    Also, in the edit template you're using 'eval' instead of 'bind'

    Try (I already included the extra column fileID):

     

        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="fileID" DataSourceID="AccessDataSource1"  >
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Label ID="l2" runat="server" Text='<%# Eval("uploadedby") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txt3" runat="server" Text='<%# Bind("uploadedby") %>'></asp:TextBox>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField >
                <ItemTemplate>
                    <asp:Label ID="l1" runat="server" Text='<%# Eval("filepath") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txt2" runat="server" Text='<%# Bind("filepath") %>'></asp:TextBox>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:HyperLinkField   DataTextField="filepath" DataTextFormatString="Download" DataNavigateUrlFields="filepath" DataNavigateUrlFormatString="~/notes/files/{0}"  HeaderText="Download Link" SortExpression="filepath" />
            <asp:CommandField   ShowDeleteButton="True" ShowEditButton="True" />
        </Columns>
        </asp:GridView>
        
        <asp:AccessDataSource ID="AccessDataSource1" runat="server" OldValuesParameterFormatString="{0}" DataFile="~App_Data/test2.mdb"
         SelectCommand="SELECT [fileID], [filepath], [uploadedby] FROM [files]"
         DeleteCommand="DELETE FROM [files] WHERE [fileID] = ?"
         UpdateCommand="UPDATE [files] SET [uploadedby] = ? , [filepath]= ? where [fileID]=?" >
            <DeleteParameters>
                <asp:Parameter Name="fileID" Type="int32" />
            </DeleteParameters>
            <UpdateParameters>
                <asp:Parameter Name="uploadedby" Type="String" />
                <asp:Parameter Name="filepath" Type="String" />
                <asp:Parameter Name="fileID" Type="int32" />
            </UpdateParameters>
        </asp:AccessDataSource>    
    
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 30, 2008 5:13 PM

All replies

  • User-1313886874 posted

     Place a button to activate ur AccessDatasource1's Update(), in ur Coding...

    Thursday, October 30, 2008 7:17 AM
  • User-821857111 posted

    Remove:

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

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 30, 2008 4:24 PM
  • User-1199946673 posted

    Do I understand you correctly that filepath is the primary key in the table files, since the property DataKeyNames of the gridview is 'filepath'. I would advice you to add an extra autonumber column fileID to the table, and use that as the primary key.

    Also, in the edit template you're using 'eval' instead of 'bind'

    Try (I already included the extra column fileID):

     

        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="fileID" DataSourceID="AccessDataSource1"  >
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Label ID="l2" runat="server" Text='<%# Eval("uploadedby") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txt3" runat="server" Text='<%# Bind("uploadedby") %>'></asp:TextBox>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField >
                <ItemTemplate>
                    <asp:Label ID="l1" runat="server" Text='<%# Eval("filepath") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txt2" runat="server" Text='<%# Bind("filepath") %>'></asp:TextBox>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:HyperLinkField   DataTextField="filepath" DataTextFormatString="Download" DataNavigateUrlFields="filepath" DataNavigateUrlFormatString="~/notes/files/{0}"  HeaderText="Download Link" SortExpression="filepath" />
            <asp:CommandField   ShowDeleteButton="True" ShowEditButton="True" />
        </Columns>
        </asp:GridView>
        
        <asp:AccessDataSource ID="AccessDataSource1" runat="server" OldValuesParameterFormatString="{0}" DataFile="~App_Data/test2.mdb"
         SelectCommand="SELECT [fileID], [filepath], [uploadedby] FROM [files]"
         DeleteCommand="DELETE FROM [files] WHERE [fileID] = ?"
         UpdateCommand="UPDATE [files] SET [uploadedby] = ? , [filepath]= ? where [fileID]=?" >
            <DeleteParameters>
                <asp:Parameter Name="fileID" Type="int32" />
            </DeleteParameters>
            <UpdateParameters>
                <asp:Parameter Name="uploadedby" Type="String" />
                <asp:Parameter Name="filepath" Type="String" />
                <asp:Parameter Name="fileID" Type="int32" />
            </UpdateParameters>
        </asp:AccessDataSource>    
    
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 30, 2008 5:13 PM
  • User-1199946673 posted

    t_anvesh,

    It would be nice if you let us know whether or not your problem has been resolved, and if so, mark the answer(s) that helped you?

    Thanks...

    Thursday, November 6, 2008 6:27 PM