none
What is the issue with this function? RRS feed

  • Question

  • This function always returns true, no matter what data is used as the username, can someone tell me why please?
    	Public Function useduname(ByVal username As String) As Boolean
    		Try
    		rowcount = DataSet.test.Rows.Count - 1
    For i = 0 To rowcount
    	If username = DataSet.test.Rows(i).Item(1) Then
    		Return True
    		Exit Try
    	Else
    	End If
    Next
    Return False
    Finally
    End Try
    End Function


    Saturday, April 13, 2019 4:58 PM

All replies

  • Hi,
    please, set OPTION STRICT ON!

    After "Return True" the "Exit Try" is not necessary.


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Saturday, April 13, 2019 5:11 PM
  • Thanks for the quick reply!

    Using Option Strict On, I get 43 errors, mostly for conversions from object to string.

    Saturday, April 13, 2019 5:17 PM
  • Hi,
    43 errors: max. 15 minutes to correct. After correction your program will run more stable.

    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Saturday, April 13, 2019 5:21 PM
  • Hi,

    I corrected all of the errors and tested the function again, it still seems to be returning true everytime.

    Saturday, April 13, 2019 5:38 PM
  • Hi,
    I don't know your data. In the following demo the result is false:

    Module Module1
    
      Sub Main()
    
        Console.WriteLine(useduname("abc"))
    
        Console.ReadKey()
      End Sub
    
      Private DataSet As New Data
    
      Public Function useduname(ByVal username As String) As Boolean
        Try
          Dim rowcount = Dataset.test.Rows.Count - 1
          For i = 0 To rowcount
            If username = DataSet.test.Rows(i).Item(1).ToString Then Return True
          Next
          Return False
        Finally
        End Try
      End Function
    
      Public Class Data
        Inherits System.Data.DataSet
    
        Public Property test As New DataTable
    
      End Class
    
    End Module


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks


    Saturday, April 13, 2019 5:49 PM
  • Thanks for the reply again.

    It's incredibly frustrating and I really can't comprehend why it's returning true for me. I tested the same function but for a different column and it's still just returning true everytime. Do you know of any other ways I can check if an item exists in a dataset? I have previously performed these checks on other forms and they work fine.

    Saturday, April 13, 2019 6:15 PM
  • Hi,
    your code is without catch. Please insert catch! Like this demo.

    Module Module1
    
      Sub Main()
        Console.WriteLine(useduname("abc"))
    
        Console.ReadKey()
      End Sub
    
      Private DataSet As New Data
    
      Public Function useduname(ByVal username As String) As Boolean
        Try
          Dim rowcount = Dataset.test.Rows.Count - 1
          For i = 0 To rowcount
            If username = DataSet.test.Rows(i).Item(1).ToString Then Return True
          Next
          Return False
        Catch ex As Exception
          Console.WriteLine(ex.ToString)
        End Try
      End Function
    
      Public Class Data
        Inherits System.Data.DataSet
    
        Public Property test As New DataTable
    
      End Class
    
    End Module


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Saturday, April 13, 2019 6:23 PM
  • Here is a conceptual example for locating a value in a specific field in a DataTable within a DataSet. Of course your DataTable may have many fields, that doesn't matter as we are focused on one field.

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim ds As DataSet = CreateMockedDataSet()
        Dim nameToFind As String = "Karen"
        Dim foundRow As DataRow = ds.Tables("Names").
                AsEnumerable().
                FirstOrDefault(Function(row) row.Field(Of String)("FirstName") = nameToFind)
    
        If foundRow IsNot Nothing Then
            MessageBox.Show($"{nameToFind} was found with id: {foundRow.Field(Of Integer)("id")}")
        Else
            MessageBox.Show($"{nameToFind} not found")
        End If
    End Sub
    Private Function CreateMockedDataSet() As DataSet
        Dim ds As New DataSet
    
        Dim dt1 As New DataTable With {.TableName = "Names"}
        dt1.Columns.Add(New DataColumn() With {.ColumnName = "Id",
                           .DataType = GetType(Integer),
                           .AutoIncrement = True,
                           .AutoIncrementSeed = 1})
        dt1.Columns.Add(New DataColumn() With {.ColumnName = "FirstName",
                           .DataType = GetType(String)})
    
        dt1.Rows.Add(New Object() {Nothing, "Jim"})
        dt1.Rows.Add(New Object() {Nothing, "Anne"})
        dt1.Rows.Add(New Object() {Nothing, "Karen"})
        dt1.Rows.Add(New Object() {Nothing, "Hank"})
        ds.Tables.Add(dt1)
        Return ds
    End Function


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Saturday, April 13, 2019 6:41 PM
    Moderator
  • Maybe you do not check correctly the result or you call a different function. Use the Debugger to verify this.

    Saturday, April 13, 2019 6:59 PM
  • Hi PG,

    I think the problem is that you don't give us a real insight in your code. You can use the code part for it. (Don't tell me that the icon for it is a fake, the regulars know that I've endless time told that. 

    However, if I do that with your code it would be:

    Public Function useduname(ByVal username As String) As Boolean
    	Try
    		rowcount = DataSet.test.Rows.Count - 1
                    For i = 0 To rowcount
    	              If username = DataSet.test.Rows(i).Item(1) Then return True
    		 exit Try
                          Else
    	          End If
                    Next
               Return False
                    Finally
               End Try
    End Function

    Sorry, I've not that knowledge of others in this forums, but it looks strange to me. 


    Success
    Cor

    Saturday, April 13, 2019 7:16 PM