Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
VB
Imports MySql.Data.MySqlClient
Public Class UserAdmin
Public mycon As New MySqlConnection
Public myadap As New MySqlDataAdapter
Public mycmd As New MySqlCommand
Public myds As New DataSet
Public rec As Integer
Public sb As Boolean
Public idnum As String
Public trec As Integer

Sub connect()
        mycon = New MySqlConnection
        mycon.ConnectionString = "server=localhost;user id=root;password=;database=dbase"
        mycon.Open()
        mycmd.Connection = mycon
        mycmd.CommandText = "select*from tbluseradmin"
        myds = New DataSet
        myadap.SelectCommand = mycmd
        myadap.Fill(myds, "tbl")
        trec = myds.Tables("tbl").Rows.Count - 1
        rec = 0
End Sub

Private Sub UserAdmin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       Call connect()
       Call display()
end sub

Private Sub cmdsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdsave.Click
        If txtid.Text = "" Or txtname.Text = "" Or txtmn.Text = "" Or txtln.Text = "" Or cbogender.Text = "" Or cbosub.Text = "" Or cbopos.Text = "" Then
            MsgBox("Complete All Fields Needed", MsgBoxStyle.Information, "")
            txtid.Focus()
        ElseIf IsNumeric(txtid.Text) = False Then
            MsgBox("ID number should be numbers only")
        ElseIf sb = True Then
            mycmd.Connection = mycon
            mycmd.CommandText = "insert into `tbluseradmin`(`ID`,`Name`,`MiddleName`,`LastName`,`Gender`,`Subject`,`Position`)values('" & txtid.Text & "','" & txtname.Text & "','" & txtmn.Text & "','" & txtln.Text & "','" & cbogender.Text & "','" & cbosub.Text & "','" & cbopos.Text & "')"
            mycmd.ExecuteNonQuery()
            mycon.Close()
            My.Computer.FileSystem.CopyFile(ofd.FileName, Application.StartupPath & "\images\" & txtid.Text & ".jpg")
            MsgBox("Record Save", MsgBoxStyle.Information, "")
            Call connect()
            Call display()
            Call textboxlocked()
            Call buttonunlock()
            Call cmdlock()
            Call cmd2unlock()
        Else
            mycmd.Connection = mycon
            mycmd.CommandText = "update `tbluseradmin` set`ID`='" & txtid.Text & "',`Name`='" & txtname.Text & "',`MiddleName`='" & txtmn.Text & "',`LastName`='" & txtln.Text & "',`Gender`='" & cbogender.Text & "'where`ID`='" & idnum & "'"
            mycmd.ExecuteNonQuery()
            mycon.Close()
            My.Computer.FileSystem.CopyFile(ofd.FileName, Application.StartupPath & "\images\" & txtid.Text & ".jpg")
            MsgBox("Record Update", MsgBoxStyle.Information, "")
            Call connect()
            Call display()
            Call textboxlocked()
            Call buttonunlock()
            Call cmdlock()
            Call cmd2unlock()
        End If
End Sub
Posted
Updated 20-Sep-14 20:00pm
v3
Comments
ChauhanAjay 21-Sep-14 1:19am    
Basing on which column will you check for duplicates?

1 solution

The solution below is with the assumption that the "ID" column is not auto incremented value.

1. The user enters the value of the 'ID' from the front end.
2. Check if that value exists in your dataset.
3. If it does not exists in your dataset then insert the value in the database.
4. If that value exists in the dataset then display a message.

Below I will just show you how to change the insert part

VB
Dim result() As DataRow = myds.Tables(0).select("ID = '"+ txtid.Text +"'")
If (result.Length == 0) Then
	mycmd.Connection = mycon
    mycmd.CommandText = "insert into `tbluseradmin`(`ID`,`Name`,`MiddleName`,`LastName`,`Gender`,`Subject`,`Position`)values('" & txtid.Text & "','" & txtname.Text & "','" & txtmn.Text & "','" & txtln.Text & "','" & cbogender.Text & "','" & cbosub.Text & "','" & cbopos.Text & "')"
    mycmd.ExecuteNonQuery()
    mycon.Close()
    My.Computer.FileSystem.CopyFile(ofd.FileName, Application.StartupPath & "\images\" & txtid.Text & ".jpg")
    MsgBox("Record Save", MsgBoxStyle.Information, "")
Else
	MsgBox("ID already exists")
End


Hope the above code helps.
 
Share this answer
 
Comments
Member 11096118 21-Sep-14 8:56am    
thank you so much ......^_^....it works...i can finish my project now thank you so much .....

chauhanajay thanks^^
ChauhanAjay 21-Sep-14 9:29am    
request you to kindly accept the answer as solved.

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