Solved

update query destroyes table

Posted on 2013-12-31
6
168 Views
Last Modified: 2014-01-03
the following update query wipes out all data between the two dates instead of updating the rows with new data from the datagridview  
      i = 0
      For Each row As DataGridViewRow In DataGridView1.Rows
            updatestatement = "update HOURSWORKED set WORKDATE ='" & DataGridView1.Item(0, i).Value() & "', STARTTIME =' " & DataGridView1.Item(1, i).Value() & "' ,  ENDTIME ='" & DataGridView1.Item(2, i).Value() & "' ,STARTLUNCH ='" & DataGridView1.Item(3, i).Value() & "', ENDLUNCH ='" & DataGridView1.Item(4, i).Value() & "' ,PTOPAID =' " & DataGridView1.Item(5, i).Value() & "',PTOUNPAID ='" & DataGridView1.Item(6, i).Value() & "',PTODETAIL ='" & DataGridView1.Item(7, i).Value() & "' , SYMITARCODE = '" & DataGridView1.Item(8, i).Value() & "' where WORKDATE >=  '" & DateString & " '  or WORKDATE <=  '" & datestring2 & " '  and EMPLOYESOCSEC = '" & TXBSOCSEC.Text & "' "
            Dim Command As New OleDbCommand(updatestatement, Con)
            Command.ExecuteNonQuery()
            Command.Parameters.Clear()
            i = i + 1
        Next

something I do not see going on in the update query??????

thanks

gary
0
Comment
Question by:javagair
  • 3
  • 2
6 Comments
 

Author Comment

by:javagair
ID: 39748474
the reason I am looping through the data is that the datagridview only shows half of the columns in the table and I only want to update the ones that are shown

gary
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39748489
where WORKDATE >=  '" & DateString & " '  or WORKDATE <=  '" & datestring2 & " '  and EMPLOYESOCSEC = '" & TXBSOCSEC.Text & "' "

the OR without () around it seems to be the culprit.
either it needs to be AND (instead of OR), or you need to put () around:

where ( WORKDATE >=  '" & DateString & " '  or WORKDATE <=  '" & datestring2 & " '  ) and EMPLOYESOCSEC = '" & TXBSOCSEC.Text & "' "
0
 
LVL 3

Expert Comment

by:13Shadow
ID: 39748587
Reference the values of the row. You are looping through each row so reference the row directly:
Instead of DataGridView1.Item(0, i).Value() use:  row.Item(0, i).Value() so you make sure you are accessing the current row you are processing.


And if your where clause is supposed to be a date range you need to use AND instead of OR:
 For Each row As DataGridViewRow In DataGridView1.Rows
            updatestatement = "update HOURSWORKED set WORKDATE ='" & row.Item(0, i).Value() & "', STARTTIME =' " & row.Item(1, i).Value() & "' ,  ENDTIME ='" & row.Item(2, i).Value() & "' ,STARTLUNCH ='" & row.Item(3, i).Value() & "', ENDLUNCH ='" & row.Item(4, i).Value() & "' ,PTOPAID =' " & row.Item(5, i).Value() & "',PTOUNPAID ='" & row.Item(6, i).Value() & "',PTODETAIL ='" & row.Item(7, i).Value() & "' , SYMITARCODE = '" & row.Item(8, i).Value() & "' where WORKDATE >=  '" & DateString & " '  AND  WORKDATE <=  '" & datestring2 & " '  and EMPLOYESOCSEC = '" & TXBSOCSEC.Text & "' "
            Dim Command As New OleDbCommand(updatestatement, Con)
            Command.ExecuteNonQuery()
            Command.Parameters.Clear()
          Next

For example:
where WORKDATE >=  '01/01/2013'  AND  WORKDATE <=  '01/31/2013'  will give you any records with a workdate for the month of January
where WORKDATE >=  '01/01/2013'  OR WORKDATE <=  '01/31/2013' will give you any records with a workdate greater than or equal to 01/01/2013 which would be any records for the entire year.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:javagair
ID: 39748659
think I was smoking something when I made the query!!!!
update one row at a time but telling it to look at 31 days.  When I changed the where to look at one row at a time it updates just fine.
0
 

Author Comment

by:javagair
ID: 39748761
135shadow

when I just put the row.item(0,i).value()
imeadiatly get a message that item is not a member of datagridview

gary
0
 
LVL 3

Accepted Solution

by:
13Shadow earned 500 total points
ID: 39752616
Yes we got rid of the counter (i) It should be row.cells(0).value()
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now