SQL Server connection related error - vb.net

Below error is thrown in a already opened program which connects to different database
A transport-level error has occurred when sending the request to the server. (provider: TCP Provider, error: 0 - An existing connection
was forcibly closed by the remote host.)
Program details: code done using vb.net with SQL Server 2008 as back-end
Two instance of same exe where running simultaneously in same PC but error was thrown only by one instance
Program uses both SqlConnection (ADO.NET) and ADODB connection (upgraded from VB6) and the error is thrown by both types of connection
If the error is due to network problem with the server then why few programs work fine? I am not able to trace the reason for this behavior of the program
Can I know why this error occurs and why only in few programs of same instance

When each instance of your exe connects, it gets its own SQL Server process ID (SPID). You are getting the error because your instance's connection was either killed at the server level or disrupted at the network level, at some point before it tried to execute the SQL command where the error occurred.
In SSMS, check your SQL Server Log file. In this example I killed a SPID, and the killing of the SPID was logged:
If you don't see a logged event, you are probably dealing with a network issue. Further troubleshooting might involve setting one or more break points in your code somewhere after it opens the ADODB connection, but before the error, and then verifying through SSMS that a SPID for that instance is running at the break point. exec sp_who2 is a good command to list all currently active SPIDs on the server.

Imports System.Data
Imports System.Data.SqlClient
Public Class Form2
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Function call_add()
Dim tam, eng, mat, sci, soc, tot As Decimal
tam = 0
eng = 0
mat = 0
sci = 0
soc = 0
tot = 0
If tamil.Text <> "" Then
tam = Convert.ToDecimal(tamil.Text)
End If
If english.Text <> "" Then
eng = Convert.ToDecimal(english.Text)
End If
If maths.Text <> "" Then
mat = Convert.ToDecimal(maths.Text)
End If
If science.Text <> "" Then
sci = Convert.ToDecimal(science.Text)
End If
If social.Text <> "" Then
soc = Convert.ToDecimal(social.Text)
End If
tot = tam + eng + mat + sci + soc
total.Text = tot.ToString
percentage.Text = total.Text / 500 * 100
Return False
End Function
Private Sub total_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles total.TextChanged
call_add()
End Sub
Private Sub percentage_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles percentage.TextChanged
call_add()
End Sub
Private Sub tamil_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tamil.TextChanged
call_add()
End Sub
Private Sub english_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles english.TextChanged
call_add()
End Sub
Private Sub maths_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles maths.TextChanged
call_add()
End Sub
Private Sub science_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles science.TextChanged
call_add()
End Sub
Private Sub social_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles social.TextChanged
call_add()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim con As SqlConnection
Dim cmd As SqlCommand
'Dim str As String
con = New SqlConnection("server=WHITE-PC\WHITEPC;initial catalog=markreg;Integrated Security=True")
cmd = New SqlCommand
con.Open()
Dim command As New SqlCommand
cmd.Connection = con
command = New SqlCommand("stdregno,stdname,tamil,english,maths,science,social,total,percentage", con)
command.ExecuteNonQuery()
con.Close()
MsgBox("Added Sucessfully", MsgBoxStyle.Information, "Succesfully")
call_clear()
End Sub
Private Function call_clear()
stdregno.Text = ""
stdname.Text = ""
tamil.Text = ""
english.Text = ""
maths.Text = ""
science.Text = ""
social.Text = ""
total.Text = ""
percentage.Text = ""
End Function
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Form1.Show()
Me.Hide()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
InputBox("Enter the Regno You want search")
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
InputBox("Enter the Regno You want Modify")
End Sub
End Class

Related

detailed scanning of unwanted files

i am trying to make launcher for my server, but i am kinda worried, is there any way that i can make a list of unwanted files in browsed directory, and if script finds any of those files when i press this button it will show like this warning message, ive tried a couple of things but none works
Imports System.IO
Public Class imeprezime
Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If (FolderBrowserDialog1.ShowDialog() = DialogResult.OK) Then
TextBox2.Text = FolderBrowserDialog1.SelectedPath
End If
End Sub
Private Sub connect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles connect.Click
Dim cleoPath As String
Dim filePath As String
filePath = System.IO.Path.Combine(TextBox2.Text, "gta_sa.exe")
cleoPath = System.IO.Path.Combine(TextBox2.Text, "cleo")
If File.Exists(filePath) Then
If Directory.Exists(cleoPath) Then
MsgBox("Pronasli smo cleo fajl u vasem GTA root folderu. Da se konektujete na server morate ga obrisati.")
Else
Dim p() As Process
p = Process.GetProcessesByName("gta_sa")
If p.Count > 0 Then
MsgBox("Vec imate pokrenut GTA San Andreas.")
Else
' Process is not running
End If
End If
Else
MsgBox("Da se konektujete morate locirati GTA San Andreas folder.")
End If
End Sub
Private Sub imeprezime_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class

MultiThreading and Sockets/TCP [VB.NET]

I started learning about TCP/Sockets yesterday and decided to make a chatbox for a friend and I.
Unfortunately, i am having some difficulties with MultiThreading.
Whenever i am using it, i can no longer receive messages from my friend.
But, if i disable it then, everything works perfectly.
I don't know what's going on here, could somebody help?
Imports System.Net.Sockets
Imports System.Net
Public Class ServerClient
Dim _TCPServer As Socket
Dim _TCPListener As TcpListener
Dim _ListenerThread As System.Threading.Thread
Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
'When Submit is pressed, send some text to the client
Dim bytes() As Byte = System.Text.Encoding.ASCII.GetBytes(txtInput.Text)
txtBox.AppendText(vbCrLf & "Server: " & txtInput.Text)
txtInput.Clear()
_TCPServer.Send(bytes)
End Sub
Private Sub TCPListen()
'If somebody calls port 2424, accept it, unblock the socket and start the timer
_TCPListener = New TcpListener(IPAddress.Any, 2424)
_TCPListener.Start()
_TCPServer = _TCPListener.AcceptSocket()
btnSend.Enabled = True
txtBox.AppendText("Connection Established" & vbCrLf)
_TCPServer.Blocking = False
_Timer.Enabled = True
End Sub
Private Sub _Timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _Timer.Tick
'If data has been sent, receive it
Try
Dim rcvdbytes(_TCPServer.ReceiveBufferSize) As Byte
_TCPServer.Receive(rcvdbytes)
txtBox.AppendText(vbCrLf & "Client: " & System.Text.Encoding.ASCII.GetString(rcvdbytes) & vbCrLf)
Catch ex As Exception
End Try
End Sub
Private Sub ServerClient_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Link the new thread to TCPListen(), allow access to all threads and wait for a call
_ListenerThread = New Threading.Thread(AddressOf TCPListen)
Control.CheckForIllegalCrossThreadCalls = False
txtBox.AppendText("Waiting for connection.." & vbCrLf)
btnSend.Enabled = False
_ListenerThread.Start()
End Sub
End Class
This example project contains four classes - TcpCommServer, TcpCommClient, clsAsyncUnbuffWriter and CpuMonitor. With these classes, you will not only be able to instantly add TCP/IP functionality to your VB.NET applications, but also has most of the bells and whistles we're all looking for. With these classes, you will be able to connect multiple clients to the server on the same port. You will be able to easily: throttle bandwidth to the clients, and send and receive files and data (text?) along 250 provided channels simultaneously on a single connection.
http://www.codeproject.com/Articles/307315/Reusable-multithreaded-tcp-client-and-server-class
Well, i learned BackgroundWorkers could do the exact same thing and now it all works.
Private Sub ServerClient_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Wait for a call
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
TCPListen()
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
_Timer.Enabled = True
End Sub

Serial port read includes what has been written in vb.net

So I am trying to talk to a gsm modem using AT commands, I'm trying to do a basic test to make sure the modem is ok by sending the command AT and receiving OK. The issue is that I'm receiving "AT Blank Line OK", is there any way so when I send AT, I only read OK?
Imports System.IO.Ports
Public Class GUI
Dim Device As New System.IO.Ports.SerialPort()
Private Sub GUI_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Port.Text = "Select Port"
For Each item As String In IO.Ports.SerialPort.GetPortNames
Port.Items.Add(item)
Next
Device.BaudRate = 9600
Device.Parity = Parity.None
Device.StopBits = StopBits.One
Device.DataBits = 8
Device.Handshake = Handshake.RequestToSend
Device.DtrEnable = True
Device.RtsEnable = True
Device.NewLine = vbCrLf
Device.WriteTimeout = 5000
End Sub
Private Sub Port_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Port.SelectedIndexChanged
Device.PortName = Port.SelectedItem.ToString
End Sub
Private Sub Send_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Send.Click
Try
Device.Open()
Dim msg As String
msg = Message.Text
Device.DiscardInBuffer()
Device.DiscardOutBuffer()
Device.Write("AT" & vbCrLf)
MsgBox(Device.ReadExisting())
Catch ex As Exception
MsgBox("Error!")
End Try
Device.Close()
End Sub
End Class
Sounds like the modem is set to Echo commands. To turn this off try sending E0 first.
More info here

VB program, looping for iterations and excel writing

I'm writing a program for an internship and need some advice. I've done my research but have mostly returned fruitless... I need to loop the "buttonOneClick for one second iterations. The program will send a "P" character, wait one second, send a p, wait one second, etc... Also I need to write the information it receives to an excel spreadsheet. Any help/critiquing of existing code would be greatly appreciated.
Here's what I have:
Public Class Form2
Dim buttonOnePush As Boolean = False
Dim buttonTwoPush As Boolean = False
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Send strings to a serial port.
Using com5 As IO.Ports.SerialPort =
My.Computer.Ports.OpenSerialPort("COM5")
com5.WriteLine("P")
End Using
End Sub
Function ReceiveSerialData() As String
' Receive strings from a serial port.
Dim returnStr As String = ""
Dim com5 As IO.Ports.SerialPort = Nothing
Try
com5 = My.Computer.Ports.OpenSerialPort("COM5")
com5.ReadTimeout = 10000
Do
Dim Incoming As String = com5.ReadLine()
If Incoming Is Nothing Then
Exit Do
Else
returnStr &= Incoming & vbCrLf
End If
Loop
Catch ex As TimeoutException
returnStr = "Error: Serial Port read timed out."
Finally
If com5 IsNot Nothing Then com5.Close()
End Try
Return returnStr
End Function
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
If IsNumeric(TextBox1.Text) AndAlso IsNumeric(TextBox2.Text) Then
TextBox1.Text = CDec(TextBox2.Text)
End If
End Sub
Private Sub TextBox6_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox6.TextChanged
If IsNumeric(TextBox6.Text) AndAlso IsNumeric(TextBox3.Text) Then
TextBox6.Text = CDec(TextBox3.Text)
End If
End Sub
Private Sub TextBox7_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox7.TextChanged
If IsNumeric(TextBox7.Text) AndAlso IsNumeric(TextBox4.Text) Then
TextBox7.Text = CDec(TextBox4.Text)
End If
End Sub
Private Sub TextBox8_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox8.TextChanged
If IsNumeric(TextBox8.Text) AndAlso IsNumeric(TextBox5.Text) Then
TextBox8.Text = CDec(TextBox5.Text)
End If
End Sub
Private Sub TextBox15_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox15.TextChanged
If IsNumeric(TextBox15.Text) AndAlso IsNumeric(TextBox16.Text) Then
TextBox15.Text = Hex(TextBox16.Text)
End If
End Sub
Private Sub TextBox14_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox14.TextChanged
If IsNumeric(TextBox14.Text) AndAlso IsNumeric(TextBox11.Text) Then
TextBox14.Text = Hex(TextBox11.Text)
End If
End Sub
Private Sub TextBox13_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox13.TextChanged
If IsNumeric(TextBox13.Text) AndAlso IsNumeric(TextBox10.Text) Then
TextBox13.Text = Hex(TextBox10.Text)
End If
End Sub
Private Sub TextBox12_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox12.TextChanged
If IsNumeric(TextBox12.Text) AndAlso IsNumeric(TextBox9.Text) Then
TextBox12.Text = Hex(TextBox9.Text)
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
buttonTwoPush = True
buttonOnePush = False
Me.Close()
Form1.Close()
End Sub
End Class
Use a Timer to issue that command at one second intervals. Drag a Timer over to your form from the toolbox, and double click on it to get a _Tick method.
Set the .Interval member of the timer in your form's constructor, and use the .Start and .Stop methods to control it.
For the Excel piece, you'll need to add a reference to the project for the Microsoft Excel 12.0 (or 14.0 if you have Excel 2010) Object Library. Find this under the COM tab of the Add Reference dialog which you get by right clicking on the project in the Solution Explorer. See this page for an exhaustive reference (scroll down to the bottom of the page for a quick example in VB.NET).

Very basic VB.Net & Serial IO issue

After searching around I am still having issues with reading data from a serial port in VB.Net/VS2010. I know the Serial Port works, I can write to the port fine but when reading from it, nothing happens. I have only been programming for the last 3 weeks so am still trying to get my head around it all.
The program must run to capture data from a door logger, I will then be outputting the data to a database (not yet implemented - I want to get this part sorted first).
I have tried using several terminal programs as well as another device which outputs data onto the serial line, with nothing displaying in the textbox tbxIn.
Any help would be greatly appreciated.
Code is below:
Imports System.IO.Ports
Imports System.IO.Ports.SerialPort
Public Class Form1
Dim comPort As IO.Ports.SerialPort = Nothing
Dim sComPort As String = ""
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
GetSerialPortNames()
End Sub
Sub GetSerialPortNames()
' Show all available COM ports.
For Each sp As String In My.Computer.Ports.SerialPortNames
lstPorts.Items.Add(sp)
Next
End Sub
Private Sub lstPorts_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstPorts.SelectedIndexChanged
sComPort = lstPorts.SelectedItem
Button1.Enabled = True
End Sub
Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Open the serial port using the OpenSerialPort method
Button1.Enabled = False
Button2.Enabled = True
Try
comPort = My.Computer.Ports.OpenSerialPort(sComPort, 9600, IO.Ports.Parity.None, 8, 1)
' comPort.DtrEnable = True
comPort.ReadTimeout = 500
Do
comPort.WriteLine("Go")
Dim sIncomming As String = comPort.ReadLine()
tbxIn.Text = sIncomming & vbCrLf
Loop
Catch ex As TimeoutException
tbxIn.Text &= "Error: Serial Port Read Timeout" & vbCrLf
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
comPort.Close()
Button1.Enabled = True
Button2.Enabled = False
End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
tbxIn.Text = e.ToString
End Sub
End Class
PRetty sure this will get you what you need. You DON't need the Serial1 component on the designer. Remove that and use this code:
Private comPort As IO.Ports.SerialPort = Nothing
Private sComPort As String = ""
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
GetSerialPortNames()
End Sub
Sub GetSerialPortNames()
' Show all available COM ports.
For Each sp As String In My.Computer.Ports.SerialPortNames
lstPorts.Items.Add(sp)
Next
End Sub
Private Sub lstPorts_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstPorts.SelectedIndexChanged
sComPort = lstPorts.SelectedItem
Button1.Enabled = True
End Sub
Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Open the serial port using the OpenSerialPort method
Button1.Enabled = False
Button2.Enabled = True
Try
comPort = My.Computer.Ports.OpenSerialPort(sComPort, 9600, IO.Ports.Parity.None, 8, 1)
' comPort.DtrEnable = True
'must add handler
AddHandler comPort.DataReceived, AddressOf SerialPort1_DataReceived
comPort.ReadTimeout = 500
Do
comPort.WriteLine("Go")
Dim sIncomming As String = comPort.ReadLine()
tbxIn.Text = sIncomming & vbCrLf
Loop
Catch ex As TimeoutException
tbxIn.Text &= "Error: Serial Port Read Timeout" & vbCrLf
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
comPort.Close()
'remove handler
RemoveHandler comPort.DataReceived, AddressOf SerialPort1_DataReceived
Button1.Enabled = True
Button2.Enabled = False
End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs)
tbxIn.Text = e.ToString
End Sub