-1

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.

Chris Music
  • 109
  • 11

2 Answers2

0

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.

Matt
  • 1,596
  • 2
  • 18
  • 32
  • No code behind. How and where do I add an update command. I've tried a few things and nothing seems to work. – Chris Music Jun 25 '15 at 11:06
  • Thanks for this. I've updated my code with your suggestion. I get an error: Data type mismatch in criteria expression. This field is a boolean (not sure if that makes a difference) but I've tried the other fields in the table and it comes back with the same. Any thoughts? – Chris Music Jun 25 '15 at 14:25
  • On the update, you shouldnt be setting the Id, that should remain static once the record has been created, however, thats incidental. Your update command (as above) seems to be missing a comma between 2 parameters (dateworked=@dateworked confirmed=@confirmed) Which field is it complaining about? Can you copy the update query as is, and run it directly in access? (You'll have to change the @parameters to real values. – Matt Jun 25 '15 at 15:26
  • OK. Been trying to get this work trying lots of different things. I must have put the comma in already, just not copied the new text. sorry.The update query works perfectly in ms access. If I take out the "confirmed" field, then it does not throw up an error, but it also does not update the table. Put it back, I get the same data type mismatch error. I've updated my code with all the current changes. – Chris Music Jun 25 '15 at 18:45
  • Can you add a Type attribute on the update parameters? i.e. Type="Boolean" or Type="Int32" – Matt Jun 26 '15 at 09:49
0

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.

Hannington Mambo
  • 998
  • 2
  • 13
  • 28