Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Imports System.Security
Imports System.Security.Cryptography
Imports System.Text


Public Class makerCode

    Private cnstr As String
    Dim conn As SqlConnection
    Dim myQuery As String

    Public Function getmakercode(ByVal toshpn As String, ByVal partslot As String)
        Dim makerCode As String = ""
        'conn = New SqlConnection("data source=192.168.1.41;initial catalog=Toshiba2;user=cats;password=dogs;Integrated Security=false")
        conn = New SqlConnection("Initial Catalog=Toshiba_kanban; Data Source=192.168.1.41; Persist Security Info = True;User ID=cats;Password=dogs;")
        myQuery = "DECLARE @MAKERTEMPLATE as varchar(MAX)" & _
            "TRUNCATE TABLE [Toshiba_kanban].[dbo].[MAKERCODELIST_KANBAN] " & _
            "INSERT INTO [Toshiba_kanban].[dbo].[MAKERCODELIST_KANBAN] SELECT DISTINCT(makerCode) as makerCode FROM [Toshiba_kanban].[dbo].[makerCode] WHERE Ionics_PN='" & toshpn & "'" & _
                "WHILE exists (SELECT * FROM [Toshiba_kanban].[dbo].[MAKERCODELIST_KANBAN]) BEGIN " & _
                    "SELECT @MAKERTEMPLATE = makerCode " & _
                    "FROM [Toshiba_kanban].[dbo].[MAKERCODELIST_KANBAN] " & _
                    "IF (SELECT CHARINDEX(@MAKERTEMPLATE,'" & partslot & "')) <> 0 BEGIN " & _
                        "(SELECT makerCode FROM [Toshiba_kanban].[dbo].[makerCode] WHERE makerCode=@MAKERTEMPLATE) " & _
                    "END " & _
                    "DELETE FROM [Toshiba_kanban].[dbo].[MAKERCODELIST_KANBAN] " & _
                    "WHERE makerCode=@MAKERTEMPLATE " & _
                "END "

        Dim mycommand As SqlCommand
        mycommand = New SqlCommand(myQuery, conn)

        Try
            conn.Open()
            Dim reader As SqlDataReader = mycommand.ExecuteReader()
            If (reader.HasRows) Then
                reader.Read()
                makerCode = reader("makerCode").ToString.ToUpper

            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            conn.Close()
        End Try
        Return makerCode

    End Function
End Class
