简体   繁体   中英

How do I update an an Access database from a GridView in Visual Studio?

I'm trying to update an access database when using a Grid View in Visual Studio 2010 but not having any success. Let me try and explain what I have.

I have an access database with a table "tblConfirmedworkhours" which has fields "dateworked" & "confirmed". I can display the filtered table on my webpage with the edit/update links but the table won't update.

Option 1: I would like (if possible) is to not have to click on the edit & update buttons, just edit the data on the screen (I'm thinking of some kind of similar thing to a continuous form is MS Access and then hit some kind of save button (which (again if possible) would run an append query already created and stored in my access database "qryToHistory".

Option 2: Be able to use the edit/update buttons to change the data in the table.

Here is my current code:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
DataSourceID="AccessDataSource1"  Width="983px" 
    AutoGenerateDeleteButton="True" AutoGenerateEditButton="True">
<Columns>
   <asp:BoundField DataField="WorkHourID" Visible="false" 
        HeaderText="Timesheet ID" />
    <asp:BoundField DataField="EmpName" HeaderText="Employee" 
        SortExpression="EmpName" />
    <asp:BoundField DataField="dateworked" 
        HeaderText="Date" SortExpression="dateworked" ApplyFormatInEditMode="True">
    <ItemStyle HorizontalAlign="Center" />
    </asp:BoundField>
    <asp:CheckBoxField DataField="confirmed" HeaderText="Confirmed" 
        SortExpression="confirmed" Text="This is OK">
    <ItemStyle HorizontalAlign="Center" />
    </asp:CheckBoxField>
</Columns>
<EditRowStyle Width="500px" Wrap="True" />
<EmptyDataRowStyle Width="5000px" />
</asp:GridView>
<asp:AccessDataSource ID="AccessDataSource1" runat="server" 
DataFile="~/PayrollDirect.accdb" 
SelectCommand="SELECT [WorkHourID], [EmpName], [dateworked], [confirmed] FROM [tblConfirmedworkhours] WHERE (([CompanyID] = ?) AND ([confirmed] = ?)) ORDER BY [dateworked]"
UpdateCommand="UPDATE tblConfirmedworkhours SET dateworked=dateworked, confirmed=confirmed where WorkHourID=WorkHourID">
<SelectParameters>
    <asp:SessionParameter DefaultValue="0" Name="CompanyID" SessionField="UserID" Type="Int32" />
    <asp:Parameter DefaultValue="false" Name="confirmed" Type="Boolean" />
</SelectParameters>
<UpdateParameters>
    <asp:Parameter Name="dateworked"/>
    <asp:Parameter Name="confirmed"/>
</UpdateParameters>
</asp:AccessDataSource>

Is Option 1 a possibility, if so how.

If option 1 is not possible, how can I sort the problem of updating my table as in option 2.

I'm still learning Visual Studio so any and all help is gratefully appreciated.

I don't see an update command in your datasource. Are you trying to update it via the aspx page, or do you have a code behind page?

Try adding an UpdateCommand in the asp:accessDataSource. I suspect that at present, you are asking the tabel to update, when you haven't told it that a) It is allowed to and b) How it should update

After edit.

this is from the top of my head. But try adding in something like the following in. (Similar to the select)

  UpdateCommand="UPDATE tblConfirmedworkhours  SET dateWorked=@dateWorked, confirmed =@confirmed where EmpName=@empName">
  <UpdateParameters>
    <asp:Parameter Name="dateWorked" />
    <asp:Parameter Name="confirmed" />
  </UpdateParameters>

I don't have VS on this PC so I can't give an exact answer at the moment sorry. However, I think you may need to also have the employee number in your queries, as you may have 2 employees with the same name, and you need a unique reference to distinguish between them.

I had a lot of trouble with this as well and discovered something that is not coming out on these posts: the main reason for me is that I had a lastupdated datetime column, which is required, and is set with a default value in the database. I assumed that the database would handle its update for me, so I had it as a hidden column in the GridView.

After hours of frustration, I started afresh and tested every step of my actions. Then I discovered that hiding a required field is all that causes this problem! You may also notice that the date format might be off, depending on your settings, so you will also need to supply it in the correct format, if required.

By the way, it is not a unique Access thing; it happens even with Ms SQL server, and I guess will happen with other databases as well.

I guess that is also the reason several questions about this report 'sometimes it works, sometimes it doesn't', or 'it works for one table and not another'.

If you don't want the user to change the values of some of your columns, then set them to templates and change their edit/add properties to read-only.

There are also other ways to handle this, eg, supply its value in code behind when it is hidden.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM