Check if record value exists

erwin

New member
Joined
Feb 17, 2013
Messages
4
Programming Experience
Beginner
hi guys.
i hav some problem about vb.net
how to check 2 record already exist in database
i use database oracle 10g and vb.net2010

this my coding to check 1 record already exist but i need check 2 in same time
sry my english bad
Private Function IsIdExist() Dim Str, Str1 As String
Dim i As Integer
Str = txtnik.Text
i = 0
While i <> Dst.Tables("hrd.employee").rows.count
Str1 = Dst.Tables("hrd.employee").Rows(i)("nik")
If Str = Str1 Then
Return True
End If
i += 1
End While
Return False
End Function
 
There are a great many issues there. First of all, if you're going to post code then don't use
tags. They are for quotes. For code use
VB.NET:
 or [xcode=vb] tags, which will format your code and, in the latter case, apply syntax highlighting.

As for the code, the first point to note is to ALWAYS specify the return type of a function.  If your function returns a Boolean then specify the the return type of the function as Boolean.  The second point is that there are multiple types of loops because each one is suited to slightly different conditions and your conditions call for a For each loop, not a While loop.

As for the question, you say that you need to search for two values.  Will it always be two or might it be one sometimes and two sometimes?  Could it ever be more than two?  How is the user going to enter these multiple values?  What data type is the column your checking?
 
hi guys.
i hav some problem about vb.net
how to check 2 record already exist in database
i use database oracle 10g and vb.net2010

this my coding to check 1 record already exist but i need check 2 in same time
sry my english bad
First off, I renamed your thread to a much more meaningful title.

Secondly, is there a reason you can't query the database looking for the str value int he "nik" field?
If not, then how about creating a DataView filtering the records from the original table?
Private Function IsIdExist(ByVal id As String, ByVa) As Boolean
    Dim Output As Boolean = False

    Dim DV As New DataView(Dst.Tables("hrd.employee"), String.Format("nik='{0}'", id), String.Empty, DataViewRowState.CurrentRows)
    Output = (DV.Count = 1)
    DV.Dispose()

    Return Output
End Function
To check for 2 ID's at the same time you would:
Private Function IsIdExist(ByVal id As String, ByVal id2 As String) As Boolean
    Dim Output As Boolean = False

    Dim DV As New DataView(Dst.Tables("hrd.employee"), String.Format("nik='{0}' Or nik='{1}'", id, id2), String.Empty, DataViewRowState.CurrentRows)
    Output = (DV.Count = 1)
    DV.Dispose()

    Return Output
End Function
I'm going with the assumption that you need to know if either of the two exists already and not know which of the two exists if it's only 1, otherwise you would just call the first function twice, one for each ID.
 
jmcilhinney@ sorry for the mistake I did in the first post...
and I will not repeat it again


JuggaloBrotha@thanks for ur correct my new post ^.^

nik is field sir
and i need to check 2 field already exist in database oracle
e.x : when i want to save data in vb.net when 2 nik same and 2 different locations at the time to check
 
VB.NET:
[/COLOR]Public Function Duplicate() As Boolean        Dim dset As New DataSet
        Dim str As String
        With Me
            str = "Select count(*) from employee where nik = '" & txtnik.Text & "' and location = '" & Microsoft.VisualBasic.Right(cbolocation.Text, 6) & "'"
            Cmd.Connection = Con
            Cmd.CommandText = str
            Dad.SelectCommand = Cmd
            Dad.Fill(dset, "employee")
            dtbl = dset.Tables("employee")
            If dtbl.Rows.Count > 0 Then
                Duplicate = True
                'MsgBox("Word already exist. Please check the word.", MsgBoxStyle.Critical, "Duplicate.")


            Else
                Duplicate = False
            End If
            Con.Close()
        End With

    End Function[COLOR=#333333]



any wrong with my coding sir?
 
Last edited:
VB.NET:
[/COLOR]Public Function Duplicate() As Boolean        Dim dset As New DataSet
        Dim str As String
        With Me
            str = "Select count(*) from employee where nik = '" & txtnik.Text & "' and location = '" & Microsoft.VisualBasic.Right(cbolocation.Text, 6) & "'"
            Cmd.Connection = Con
            Cmd.CommandText = str
            Dad.SelectCommand = Cmd
            Dad.Fill(dset, "employee")
            dtbl = dset.Tables("employee")
            If dtbl.Rows.Count > 0 Then
                Duplicate = True
                'MsgBox("Word already exist. Please check the word.", MsgBoxStyle.Critical, "Duplicate.")


            Else
                Duplicate = False
            End If
            Con.Close()
        End With

    End Function[COLOR=#333333]



any wrong with my coding sir?
Three points:

1. Don't use string concatenation to insert values into SQL code. To learn why and how to use parameters, follow the Blog link in my signature and check out my post on Parameters In ADO.NET.

2. Don't use a data adapter and DataSet when all you need is one value. Create a command and call ExecuteScalar.

3. Don't assign to the function name itself. Always use a Return statement.
 
Three points:

1. Don't use string concatenation to insert values into SQL code. To learn why and how to use parameters, follow the Blog link in my signature and check out my post on Parameters In ADO.NET.

2. Don't use a data adapter and DataSet when all you need is one value. Create a command and call ExecuteScalar.

3. Don't assign to the function name itself. Always use a Return statement.


thanks for suggestion sir...
I will fix this error
:peaceful:
 
Back
Top