Results 1 to 5 of 5

Thread: do While problem !

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2007
    Posts
    192

    do While problem !

    Code:
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            On Error Resume Next
            Dim sql As String
            ListBox1.Items.Clear()
            ListBox2.Items.Clear()
    
    
            Select Case SifraDokum
                Case 1, 2, 3
                    sql = "kupac = True"
                Case 4
                    sql = "dobav = True"
    
                Case Else
                    MsgBox("Morate odrediti vrstu dokumenta!", MsgBoxStyle.OkOnly)
                    Exit Sub
            End Select
    
            If TextBox1.Text <> "" Then
                Select Case Len(TextBox1.Text)
                    Case 1
                        sql = "SELECT sifra, naziv_dob FROM kupac " _
                           & " WHERE " & sql & " AND naziv_dob Like '" & TextBox1.Text & "%'" _
                           & " ORDER BY naziv_dob"
                    Case 2, 3, 4, 5, 6, 7, 8
                        sql = "SELECT sifra, naziv_dob FROM kupac " _
                             & " WHERE " & sql & " AND naziv_dob Like '%" & TextBox1.Text & "%'" _
                            & " ORDER BY naziv_dob"
                End Select
                'rsProdaja.open(sql, cnProdaja, adOpenForwardOnly, adLockReadOnly)
    
              
                'cnProdaja = New Data.SqlServerCe.SqlCeConnection("Data Source =\Program Files\POCKET_PC\pocket.sdf;")
                Dim naredba As New System.Data.SqlServerCe.SqlCeCommand(sql, cnProdaja)
                'cnProdaja.Open()
                Dim dr4 As System.Data.SqlServerCe.SqlCeDataReader = naredba.ExecuteReader
                'Dim dr4 As System.Data.SqlServerCe.SqlCeDataReader
                If dr4.Read <> 0 Then
                    'Do While dr4.Read
    
                    Do While dr4.Read
                        ListBox2.Items.Add(dr4("naziv_dob").ToString)
                        ListBox1.Items.Add(dr4("sifra").ToString)
                        'rsProdaja.movenext()
    
                    Loop
                End If
                dr4.Close()
            End If
            TextBox1.Text = ""
        End Sub
    Bolded is problem,when I start program and when i put brakepoint
    Do While dr4.Read
    program just keep running in
    Code:
    Do While dr4.Read
    listBox2.Items.Add(dr4("naziv_dob").ToString)
    ListBox1.Items.Add(dr4("sifra").ToString)
     'rsProdaja.movenext()
    and never to get to End if.
    I database I have only one naziv_dob and one sifra .Where is problem here?

  2. #2
    Member
    Join Date
    Jan 2008
    Posts
    33

    Re: do While problem !

    just a note:

    i think whenever you call dr.Read, it actually reads one record then point to the next record, if any. if i am right, your IF statement has already read the first record, and your do while is now processing the second record, if any.

    i haven't tried your code yet, but i think it would be cleaner if it would look like so (remove the IF-ENDIF)

    Code:
    While dr4.Read
           ListBox2.Items.Add(dr4("naziv_dob").ToString)
           ListBox1.Items.Add(dr4("sifra").ToString)
    End While
    try it and post back if anything....cheers!


    elf

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2007
    Posts
    192

    Re: do While problem !

    It same problem again !

  4. #4
    Member
    Join Date
    Jan 2008
    Posts
    33

    Re: do While problem !

    i see.

    i noticed u have On Error Resume Next, which i think is almost never advisable. remove that, and resolve any error that may be thrown by the app.

    i will try your code when i find the time.


    cheers!

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: do While problem !

    This line:

    If dr4.Read <> 0 Then

    should be:

    If dr4.HasRows Then

    otherwise you will be skipping over the first row. You say that you only have one row, so the do loop actually should never happen, because the If statement should have moved the pointer past that one row. There are a couple of things that might have happened at that point, one of which would be an exception, but with On Error Resume Next, you may have gotten frozen into a perpetual loop, since the Next statement might have sent you right around the loop forever.

    I would guess that the entire problem was ultimately the If statement, and fixing that could very well solve your problem, but that On Error Resume Next will cause you terrible problems whenever you use it. Basically, every statement could throw an exception, and you'd just keep on going. The actual behavior of the program would become nearly random. Structured exception handling (the Try....Catch block) is a vastly superior way to handle errors, partly for that reason. I agree that you should be using exception handling whenever you work with a database, but never On Error Resume Next.
    My usual boring signature: Nothing

Posting Permissions

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



Click Here to Expand Forum to Full Width