dcsimg
Results 1 to 8 of 8

Thread: [RESOLVED] VS2015 is this the way checking exists record(Using a Function) Need a loop?

  1. #1

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    4,718

    Resolved [RESOLVED] VS2015 is this the way checking exists record(Using a Function) Need a loop?

    hey,
    i have some difficulty checking exists record using a function
    would love to get some help
    this is my code
    Code:
      Public Function IfExsitsName(RcordName As String, Tabl As String) As Boolean
    
            Dim Rs As SqlDataReader
    
            Try
    
                Dim str As String = "select ('" & RcordName & "') From " & Tabl
    
                Cn.Open()
                Cmd = New SqlCommand(str, Cn)
                Rs = Cmd.ExecuteReader
                If Rs.HasRows Then
                    IfExsitsName = True
                Else
                    IfExsitsName = False
                End If
    
    
                Rs.Close()
                Cmd.Parameters.Clear()
                Cmd.Dispose()
                Cn.Close()
    
    
            Catch ex As Exception
    
                Cmd.Parameters.Clear()
                Cmd.Dispose()
                Cn.Close()
                MessageBox.Show(ex.Message)
                Exit Function
            End Try
    
    
        End Function
    im sure there is a efficient way of doing this
    tnx for any help
    salsa

  2. #2
    Fanatic Member PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Newport, UK
    Posts
    888

    Re: VS2015 is this the way checking exists record(Using a Function) Need a loop?

    You could do a SELECT COUNT(*) in your SQL, that should be more efficient, https://docs.microsoft.com/en-us/sql...ql-server-2017 should give you the details.

  3. #3
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    3,847

    Re: VS2015 is this the way checking exists record(Using a Function) Need a loop?

    Are you trying to find if a Field names exist? Because that's all your doing and if there's no Field named "RcordName" it will cause and error and that's not good.

    Or are you trying to find out if a specific record exists? If thats the case then you need to use a WHERE clause,

    Code:
    Dim RcordName as String ="Fred"
    
    Din str as String ="Select FirstName from " & Tabl & " WHERE FirstName='" & RcordName & "'"

  4. #4

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    4,718

    Re: VS2015 is this the way checking exists record(Using a Function) Need a loop?

    Or are you trying to find out if a specific record exists?
    exactly
    how did i miss that?
    thank you

  5. #5

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    4,718

    Re: [RESOLVED] VS2015 is this the way checking exists record(Using a Function) Need a

    one question
    why do i need the
    where?
    dont i need to loop the table?
    this is a function that i plan to use for serveral tables

  6. #6
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    3,847

    Re: [RESOLVED] VS2015 is this the way checking exists record(Using a Function) Need a

    Quote Originally Posted by salsa31 View Post
    one question
    why do i need the
    where?
    dont i need to loop the table?
    this is a function that i plan to use for serveral tables
    No you don't need a loop, the WHERE clause does it for you.

    It sounds like you want to use one function to check various tables with different schema's to see if a certain record exists.

    Code:
            Dim con As New SqlConnection(My.Settings.BooksDBConnectionString)
            con.Open()
            Dim fldName = "BookName"
            Dim fldValue = "www"
            Dim tableName = "Books"
            Dim selectStr = "Select Count(*) from " & tableName & " WHERE " & fldName & "='" & fldValue & "'"
            Dim cmd As New SqlCommand(selectStr, con)
            Dim cnt As Integer
            cnt = cmd.ExecuteScalar
            MessageBox.Show(cnt.ToString)
    This should get you started.

  7. #7

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    4,718

    Re: [RESOLVED] VS2015 is this the way checking exists record(Using a Function) Need a

    It sounds like you want to use one function to check various tables with different schema's to see if a certain record exists.
    yes thats correct

  8. #8
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    3,847

    Re: [RESOLVED] VS2015 is this the way checking exists record(Using a Function) Need a

    This "cnt = cmd.ExecuteScalar" needs to be changed to this "cnt = CInt(cmd.ExecuteScalar)" if you have Option Strict On, as you should.

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