Private Sub Glass_Click(sender As System.Object, e As System.EventArgs) Handles Glass.Click
Dim strBatch As String = lstBatch.Text
If strBatch = "" Then
MsgBox("Please select a batch and try again")
Exit Sub
End If
'Dim cn As SqlClient.SqlConnection = New SqlClient.SqlConnection("Data Source = THOECHERL-PC;Initial Catalog = TWO; " & _
' "Persist Security Info= True;Connection Timeout = 0;User ID=sa;Password=Great Plains!")
Dim cn As SqlClient.SqlConnection = New SqlClient.SqlConnection("Data Source = THOR;Initial Catalog = TEST2; " & _
"Persist Security Info= True;Connection Timeout = 0;User ID=sa;Password=Great Plains!")
'Dim Port As Devices.Ports
Dim Port As String
Dim Host, Button As String
Dim Header As String
Dim LabelFormat As String
Dim Bin, Assorted_bin, Item_number, Custname, Order_Number, Batch_Number, Color, Order, Customer, Batch As String
Dim Glass_Assorted_bin, Glass_Type_1, Glass_Type_2, Mull_Config, Mull_Location, Special_Instructions, WinPart, GlassSize, Glass_Field, MullInfo, Spec_Inst, AssBin, PartNum, BarcodeOrder, AssortBin As String
Dim Line_Number, SeriesQty As Integer
Dim Spacer_Thickness, Qty, FixedWidth, FixedHeight, VentWidth, VentHeight, ScreenHeight, ScreenWidth As String
Dim Quantity As Double
Button = Glass.Name.ToString
Dim cmd As SqlClient.SqlCommand = New SqlClient.SqlCommand
cmd.Connection = cn
cmd.CommandType = CommandType.Text
cn.Open()
cmd.CommandText = "SELECT PORT FROM PCT_LABEL_CONFIG WHERE NAME = '" & Button & "'"
Port = cmd.ExecuteScalar()
cmd.CommandText = "SELECT HOST FROM PCT_LABEL_CONFIG WHERE NAME = '" & Button & "'"
Host = cmd.ExecuteScalar()
cmd.CommandText = "SELECT HEADINGFORMAT FROM PCT_LABEL_CONFIG WHERE NAME = '" & Button & "'"
Header = cmd.ExecuteScalar()
cmd.CommandText = "SELECT LabelFormat FROM PCT_LABEL_CONFIG WHERE NAME = '" & Button & "'"
LabelFormat = cmd.ExecuteScalar()
cn.Close()
Dim IPs As IPHostEntry = Dns.GetHostEntry(Host) ' Grab this host's IPs
Dim IP As IPAddress = IPs.AddressList(0) ' Let's use the first IP listed for this host.
Dim EP As New IPEndPoint(IP, Port) ' Establish an endpoint.
PrintSocket.Connect(EP) ' Connect the socket.
PrintSocket.Send(System.Text.Encoding.ASCII.GetBytes("<STX><ESC>C<ETX>" & vbCrLf)) ' Clean slate for commands.
PrintSocket.Send(System.Text.Encoding.ASCII.GetBytes("<STX><ESC>P<ETX>" & vbCrLf)) ' Enter programming mode.
PrintSocket.Send(System.Text.Encoding.ASCII.GetBytes("<STX>E5;F5;<ETX>" & vbCrLf)) ' Erase format 5, Start programming format 5
PrintSocket.Send(System.Text.Encoding.ASCII.GetBytes("<STX>" & Header & "<ETX>" & vbCrLf)) ' Send the two field definitions from our table.
PrintSocket.Send(System.Text.Encoding.ASCII.GetBytes("<STX>R<ETX>" & vbCrLf)) ' End of programming mode
' The next four lines are the heading label's data
PrintSocket.Send(System.Text.Encoding.ASCII.GetBytes("<STX><ESC>E5<CAN><ETX>" & vbCrLf)) ' Announce label data should use format 5
PrintSocket.Send(System.Text.Encoding.ASCII.GetBytes("<STX>" & Button & "<CR><ETX>" & vbCrLf)) ' The first field is the name of the report
PrintSocket.Send(System.Text.Encoding.ASCII.GetBytes("<STX>" & strBatch & "<CR><ETX>" & vbCrLf)) ' The second and final field is the batch numnber.
PrintSocket.Send(System.Text.Encoding.ASCII.GetBytes("<STX><ETB><ETX>" & vbCrLf)) ' End of this label.
' These lines are the format info for the label body
PrintSocket.Send(System.Text.Encoding.ASCII.GetBytes("<STX><ESC>C<ETX>" & vbCrLf)) ' Clean slate for commands.
PrintSocket.Send(System.Text.Encoding.ASCII.GetBytes("<STX><ESC>P<ETX>" & vbCrLf)) ' Enter programming mode.
PrintSocket.Send(System.Text.Encoding.ASCII.GetBytes("<STX>E5;F5;<ETX>" & vbCrLf)) ' Erase format 5, Start programming format 5
PrintSocket.Send(System.Text.Encoding.ASCII.GetBytes("<STX>" & LabelFormat & "<ETX>" & vbCrLf)) ' Send the field definitions from our table.
PrintSocket.Send(System.Text.Encoding.ASCII.GetBytes("<STX>R<ETX>" & vbCrLf)) ' End of programming mode
PrintSocket.Send(System.Text.Encoding.ASCII.GetBytes("<STX><ESC>E5<CAN><ETX>" & vbCrLf)) ' Announce label data should use format 5
When I don't close the PrintSocket at the end of the code I get the error message on the attachment when I try to reopen the socket at the beginning of the code (PrintSocket.Connect(EP)). I have two other labels that I am printing that have the same code as far as connecting and disconnecting the socket, but neither of them give me the error.
There must be something else in this code that is causing the problem.
I was able to resolve the issue. By comparing this code with the code of an identical process that was working, I realized that I was missing this line:
' Create the TCP socket.
Dim PrintSocket As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Once I added that back in the problem disappeared.
Visual Basic .NET (VB.NET) is an object-oriented programming language implemented on the .NET framework, but also supported on other platforms such as Mono and Silverlight. Microsoft launched VB.NET as the successor to the Visual Basic language. Though it is similar in syntax to Visual Basic pre-2002, it is not the same technology,
It's because you closed the PrintSocket at the end of your code. Do not close it or you have to re-open it before call Connect() function.
Cheer.