Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

update query destroyes table

Posted on 2013-12-31
6
Medium Priority
?
181 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 143

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 2000 total points
ID: 39752616
Yes we got rid of the counter (i) It should be row.cells(0).value()
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses
Course of the Month13 days, 6 hours left to enroll

963 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