dcsimg
Results 1 to 11 of 11

Thread: [RESOLVED] Validate DGV rows contains "0" value before inserting into Access DB

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2011
    Posts
    76

    Resolved [RESOLVED] Validate DGV rows contains "0" value before inserting into Access DB

    Dear All,

    I am doing an application with DGV to get the user input.
    There are five column and n number of rows which user can add, now i want to validate if the user enters 0 (zero quantity) in a particular column in
    n number of rows before inserting into access db

    Here my code but its not checking entire number of rows even i looped DGV, if 1st row is zero and 2,3,4... rows is 1 or > than zero then its validating and if 1st row is 1 or > than zero and 2,3,4... rows is zero then its going to else part.

    Code:
    For Each item As DataGridViewRow In Me.DataGridView2.Rows
    If item.Cells(3).Value = 0 Then
    MsgBox("Zero Qty is not Allowed")
    Exit For
    Else
    ' Insert into table Qry
    End If
    Next

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,242

    Re: Validate DGV rows contains "0" value before inserting into Access DB

    The first time that you encounter a zero, you exit the loop (Exit For) so of course it doesn't check all the rows.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2011
    Posts
    76

    Re: Validate DGV rows contains "0" value before inserting into Access DB

    Quote Originally Posted by dunfiddlin View Post
    The first time that you encounter a zero, you exit the loop (Exit For) so of course it doesn't check all the rows.
    Thanks for your reply...

    Even if i remove the line Exit For its validating only the 1st row.

    Code:
    For Each item As DataGridViewRow In Me.DataGridView2.Rows
    If item.Cells(3).Value = 0 Then
    MsgBox("Zero Qty is not Allowed")
    
    Else
    ' Insert into table Qry
    End If
    Next

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,507

    Re: Validate DGV rows contains "0" value before inserting into Access DB

    did you set a breakpoint and step through the code one line at a time? Make sure that it actually is looping.

    Also... clear out the bin folder... if you change code, and still experience the samething, it's because it's still running the old code... I'd start there... clear out the bin folder, re-run the app, if it's still not working... breakpoints.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    4,239

    Re: Validate DGV rows contains "0" value before inserting into Access DB

    You may want to validate the data as it is entered. Instead of after all the data is entered. The DGV control has a Validating event.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Oct 2011
    Posts
    76

    Re: Validate DGV rows contains "0" value before inserting into Access DB

    Hi tg...

    I have cleared all the bin files and check with the break points, if i enters value in two rows out of that 1st rows has value and the 2nd row has zero and i run through the break point IF condition becomes false at 1st row because 1st row contains value and directly goes to the Else part (loop is not checking the next rows) and finally my rows are getting inserted in DB...
    Please advice...

    Quote Originally Posted by techgnome View Post
    did you set a breakpoint and step through the code one line at a time? Make sure that it actually is looping.

    Also... clear out the bin folder... if you change code, and still experience the samething, it's because it's still running the old code... I'd start there... clear out the bin folder, re-run the app, if it's still not working... breakpoints.

    -tg

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,507

    Re: Validate DGV rows contains "0" value before inserting into Access DB

    Ok... hold on... let me break this down so I get it...

    [quote] if i enters value in two rows out of that 1st rows has value and the 2nd row has zero[/qote]
    OK... so it looks like this?
    Code:
    Row 1 --- Value = 1
    Row 2 --- Value = 0
    Right...

    and i run through the break point IF condition becomes false at 1st row because 1st row contains value and directly goes to the Else part
    Yes... correct... so row 1 has a value... so it's doing exactly what you told it to ... if the value is 0, display message, if the value IS NOT 0 then insert it...

    my rows are getting inserted in DB...
    Again... right... because the value is NOT 0, it will get inserted...
    And as you step through, after it adds row1, it should go back to the top of the loop, and grab row2...

    And at that point, row 2 has a 0, so there you SHOULD get the message box, but by that point, Row 1 has ALREADY been added to the database...
    So it IS doing exactly what you told it to do...

    Is that not what you want?


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Oct 2011
    Posts
    76

    Re: Validate DGV rows contains "0" value before inserting into Access DB

    yes exactly you understand my problem...
    loop should check all the rows before inserting into DB and i unable to find where my logic goes wrong...

    [QUOTE=techgnome;4482763]Ok... hold on... let me break this down so I get it...

    if i enters value in two rows out of that 1st rows has value and the 2nd row has zero[/qote]
    OK... so it looks like this?
    Code:
    Row 1 --- Value = 1
    Row 2 --- Value = 0
    Right...


    Yes... correct... so row 1 has a value... so it's doing exactly what you told it to ... if the value is 0, display message, if the value IS NOT 0 then insert it...


    Again... right... because the value is NOT 0, it will get inserted...
    And as you step through, after it adds row1, it should go back to the top of the loop, and grab row2...

    And at that point, row 2 has a 0, so there you SHOULD get the message box, but by that point, Row 1 has ALREADY been added to the database...
    So it IS doing exactly what you told it to do...

    Is that not what you want?


    -tg

  9. #9
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,507

    Re: Validate DGV rows contains "0" value before inserting into Access DB

    you're logic is wrong by inserting on the INSIDE of the loop...
    Code:
    For Each item As DataGridViewRow In Me.DataGridView2.Rows
    If item.Cells(3).Value = 0 Then
    MsgBox("Zero Qty is not Allowed")
    
    Else
    ' Insert into table Qry
    End If
    Next
    Clearly (now)... that's not what you need or want...

    Code:
    dim someFlag as Boolean = True
    For Each item As DataGridViewRow In Me.DataGridView2.Rows
      If item.Cells(3).Value = 0 Then
        MsgBox("Zero Qty is not Allowed")
        someFlag = False
        exit for
      End If
    Next
    
    if someFlag then
      'NOW do your insert...
    end if

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Oct 2011
    Posts
    76

    Re: Validate DGV rows contains "0" value before inserting into Access DB

    Bunch of Thanks tg...
    As per your code logic its works perfect, now i want to learn more on flag concept, and many more thanks for teaching me

    Quote Originally Posted by techgnome View Post
    you're logic is wrong by inserting on the INSIDE of the loop...
    Code:
    For Each item As DataGridViewRow In Me.DataGridView2.Rows
    If item.Cells(3).Value = 0 Then
    MsgBox("Zero Qty is not Allowed")
    
    Else
    ' Insert into table Qry
    End If
    Next
    Clearly (now)... that's not what you need or want...

    Code:
    dim someFlag as Boolean = True
    For Each item As DataGridViewRow In Me.DataGridView2.Rows
      If item.Cells(3).Value = 0 Then
        MsgBox("Zero Qty is not Allowed")
        someFlag = False
        exit for
      End If
    Next
    
    if someFlag then
      'NOW do your insert...
    end if

    -tg

  11. #11
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,507

    Re: Validate DGV rows contains "0" value before inserting into Access DB

    the flag is jsut a boolean... indicates a state, yes or no, true or false, on or off... it's nothing special... I just gave it some generic name.... this works equaly well...


    Code:
    dim doInsert as Boolean = True
    For Each item As DataGridViewRow In Me.DataGridView2.Rows
      If item.Cells(3).Value = 0 Then
        MsgBox("Zero Qty is not Allowed")
        doInsert = False
        exit for
      End If
    Next
    
    if doInsert then
      'NOW do your insert...
    end if

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width