hi Guys,

i have several clients on a LAN network, these clients all have access to a particular sql server. i want to write an application that allows any client on the network to send one or more messages to another client on the network providing that both clients have access to the specified sql server.


i don't know what class or component i should read on, if anyone has information on how to achieve this i would be grateful.

Regards

I created a similar thing on a project once.
Here's what I did.

I created a class with two main methods. One for reading messages from the database, and one for writing the message to the database.
I then used threading to run that class and let it trigger the read method every 5 minutes (or so).
If a message was found, destined to a user running a certain instance of the client it displayed a MessageBox containing who sent it, when and the message.

Here is the class I created.
It was a while ago, so it may seem a bit ugly. (But it works, mostly.)

Imports System.Data.SqlClient
Imports System.Threading

Namespace Classes
    Public Class clsIM
#Region " Member Variables "
        Private strUser As String
        Private strErr As String
        Private isHidden As Boolean = False

        Private con As SqlConnection

        Private m_clsThread As Thread
        Private m_clsSynchronizingObject As System.ComponentModel.ISynchronizeInvoke
        Private m_clsNotifyDelegate As NotifyProgress

        Public Delegate Sub NotifyProgress(ByVal Message As String)
#End Region

#Region " Constructors "
        Public Sub New(ByVal UserName As String)
            'The username of the current user
            strUser = UserName
        End Sub

        Public Sub New(ByVal UserName As String, ByVal SynchronizingObject As System.ComponentModel.ISynchronizeInvoke, ByVal NotifyDelegate As NotifyProgress)
            'The username of the current user
            strUser = UserName
            m_clsSynchronizingObject = SynchronizingObject
            m_clsNotifyDelegate = NotifyDelegate
        End Sub
#End Region

#Region " Public Methods "
        Public Sub Start()
            m_clsThread = New Thread(AddressOf PollMessages)
            m_clsThread.Name = "Instant Messages"
            m_clsThread.IsBackground = True
            m_clsThread.Start()
        End Sub

        Public Function SendMessage(ByVal ToUser As String, ByVal DateTime As String, ByVal Message As String) As Boolean
            Dim SQL As String = ""
            Dim arrToUser() As String = Nothing
            Dim i As Integer = 0
            con = New SqlConnection(ConStr)
            Dim com As SqlCommand
            Dim dr As SqlDataReader
            Try
                If ToUser = "allUser" Then
                    con.Open()
                    com = New SqlCommand("SELECT username FROM WhoOnline", con)
                    dr = com.ExecuteReader(CommandBehavior.CloseConnection)
                    If dr.HasRows Then
                        While dr.Read
                            ReDim Preserve arrToUser(i)
                            If Not IsDBNull(dr.Item("username")) Then arrToUser(i) = dr.Item("username")
                            i += 1
                        End While
                    End If
                    con.Close()
                    con.Open()
                    For j As Integer = 0 To arrToUser.Length - 1
                        SQL = "INSERT INTO tblInstantMessage ([From],[To],Message,Sent) VALUES ('Admin','" & arrToUser(j) & "','" & Message & "','" & DateTime & "')"
                        com = New SqlCommand(SQL, con)
                        If com.ExecuteNonQuery = 0 Then
                            strErr = "Unable to send message to user: " & arrToUser(j) & ". Cancelling. Try again later."
                            con.Close()
                            Return False
                        End If
                    Next
                    con.Close()
                Else
                    con.Open()
                    SQL = "INSERT INTO tblInstantMessage ([From],[To],Message,Sent) VALUES ('" & strUser & "','" & ToUser & "','" & Message & "','" & DateTime & "')"
                    com = New SqlCommand(SQL, con)
                    If com.ExecuteNonQuery = 0 Then
                        strErr = "Unable to send message. Try again later."
                        con.Close()
                        Return False
                    End If
                    con.Close()
                End If
            Catch ex As Exception
                If con.State = ConnectionState.Open Then
                    con.Close()
                End If
                MessageBox.Show("An error occured while sending the message." & vbCrLf & "Error Message: " & ex.Message, "Database error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Return False
            End Try

            Return True
        End Function

        Public Function SendMessage(ByVal ToUsers() As String, ByVal FromUser As String, ByVal DateTime As String, ByVal Message As String) As Boolean
            Dim SQL As String = ""
            Dim arrToUser() As String = Nothing
            Dim i As Integer = 0
            con = New SqlConnection(ConStr)
            Dim com As SqlCommand
            Dim dr As SqlDataReader
            Try
                con.Open()
                com = New SqlCommand("SELECT username FROM WhoOnline", con)
                dr = com.ExecuteReader(CommandBehavior.CloseConnection)
                If dr.HasRows Then
                    While dr.Read
                        If Not IsDBNull(dr.Item("username")) AndAlso Array.IndexOf(ToUsers, dr.Item("username").ToString.ToUpper, 0) > -1 Then
                            ReDim Preserve arrToUser(i)
                            arrToUser(i) = dr.Item("username")
                            i += 1
                        End If
                    End While
                End If
                con.Close()

                con.Open()
                For j As Integer = 0 To arrToUser.Length - 1
                    SQL = "INSERT INTO tblInstantMessage ([From],[To],Message,Sent) VALUES ('" & FromUser & "','" & arrToUser(j) & "','" & Message & "','" & DateTime & "')"
                    com = New SqlCommand(SQL, con)
                    If com.ExecuteNonQuery = 0 Then
                        strErr = "Unable to send message to user: " & arrToUser(j) & ". Cancelling. Try again later."
                        con.Close()
                        Return False
                    End If
                Next
                con.Close()
            Catch ex As Exception
                If con.State = ConnectionState.Open Then
                    con.Close()
                End If
                MessageBox.Show("An error occured while sending the message." & vbCrLf & "Error Message: " & ex.Message, "Database error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Return False
            End Try

            Return True
        End Function

        ''' <summary>
        ''' Method for killing the thread
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub KillIM()
            If m_clsThread.IsAlive = True Then
                m_clsThread.Abort()
            End If
            If con.State = ConnectionState.Open Then
                con.Close()
            End If
        End Sub
#End Region

#Region " Private Methods "
        ''' <summary>
        ''' Check for new messages
        ''' </summary>
        ''' <remarks></remarks>
        Private Sub PollMessages()
            While m_clsThread.ThreadState = ThreadState.Background
                If Not m_clsThread.ThreadState = ThreadState.WaitSleepJoin Then
                    RetrieveMessage()
                End If
            End While
        End Sub

        ''' <summary>
        ''' Fetch new messages
        ''' </summary>
        ''' <remarks></remarks>
        Private Sub RetrieveMessage()
            If strUser = "" Then Exit Sub
            Dim strSent As String = ""
            Dim dr As SqlDataReader
            con = New SqlConnection(ConStr)
            Try
                con.Open()
                Dim com As SqlCommand = New SqlCommand("SELECT [From],[To],Message,Sent FROM tblInstantMessage WHERE [To] = '" & strUser & "'", con)
                dr = com.ExecuteReader(CommandBehavior.CloseConnection)
                If dr.HasRows Then
                    While dr.Read
                        If Not IsDBNull(dr.Item("Message")) Then
                            NotifyUI(dr.Item("From") & "¤" & dr.Item("Message") & "¤" & dr.Item("Sent"))
                        End If
                    End While
                    If isHidden = False Then
                        RemoveMessages(strUser)
                    End If
                End If
                dr.Close()
            Catch ex As Exception
            End Try
            Thread.Sleep(5000)
            'm_clsThread.Sleep(5000)
        End Sub

        ''' <summary>
        ''' Remove fetched messages
        ''' </summary>
        ''' <param name="ToUser">Recipient</param>
        Private Sub RemoveMessages(ByVal ToUser As String)
            Try
                Dim con As SqlConnection = New SqlConnection(ConStr)
                con.Open()
                Dim com As SqlCommand = New SqlCommand("DELETE FROM tblInstantMessage WHERE [To] = '" & ToUser & "'", con)
                com.ExecuteNonQuery()
                con.Close()
            Catch ex As Exception
            End Try
        End Sub

        ''' <summary>
        ''' Report back to main thread
        ''' </summary>
        ''' <param name="Message">Message to send to main thread</param>
        Private Sub NotifyUI(ByVal Message As String)
            If Not m_clsNotifyDelegate Is Nothing Then
                Dim args(0) As Object
                args(0) = Message
                m_clsSynchronizingObject.Invoke(m_clsNotifyDelegate, args)
            End If
        End Sub
#End Region

#Region " Properties "
        Public Property FormIsHidden() As Boolean
            Get
                Return isHidden
            End Get
            Set(ByVal value As Boolean)
                isHidden = value
            End Set
        End Property

        Public ReadOnly Property IsAlive() As Boolean
            Get
                If m_clsThread.IsAlive = True Then
                    Return True
                Else
                    Return False
                End If
            End Get
        End Property
#End Region
    End Class
End Namespace

And here's how to use it.
On the main startup form, declare an instance of the class as Public and simply call the Start method.
Also add a method for displaying the message.

Public IM As clsIM

Private Sub Start_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    IM = New clsIM(userid, Me, New clsIM.NotifyProgress(AddressOf PrintProgress))

    If IM IsNot Nothing Then
        IM.FormIsHidden = False
        IM.Start()
    End If
End Sub

Private Sub PrintProgress(ByVal Message As String)
    Dim strMessage() As String = Message.Split("¤")
    Dim con As SqlConnection
    Dim com As SqlCommand
    Dim dr As SqlDataReader

    If IM.FormIsHidden = False Then
        MessageBox.Show("Sent: " & strMessage(2) & vbCrLf & vbCrLf & strMessage(1), "Message from: " & strMessage(0), MessageBoxButtons.OK, MessageBoxIcon.None)
    End If
End Sub

And from whatever form you intend to send the message:

IM.SendMessage("to user", "date and time", "message")
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.