Imports MySql.Data.MySqlClient
Imports System.Data
Imports System.Configuration

Public Class frmMasterCode
    Dim conn As New MySqlConnection
    Dim myDataAdapter As New MySqlDataAdapter
    Dim sqlquery As String
    Dim cmd As New MySqlCommand()
    Dim myDataReader As MySqlDataReader

    Private aeps1DataSet As epsDataSet
    Private amasterCodeTableAdapter As epsDataSetTableAdapters.master_codeTableAdapter
    Private WithEvents amaster_codeBindingSource As BindingSource



    Private Sub frmMasterCode_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        RetrieveData()
    End Sub

    Private Sub RetrieveData()
        Try

            aeps1DataSet = New epsDataSet
            amasterCodeTableAdapter = New epsDataSetTableAdapters.master_codeTableAdapter
            amasterCodeTableAdapter.Fill(aeps1DataSet.master_code)

            'set up stores binding source

            amaster_codeBindingSource = New BindingSource

            With amaster_codeBindingSource
                .DataSource = Me.aeps1DataSet
                .DataMember = "master_code"
            End With

            With cboMcCode
                .DataSource = Me.amaster_codeBindingSource
                .DisplayMember = "mc_id"
                .ValueMember = "mc_id"
                .DataBindings.Add("text", amaster_codeBindingSource, "mc_id", False, DataSourceUpdateMode.Never)
                .DropDownStyle = ComboBoxStyle.DropDown

            End With



            txtMcDesc.DataBindings.Add("text", amaster_codeBindingSource, "mc_desc")

            conn = New MySqlConnection()
            conn.ConnectionString = "server=localhost; user id=root; database=eps"
            conn.Open()

            sqlquery = "SELECT mc_id, mc_desc FROM master_code "
            Dim myCommand As New MySqlCommand()
            myCommand.Connection = conn
            myCommand.CommandText = sqlquery

            'start query
            myDataAdapter.SelectCommand = myCommand


            Dim myData As MySqlDataReader
            myData = myCommand.ExecuteReader()
            myData.Read()

            ' cboMcCode.Text = CStr(myData("mc_id"))
            'txtMcDesc.Text = CStr(myData("mc_desc"))

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
        Finally
            conn.Close()
        End Try
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        clearTextBox()
    End Sub

    Private Sub clearTextBox()
        cboMcCode.Text = ""
        txtMcDesc.Text = ""
        cboMcCode.Focus()
    End Sub

    Private Sub insertData()
        Dim strSql As String

        strSql = "INSERT INTO master_code(mc_id, mc_desc) VALUES ('" & cboMcCode.Text & "','" & txtMcDesc.Text & "')"
        conn.ConnectionString = "server=localhost;user id=root;database=eps"


        conn.Open()
        cmd.Connection = conn
        cmd.CommandText = strSql

        myDataAdapter.SelectCommand = cmd


        myDataReader = cmd.ExecuteReader()


        With cmd.Parameters
            .AddWithValue("@mc_id", cboMcCode.Text)
            .AddWithValue("@mc_desc", txtMcDesc.Text)
        End With


        conn.Close()

        If myDataReader.HasRows = False Then
            MsgBox("Save Data Succeed")
            amaster_codeBindingSource.CancelEdit()
            clearTextBox()
        Else
            MsgBox("Data Cannot Saved", MsgBoxStyle.OkOnly)
        End If

        conn.Close()
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        validateData()
        
    End Sub

    Private Sub validateData()
        Dim sqlquery As String

        conn.ConnectionString = "server=localhost;user id=root;database=eps"
        sqlquery = "SELECT mc_id FROM master_code Where mc_id='" & cboMcCode.Text & "'"

        Try
            conn.Open()
        Catch myerror As MySqlException
            MsgBox("Error Connecting to Database: " & myerror.Message)
        End Try

        Dim myCommand As New MySqlCommand()
        myCommand.Connection = conn
        myCommand.CommandText = sqlquery

        'start query
        myDataAdapter.SelectCommand = myCommand
        Dim myData As MySqlDataReader
        myData = myCommand.ExecuteReader()

        'see if user exits.
        If myData.HasRows = False Then
            'MsgBox("Invalid Login Details")
            conn.Close()
            insertData()
            RetrieveData()
        Else
            'Dim frmMain As New frmMain
            'frmMain.Show()
            'Me.Visible = False
            conn.Close()
            MsgBox("Duplicate Master Code!")
            clearTextBox()
        End If
    End Sub

End Class

This causes two bindings in the collection to bind the same property.
Parameter: binding

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.