Jabber-net Send message (VB.NET) - asp.net

I have created test project to send message via google talk using Jabber library. As I already have test project that can send message successfully using agsXMPP, I want to imitate this project to use jabber library instead. However, there is no message sent even though the code run pass sending message command without any error. It seems that the password has not even been checked as it didn't enter OnAuthError event.
My test project is ASP.NET Web application project using VB.NET language. There are 4 textboxes to fill in: sender account (txt_Sender), sender's password (txt_Password), message to be sent (txt_Message) and receiver account (txt_Receiver) and also 1 button for sending the message (btn_Send). I test by using my email account (xxx1#gmail.com) and send message to my friend (xxx2#gmail.com). Here are my VB code
Imports jabber
Imports jabber.client
Imports Microsoft.Win32
Imports System.Threading
Imports jabber.protocol.client
Imports jabber.connection
Public Class TestSendMsg
Inherits System.Web.UI.Page
Public done As ManualResetEvent
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
done = New ManualResetEvent(False)
End Sub
Private Sub btn_Send_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_Send.Click
Dim jcSender As JabberClient = New JabberClient()
Dim jidSender As New jabber.JID(txt_Sender.Text.Trim)
With jcSender
.User = jidSender.User
.Password = txt_Password.Text.Trim
.Server = jidSender.Server
.AutoReconnect = True
.AutoRoster = True
End With
With jcSender
Try
AddHandler .OnAuthenticate, New bedrock.ObjectHandler(AddressOf j_OnAuthenticate)
'AddHandler .OnAuthenticate, AddressOf j_OnAuthenticate
AddHandler .OnPresence, AddressOf j_OnPresence
AddHandler .OnBeforePresenceOut, AddressOf j_OnBeforePresenceOut
AddHandler .OnAuthError, AddressOf j_OnAuthError
AddHandler .OnAfterPresenceOut, AddressOf j_OnAfterPresenceOut
.Connect()
.Login()
.IsAuthenticated = True
.Message(txt_Reciever.Text.Trim, txt_Message.Text.Trim)
Catch ex As Exception
MsgBox(ex.Message)
End Try
.Close()
.Dispose()
End With
End Sub
Private Sub j_OnAfterPresenceOut(ByVal sender As Object, ByVal pres As Presence)
'Dim j As JabberClient = CType(sender, JabberClient)
'j.Message(TARGET, "Registered: " & iq.BaseURI)
'done.Set()
End Sub
Private Sub j_OnAuthError(ByVal sender As Object, ByVal pres As Presence)
'Dim j As JabberClient = CType(sender, JabberClient)
'j.Message(TARGET, "Registered: " & iq.BaseURI)
'done.Set()
End Sub
Private Sub j_OnBeforePresenceOut(ByVal sender As Object, ByVal pres As Presence)
'Dim j As JabberClient = CType(sender, JabberClient)
'j.Message(TARGET, "Registered: " & iq.BaseURI)
'done.Set()
End Sub
Private Sub j_OnPresence(ByVal sender As Object, ByVal pres As Presence)
'Dim j As JabberClient = CType(sender, JabberClient)
'j.Message(TARGET, "Presence: " & pres.BaseURI)
'done.Set()
End Sub
Private Sub j_OnAuthenticate(ByVal sender As Object)
' Sender is always the JabberClient.
Dim j As JabberClient = CType(sender, JabberClient)
j.Message(txt_Reciever.Text.Trim, "Test OnAuthenticate")
' Finished sending. Shut down.
done.Set()
End Sub
End Class

You need to wait for OnAuthenticate before sending your message.

Related

Asp.net Vb.net , access control concurrency

Hello guys I am trying to make restriction on my WebApp that runs on VB.net with framework Asp.net. In another words, how can I Control the permitted number of sessions.
The WebApp works perfectly and now I want to add "How many users can access the WebApp" if Number of users "Sessions" exceeded the new coming users will be redirected to a site.
I have been working on this for few weeks... tried Cookies and Seasons and neither is working
Scenario: Maximum users can access the WebApp is 1
when I launch the second Browser and start new session the UserNumber will be incremented by 1 hence will be redirected to "ServerBusy" page...
However, If the second Browser opened new tab and accessed the WebApp that will bypass the Condition at Session_Start and will access the WebApp .
Thank you.
here is the Code
Imports System.Web
Public Class Global_asax
Inherits System.Web.HttpApplication
Public Shared maxUsersAllowance As Integer = 1 'Total number of users can enter the server
Public Shared UserNumber As Integer = 0 ' the current number of users
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the application is started
End Sub
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
UserNumber = UserNumber + 1
Dim myCookie As New HttpCookie("UserNumber")
Dim myCookie2 As New HttpCookie("Access")
myCookie.Value = UserNumber
myCookie2.Value = True
Response.Cookies.Add(myCookie)
Response.Cookies.Add(myCookie2)
If (Request.Cookies("UserNumber") IsNot Nothing) Then
If (Request.Cookies("UserNumber").Value > 1) Then
If (Request.Cookies("Access").Value) Then
Response.Redirect("~/ServerBusy.aspx")
End If
End If
End If
End Sub
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Sub Application_Init(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Sub Application_Dispose(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Sub Application_Unload(ByVal sender As Object, ByVal e As EventArgs)
End Sub
End Class
After Reading Article https://stackoverflow.com/a/6218525/1260204 and Updating the code, the Issue still exist which is that the User Can get into the WebApp if he open new Tab on the browser and connected
Imports System.Web
Public Class Global_asax
Inherits System.Web.HttpApplication
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the application is started
Application("ActiveSessions") = 0
End Sub
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
Try
Application.Lock()
Dim activeSessions As Integer = CInt(Application("ActiveSessions")) + 1
Dim allowedSessions As Integer = 1
' retrieve the threshold here instead
Application("ActiveSessions") = activeSessions
If activeSessions > allowedSessions Then
System.Web.HttpContext.Current.Response.Redirect("~/ServerBusy.aspx", False)
End If
Finally
Application.UnLock()
End Try
End Sub
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
Dim LiveSessionsCount As Integer = CInt(Application("LiveSessionsCount"))
End Sub
Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
Application.Lock()
Application("ActiveSessions") = CInt(Application("ActiveSessions")) - 1
Application.UnLock()
End Sub
End Class
Update, I have solved the Issue with the Tabs. by adding
Session.Abandon()
In my redirected page. However that is half solution as far as i can get to... the other half is , if the user closes his browser by the X , I do need to wait 20 minutes until the session ends ... is there a away to terminate the session once the user exit/kill the page?

How do i give a value a TextBox create dynamically when i click and doubleClick? vb.net

I have text box create like that:
Dim Result1 As New TextBox
Result1.ID = "BOX_Result" & a & "_" & i
I want when i click on that textbox to write "OK" and when i double click in cell to put NOT/OK
Important! The TextBox is created Dynamically if i try Result.Click doesn't work, get ne that error: "Result1.Click display error: "Click is not an event of 'System.Web.UI.WebControls.TextBox' "
I try like that but doesn't work:
AddHandler Result1.Click, AddressOf Me.Result1_Click
Private Sub Result1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs)
Result1.Text = "OK"
End Sub
I want when a person click on that textbox created dynamically but doesn't work click. Thanks for help
You can add these lines to your TextBox definition:
Result1.Attributes.Add("onclick", "this.value = 'OK';")
Result1.Attributes.Add("ondblclick", "this.value = 'NOT/OK';")
In this code, the text "NOT/OK" is displayed when the user double-clicks in the TextBox. In your question, you talk about a double-click "in cell". If that "cell" is not the TextBox, please give some indication of what kind of control it is.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim tb As New TextBox 'Create the new TextBox
AddHandler tb.DoubleClick, AddressOf TB_DoubleClick 'Add a handler to the textbox`s DoubleClick event
AddHandler tb.Click, AddressOf TB_Click
'Set any other properties of textbox you want here....
Me.Controls.Add(tb) 'Add the textbox to the forms controls
End Sub
'This is the textbox Click event handler sub
Private Sub TB_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim tb As TextBox = DirectCast(sender, TextBox) 'Cast the (sender) into a textbox to get access to the textbox`s properties
Result1.Text = "OK"
End Sub
'This is the textbox DoubleClick event handler sub
Private Sub TB_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs)
Dim tb As TextBox = DirectCast(sender, TextBox) 'Cast the (sender) into a textbox to get access to the textbox`s properties
Result1.Text = "NOT OK"
End Sub
I have create a simple exemple for you :
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Result1 As New TextBox
Result1.Text = "BOX_Result"
Dim loc As New Point With {.Y = 117, .X = 111}
Result1.Location = loc
Me.Controls.Add(Result1)
AddHandler Result1.Click, AddressOf Me.Result1_Click
End Sub
Private Sub Result1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim txt As TextBox = sender
sender.Text = "OK"
End Sub
End Class
Hope that you want

Adding isbn and book title to Access DB in vb.net webform

I am currently working on a online bookstore for a class using vb.net. I am having trouble adding the isbn and book title to my database. I am able to add author name and email fine, but when I try to do the same I get "duplicate values" error. Here is my source code for adding the book to the database.
Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient
Public Class bookAdded
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If (Request.QueryString("type") = "delete") Then
result.Text = "The author " & Request.QueryString("authorName") & " has been deleted."
Else
result.Text = "The author " & Request.QueryString("authorName") & " has been added."
If (Not Page.IsPostBack) Then
Dim conn As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OleDb.12.0;Data Source=" + Server.MapPath("~/bookstore.accdb"))
conn.Open()
Dim sql As String = "INSERT INTO books(ISBN, title) VALUES(#isbn, #title)"
Dim cmd As New OleDb.OleDbCommand(sql, conn)
cmd.Parameters.AddWithValue("#isbn", Request.QueryString("isbn"))
cmd.Parameters.AddWithValue("#title", Request.QueryString("title"))
cmd.ExecuteNonQuery()
cmd.Dispose()
conn.Close()
End If
End If
End Sub
Protected Sub back_Click(sender As Object, e As EventArgs) Handles back.Click
Response.Redirect("NextPage.aspx")
End Sub
Protected Sub search_Click(sender As Object, e As EventArgs) Handles search.Click
Response.Redirect("search.aspx")
End Sub
End Class
Your help is much appreciated. Thank you!
P.S: ISBN is a integer and title is a string

How to code registration in MS Access VB.net

I am cluesless for coding in my registration page written in aspx.vb.
The page allows the student to register and the information will be kept in the MS Access database. The clear button is to clear all the textboxes and droplists
Username textbox
student id textbox
course (dropdownlist) needs to be connected to dtb.
Submit and clear button
Code written so far:
Imports System
Imports System.Data
Imports System.Data.OleDb
Public Class WebForm5
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub Submit_Click(sender As Object, e As EventArgs) Handles Submit.Click
Dim con As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\DELL-PC\Desktop\OnlineDB.mdb")
End Sub
Protected Sub Clear_Click(sender As Object, e As EventArgs) Handles Clear.Click
TextBox1.Clear()
TextBox2.Clear()
End Sub
End Class
My friend, you need to look into .ExecuteNonQuery and .ExecuteReader objects of the Class OleDB.
It's here you can execute SQL queries to your access database.
an example:
Public Sub ACNonquery(ByVal utos As String)
Dim Accessconn as new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\DELL-PC\Desktop\OnlineDB.mdb")
Dim sqlcommand As New OleDbCommand(utos, accessconn)
Try
accessconn.Open()
sqlcommand.ExecuteNonQuery()
accessconn.Close()
Catch ex As Exception
accessconn.Close()
MsgBox(utos & vbNewLine & vbNewLine & ex.ToString)
End Try
End Sub
call it like this:
ACNonquery("insert into tablename values('data1','data2');")
In this example, I used the NonQuery to execute an SQL command (in this case, an insert) to my access database.

using session state dont work properly

i am still new to using session state, i want to convert page name into and integer according to a database table
a function then compares "X" and "Y" to check if a user have the right to view this page
i know this is not the best way of managing website security, but it is like "training on how to use the session"
what have i done wrong
Partial Class advancedsearch
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
Label1.Text = Session("username").ToString
Label3.Text = Session("role").ToString
Label4.Text = System.IO.Path.GetFileName(Request.Url.ToString())
Catch ex As Exception
Response.Redirect("login.aspx")
End Try
If Label1.Text = "" Then
Response.Redirect("login.aspx")
End If
Dim x As Integer = Int32.Parse(Label3.Text)
Dim y As Integer = Int32.Parse(DropDownList1.SelectedItem.ToString)
If x < y Then Response.Redirect("login.aspx")
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Response.Redirect("default.aspx")
End Sub
End Class
try putting the comparison part in pre render complete
Protected Sub Page_PreRenderComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRenderComplete
Dim x As Integer = Int32.Parse(Label3.Text)
Dim y As Integer = Int32.Parse(DropDownList1.SelectedItem.ToString)
If x < y Then Response.Redirect("login.aspx")
End Sub

Resources