Solved

update query destroyes table

Posted on 2013-12-31
6
171 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

929 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

11 Experts available now in Live!

Get 1:1 Help Now