none
Difficulties when i check existing record in database RRS feed

  • Question

  • Hi members

    i want to check if the record is ixist or not and i tried a lot without a solution

    please i want a help oncing for all because i use this a lot in my apps

     For b = 0 To DataGridView4.Rows.Count - 1
                If cn.State = ConnectionState.Open Then
                    cn.Close()
                End If
                cn.Open()
                Dim cs55 As New SqlCommand("select count(do_tiers) from F_DOCENTETE where do_type='12' and DO_Piece='" & DataGridView4.Rows(b).Cells(0).Value & "'", cn)
    
    
                Dim az = cs55.ExecuteScalar
    
    
                If az = 0 Then
    
                Else
                    Dim cs555 As New SqlCommand("select do_tiers,do_piece from F_DOCENTETE where do_type='12' and DO_Piece='" & DataGridView4.Rows(b).Cells(0).Value.ToString & "' and DO_Tiers='" & TextBox1.Text & "'", cn)
    
    
                    Dim azz As SqlDataReader = cs555.ExecuteReader
                    azz.Read()
                    If azz.HasRows Then
                        MessageBox.Show("bien" & DataGridView4.Rows(b).Cells(0).Value)
    
                    ElseIf Not azz.HasRows Then
    
    
                        MessageBox.Show("fournisseur n'est pas equivalent au Bon Commande !!!" & DataGridView4.Rows(b).Cells(0).Value)
    
    
                    End If
    
                End If
    
    
    
            Next

    if azz.hasRow work perfectly but in else statement

    i maked a messageBox to see what hapen("probably it stepped the row where not equal")

    even i maked elseif not azz.hasRow 

    please help

    Friday, November 29, 2019 8:12 AM

All replies

  • Your code is full off "I don't trust what I do myself". Why not create code which you can read yourself and find the errors. 

    What ElseIf, if it has no rows, it falls direct in the Else branch. 

    Your code is in an anologly

    If the sun is up then switch the light off 
    else if the sun is down then switch it on. 

    Completely senseless. If it is up, it not down. 

    The same is with

    •  testing if the connection is open. Do it right, open it at the begin where you use it and close it at the end.
    •  do an If and then create an NOP and use the Else branch and make it more complicated.
    •  Test if it has rows, (with other where requirements) and than process it. If the reader has no rows that is enough.

    A computer is not a human, it does what you say and nothing else if you tell it in the right way.


    Success
    Cor



    Friday, November 29, 2019 5:23 PM
  • thank you for your answer

    and always i face this and i work hard to test if any idea can pass through and resolved

    so i think if there a way that will work at any time

    i will go through ( Test if it has rows, (with other where requirements) and than process it. If the reader has no rows that is enough.)

    Saturday, November 30, 2019 9:51 AM
  • thank you for your answer

    and always i face this and i work hard to test if any idea can pass through and resolved

    so i think if there a way that will work at any time

    i will go through ( Test if it has rows, (with other where requirements) and than process it. If the reader has no rows that is enough.)

    You did not understand what I wrote. Maybe you thought, that is not true, I even test it more. 

    However, if you test if something is false, then it won't follow the path in a next if as that contains an else if which state the same. 

    You're not the first one doing this. But by make your code more simple, you see that mistake yourself. I made a code sample like yours in more human readable text.

    If theAnimal = Mamal
    Else
      If theAnimal = fish
         messagebox.show("fish")
      elseif theAnimal = dog
         messagebox.show("dog")
      end if
    end if

    50 years ago they told me this is stupid code, while there is no need at all to use elseif. Like you I thought it cannot be. But afterwards I learned they where right. The elseif is never reached, it is stepped over with the first Else sentence. 

    But there are always persons who try it again.


    Success
    Cor



    • Edited by Cor Ligthert Saturday, November 30, 2019 5:33 PM
    Saturday, November 30, 2019 11:46 AM
  • May be this code help you 

    Sub Availability()
            Using conn As New SqlConnection(cs)
                conn.Open()
                Using cmd As New SqlCommand()
                    cmd.Connection = conn
                    cmd.CommandText = "select ColName
                                       from Table1 
                                       where ColName=@a"
    cmd.Parameters.Add("@a", SqlDbType.Int).Value = Val(txtID.Text)
                    Using rdr As SqlDataReader = cmd.ExecuteReader()
                        If rdr.Read Then
                            MessageBox.Show("Record Is Exist", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                           'Do any thing here
    
                            If Not rdr Is Nothing Then
                                rdr.Close()
                            End If
    
                        Else
                            
                            MessageBox.Show("Record IS NOT Exist", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                            'Do any thing here
    
                        End If
    
                    End Using
                End Using
            End Using
    
        End Sub
     



    Regards From Egypt

    Sunday, December 1, 2019 7:23 AM
  • thank you a lot
    Saturday, December 7, 2019 10:28 AM
  • Hi Houssem12,

    Did you solve your problem? If your question has been answered then please click the "Mark as Answer" Link at the bottom of the post(s) to help others searching the forum in the future.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, December 9, 2019 1:29 AM
    Moderator
  • Hi Houssem12,

    Is your problem solved? If so, please click "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    '

    Tom's English Writing Tips

    Write in the correct tense (past, current, future). Use the same tense for the same action in the same sentence. Use active not passive voice (verb comes before subject).

    For example, instead of: "Is your problem solved?" use:

    Did you solve your problem?

    Avoid words like it and so for nouns. Use the word. Instead of writing "it" say "the dog." Instead of "so" use "answered".

    Avoid Dead Wood (words you don't need) ... the answer will be the appropriate one or it would not be in the process of being marked. Therefore the words are not needed.

    "If so, please click "Mark as answer" to the appropriate answer, "

    If your question has been answered then please click the Mark as Answer Link at the bottom of the post.


    "so that it will help other members to find the solution quickly if they face a similar issue."

    Cut the dead wood.

    "... to help others in the future.

    Final answer:

    If your question has been answered then please click the "Mark as Answer" Link at the bottom of the post(s) to help others searching the forum in the future.

    :)


    PS feel free to correct me.

    Tuesday, December 10, 2019 12:25 PM
  • Hi tommytwotrain,

    Thank you for suggestions.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, December 11, 2019 1:18 AM
    Moderator