Click here to Skip to main content
15,903,203 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can check the record is exists or not? And also i can insert the record when its not existing


Private Sub AddButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddButton.Click
            con = New SqlConnection(Constr)
            con.Open()
            Dim cmdstr1 As String = "Select * from Mcdaily where Mcno='" & McnoCombo.Text & "'and Ftime=" & DGVlogentry.Item(2, i).Value & ""
            dt = DataManager.ExecuteQuery(cmdstr1, Constr)
            If (dt.Rows.Count > 0) Then
                MessageBox.Show("Already Exists")
            Else
                 Insertcmd()
             End if 
End sub
Posted
Updated 16-Aug-11 19:48pm
v2
Comments
Toniyo Jackson 17-Aug-11 2:38am    
What you are doing is correct. Are you getting any error?

You code for checking is already there.
So where is your problem?
 
Share this answer
 
This code is correct, where are you struck?

A little suggestion from me.
You can use "select count(*)......." rather than "select *.......".
Then you can use ExecuteScalar() method which will directly return count of rows and you won't need a datatable to work with.
Just a suggestion. :)

BTW, what problem you are facing?
 
Share this answer
 
Also use parameters instead of concatenating string to the SQL text. For more info: SqlParameter[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900