jabber.net not working for vb.net - asp.net

i am trying to use jabber.net for web application
i know this is for desktop application as given in below given link
http://www.codeproject.com/Articles/34300/Google-Chat-Desktop-Application-using-Jabber-Net
but i found a post on stack overflow related to this and it say that one dude has implemented it with web application
Web Chat Application - ASP.NET/Jabber/Ajax/WCF/Comet/ReverseAjax - Issues Faced - Seeking Insights
actually the code project code was written in c#
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using jabber.client;
using System.Threading;
using jabber.protocol.iq;
using jabber;
using Google.GData.Contacts;
using Google.GData.Extensions;
using jabber.protocol;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
static ManualResetEvent done = new ManualResetEvent(false);
private jabber.client.JabberClient jabberClient1=new jabber.client.JabberClient();
protected void Page_Load(object sender, EventArgs e)
{
jabberClient1.OnMessage += new MessageHandler(jabberClient1_OnMessage);
jabberClient1.OnDisconnect += new bedrock.ObjectHandler(jabberClient1_OnDisconnect);
jabberClient1.OnError += new bedrock.ExceptionHandler(jabberClient1_OnError);
jabberClient1.OnAuthError += new jabber.protocol.ProtocolHandler(jabberClient1_OnAuthError);
jabberClient1.User = "sa";
jabberClient1.Server = "gmail.com";
jabberClient1.Password = "download";
jabberClient1.Connect();
jabberClient1.OnAuthenticate += new bedrock.ObjectHandler(jabberClient1_OnAuthenticate);
}
void jabberClient1_OnAuthenticate(object sender)
{
done.Set();
}
void jabberClient1_OnAuthError(object sender, System.Xml.XmlElement rp)
{
if (rp.Name == "failure")
{
Response.Write("Invalid User Name or Password");
}
}
void jabberClient1_OnError(object sender, Exception ex)
{
Response.Write(ex.Message);
}
void jabberClient1_OnDisconnect(object sender)
{
Response.Write("Disconnected");
}
private void jabberClient1_OnMessage(object sender, jabber.protocol.client.Message msg)
{
Response.Write("Message Posted");
//frmChat[(int)chatIndex[msg.From.Bare]].ReceiveFlag = true;
//string receivedMsg = msg.From.User + " Says : " + msg.Body + "\n";
//frmChat[(int)chatIndex[msg.From.Bare]].AppendConversation(receivedMsg);
//frmChat[(int)chatIndex[msg.From.Bare]].Show();
}
}
}
so i converted it to vb.net like this
Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports jabber.client
Imports System.Threading
Imports jabber.protocol.iq
Imports jabber
Imports Google.GData.Contacts
Imports Google.GData.Extensions
Imports jabber.protocol
Public Class GtalkIntegration
Inherits System.Web.UI.Page
Shared done As New ManualResetEvent(False)
Private WithEvents jabberClient1 As New jabber.client.JabberClient()
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
AddHandler jabberClient1.OnMessage, AddressOf jabberClient1_OnMessage
AddHandler jabberClient1.OnDisconnect, AddressOf jabberClient1_OnDisconnect
AddHandler jabberClient1.OnError, AddressOf jabberClient1_OnError
AddHandler jabberClient1.OnAuthError, AddressOf jabberClient1_OnAuthError
jabberClient1.User = "sa"
jabberClient1.Server = "gmail.com"
jabberClient1.Password = "download"
jabberClient1.Connect()
AddHandler jabberClient1.OnAuthenticate, AddressOf jabberClient1_OnAuthenticate
End Sub
Private Sub jabberClient1_OnAuthenticate(ByVal sender As Object)
done.[Set]()
End Sub
Private Sub jabberClient1_OnAuthError(ByVal sender As Object, ByVal rp As System.Xml.XmlElement)
If rp.Name = "failure" Then
Response.Write("Invalid User Name or Password")
End If
End Sub
Private Sub jabberClient1_OnError(ByVal sender As Object, ByVal ex As Exception)
Response.Write(ex.Message)
End Sub
Private Sub jabberClient1_OnDisconnect(ByVal sender As Object)
Response.Write("Disconnected")
End Sub
Private Sub jabberClient1_OnMessage(ByVal sender As Object, ByVal msg As jabber.protocol.client.Message)
Response.Write("Message Posted")
'frmChat[(int)chatIndex[msg.From.Bare]].ReceiveFlag = true;
'string receivedMsg = msg.From.User + " Says : " + msg.Body + "\n";
'frmChat[(int)chatIndex[msg.From.Bare]].AppendConversation(receivedMsg);
'frmChat[(int)chatIndex[msg.From.Bare]].Show();
End Sub
End Class
but it is giving me error like
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
please help me out guys thanks in advance

Make sure to reference jabber-net.dll, zlib.net.dll, and netlib.Dns.dll.

Related

ASP.NET WebForms - VB.NET and SignalR

This is my Hub code (very simple):
Imports System
Imports System.Web
Imports Microsoft.AspNet.SignalR
Imports Microsoft.AspNet.SignalR.Hubs
Imports Microsoft.AspNet.SignalR.Client
Imports Microsoft.AspNet.SignalR.Messaging
Imports System.Threading.Tasks
Namespace SignalRChat
Public Class ChatHub
Inherits Hub
Public Sub Send(userName As String, message As String)
Clients.All.broadcastMessage(userName, message)
End Sub
End Class
End Namespace
This is my Aspx page code:
Imports System.Web.UI.WebControls
Imports Microsoft.AspNet.SignalR.Client
Imports System.Threading.Tasks
Public Class WebForm9
Inherits System.Web.UI.Page
Public Shared hubConnection As HubConnection
Public Shared chatHubProxy As IHubProxy
Public Sub MyChat_init(sender As Object, e As EventArgs) Handles Me.Init
If IsPostBack = False Then
hubConnection = New HubConnection("https://localhost:44343/")
hubConnection.TraceLevel = TraceLevels.All
hubConnection.TraceWriter = Console.Out
chatHubProxy = hubConnection.CreateHubProxy("ChatHub")
hubConnection.Start().Wait()
End If
chatHubProxy.On(Of String, String)("broadcastMessage", Sub(ByVal userName As String, ByVal message As String)
Dim li As ListItem = New ListItem
li.Value = userName & " - " & message
li.Text = userName & " - " & message
ListBox1.Items.Add(li)
End Sub)
End Sub
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
chatHubProxy.Invoke("Send", "Io", "Messaggio")
End Sub
End Class
I made a lot of tries but always ended up with no result... I added the postback checking because I noticed I was having the connection to the hub starting and starting again on each button_click...
By the way, if I add in the same project a page with JScript code I can catch all the messages sent on the JScript code, but none of the messages sent from the html page is catched by the aspx codebehind...
It's really strange because if I take away the listbox.items.add method and I put a "MsgBox" instead, then it fires up and work... but I have found no way to manage the "messages" from my codebehind and so update controls on my page... Maybe it's a connection mistake? Did anyone of you has any experience with SignalR and WebForms with VB.NET codebehind?
If this helps, I have a working client code (for testing purposes) in VB.NET WinForms app. (My Hub is in C#):
Hub (ASP.NET Core in net5.0 - created using Gerald Versluis' tutorial: https://www.youtube.com/watch?v=pDr0Hx67guk):
using Microsoft.AspNetCore.SignalR;
using System;
using System.Threading.Tasks;
namespace SignalR.Hubs;
public class OneHub : Hub
{
public async Task SendMessage(Message message)
{
Console.WriteLine($"{message.SentDateTime} Sender : {message.SenderId} - {message.MessageText}");
await Clients.All.SendAsync("MessageReceived", message);
}
}
WinForms (net4.8):
Imports Microsoft.AspNetCore.SignalR.Client
Imports SignalRWinForms.Client.Messaging.Models
Public Class Form1
Private connection As HubConnection
Sub New()
InitializeComponent()
connection = New HubConnectionBuilder().WithUrl("http://192.168.1.230:5296/chat").Build()
connection.On(Of Messages)("MessageReceived", Sub(Messages)
Invoke(Sub()
ReceiveMessage(Messages)
End Sub)
End Sub)
connection.StartAsync()
End Sub
Private Sub ReceiveMessage(msg As Messages)
chatMessages.Text &= $"{Environment.NewLine}{msg.MessageText}"
End Sub
Private Async Sub btnSendMessage_Click(sender As Object, e As EventArgs) Handles btnSendMessage.Click
Dim message = New Messages With {
.MessageText = txtMessage.Text,
.SenderId = 1111,
.ReceiverId = 2222,
.Token = "token",
.SentDateTime = DateTime.Now
}
Await connection.InvokeCoreAsync("SendMessage", args:={message})
txtMessage.Text = String.Empty
End Sub
End Class
Form1 is very simple - chatMessages label to populate messages, txtMessage textbox to write message and a btnSendMessage button.
Messages Class (common for both projects)
Public Class Messages
Public Property SenderId As Integer
Public Property ReceiverId As Integer
Public Property MessageText As String
Public Property Token As String
Public Property SentDateTime As DateTime
End Class

Google reCaptcha V2 Implementation VB.net

Having a hard time getting reCaptcha to validate on my site :(
I have tried to find other sources for VB.net implementations, but haven't had much luck. Here is what I have tried...
default.aspx.vb
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data
Imports System.Net
Imports System.Text
Imports System.IO
Imports System.Web.Script.Serialization
Public Class _Default
Inherits System.Web.UI.Page
Sub reCaptcha_Click(ByVal sender As Object, ByVal e As EventArgs)
If (capValidate()) Then
MsgBox("Valid Recaptcha")
Else
MsgBox("Not Valid Recaptcha")
End If
End Sub
Public Function capValidate() As Boolean
Dim Response As String = Request("g-captcha-response")
Dim Valid As Boolean = False
Dim req As HttpWebRequest = DirectCast(WebRequest.Create(Convert.ToString("https://www.google.com/recaptcha/api/siteverify?secret=THIS IS WHERE MY KEY IS&response=") & Response), HttpWebRequest)
Try
Using wResponse As WebResponse = req.GetResponse()
Using readStream As New StreamReader(wResponse.GetResponseStream())
Dim jsonResponse As String = readStream.ReadToEnd()
Dim js As New JavaScriptSerializer()
Dim data As MyObject = js.Deserialize(Of MyObject)(jsonResponse)
Valid = Convert.ToBoolean(data.success)
Return Valid
End Using
End Using
Catch ex As Exception
Return False
End Try
End Function
Public Class MyObject
Public Property success() As String
Get
Return m_success
End Get
Set(value As String)
m_success = Value
End Set
End Property
Private m_success As String
End Class
And my front page...
<div class="g-recaptcha"
data-sitekey="THIS IS WHERE MY SITE KEY IS"></div>
<asp:Button ID="btnLogin" CssClass="captcha_click" runat="server" Text="Check Recaptcha" OnClick="reCaptcha_Click" TabIndex ="4"/>
My message boxes always return "not a valid recaptcha"
Can anyone shed some light on why I cannot get a valid recaptcha return?
Thanks!
Try:
Dim Response As String = Request("g-recaptcha-response")
Note the re

request querystring in variable

hello iam trying to get the id from a url and send it to the clint side this is what i did
this is my url :
http://localhost:53010/edit.aspx?Id=4
code behind
Public Partial Class Edit
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
End Sub
Private _myId As String = Request.QueryString("id")
Public Property myId() As String
Get
Return _myId
End Get
Set(ByVal value As String)
_myId = value
End Set
End Property
End Class
client
<%= myId%>
error
Request is not available in this context
this is also what i get when i move the private prop to page_load()
"private " is not valid on local variable declaration –
any idea what is going on
Thanks
i solve this problem here is the answer
Public Partial Class Edit
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
MyIdVal = Request.QueryString("id")
End Sub
Private _myIdVal As String
Public Property MyIdVal() As String
Get
Return _myIdVal
End Get
Set(ByVal value As String)
_myIdVal = value
End Set
End Property
End Class
That's a field initializer.
Field initializers run before the constructor and cannot access the instance they're initializing.
Therefore, you can't use the Request property there.
You need to move that to the constructor or Page_Load.
You're accessing the Request too early.
It will work if you set myId on Init, Page_Load or any other similar page event.
Try to set _myId in your PageLoad.
So I wanted a class with properties that were set from querystrings and found this thread. I also wanted to be able to access properties on the front page and even in JavaScript from a single location. Here is what I came up with:
// App_Code/QueryStrings.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for QueryStrings
/// </summary>
public class QS
{
private int id = -1;
public QS()
{
if (HttpContext.Current.Request.QueryString["id"] != null)
try
{
Int32.TryParse(HttpContext.Current.Request.QueryString["id"], out id);
}
catch
{
id = -2;
}
else
id = -3;
}
public int ID
{
get
{
return id;
}
}
}
Then you can call it from your .aspx page as follows:
<body>
<form id="form1" runat="server">
<div>
<% QS qs = new QS(); %>
ID = <%= qs.ID %>
</div>
</form>
</body>
Of course you can call from code behind with the same syntax.

RiseEvent from usercontrol or communicate between usercontrols

I have problem with bubbleup event from my dynamic loaded ascx control (katalogbooklist.ascx) to it´s parent control (ViewAJBarnboksKatalog.ascx)
i need to fire/run sub uppdateraAndraModuler in the parent control when the event addMultiVotes_command fires in the chiled control.
is there any one who knows or have an idea on how to do this?
/
Andreas
(the code lives in a dotnetnuke cms modules, if that's any help)
Partial Class ViewAJBarnboksKatalog '<--------Partial codebehind file for ViewAJBarnboksKatalog.ascx
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
.. code for adding for loading katalogbooklist.ascx dynamic...
Me.Controls.Add(..code.. add katalogbooklist.ascx ..) '
End Sub
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim t As Execkoden = New Execkoden()
AddHandler t.onAjeventtest, AddressOf Uppdateramod
End Sub
Public Sub Uppdateramod(sender As Object, e As ajeventArgs)
uppdateraAndraModuler()
End Sub
Public Sub uppdateraAndraModuler()
'..do some code
End Sub
End Class
Partial Class katalogenBookList '<--------Partial codebehind file for katalogbooklist.ascx
Protected Sub addMultiVotes_command(ByVal sender As Object, ByVal e As System.EventArgs)
'..more code...
Dim te As New Execkoden ' <----- i want to use the constructor to raise the event in class Execkoden can´t raiseevent directly it wont´t fire
'... more code...
End sub
End Class
Public Class ajeventArgs : Inherits EventArgs
Public Sub New()
End Sub
End Class
Public Delegate Sub Uppdatera(sender As Object, e As ajeventArgs)
Public Class Execkoden
Public Event onAjeventtest As Uppdatera
Public Sub New()
RaiseEvent onAjeventtest(Me, New ajeventArgs)
End Sub
End Class
Create an event handler in the child control, like this:
public event EventHandler DeleteButtonClick;
When a button is clicked in the child control, do this:
protected void DeleteClick(object sender, EventArgs e)
{
if (this.DeleteButtonClick != null)
this.DeleteButtonClick(sender, e);
}
And in your parent control, in the markup:
<UC:SomeUserControl ID="UserControl1" runat="server" OnDeleteButtonClick="UserControl1_DeleteClick" ...>
And in the code behind of the parent control:
protected void UserControl1_DeleteClick(object sender, EventArgs e)
{
//do something
}
I would suggest using the IMC or Inter Module Communication feature that's built in to DotNetNuke.

Add an "Export to Excel" button to a webpage to export gridview to excel in webapplication

i built a patient management software for a clinic and i need to export patiet list from ASP.net grid view to excel file
my question is:
Is there a way to export gridview to excel
i am using vb.net and visual web developer 2010
i store datasource from advanced search page into a session and redirect to result page
here is the code of result page
Partial Class Sresults
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
GridView1.DataSource = Session("dsource")
GridView1.DataBind()
End Sub
Protected Sub Backbtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Backbtn.Click
Session("dsource") = ""
Response.Redirect("searchme.aspx")
End Sub
Protected Sub Page_PreRenderComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRenderComplete
Response.Write(GridView1.Rows.Count.ToString + " Records")
End Sub
End Class
try below code on the button click
// Get DataTable that DataGrid is bound to.
var dataTable = (DataTable)dataGrid.DataSource;
// Create new ExcelFile.
var ef = new ExcelFile();
// Add new worksheet to the file.
var ws = ef.Worksheets.Add(dataTable.TableName);
// Insert the data from DataTable to the worksheet starting at cell "A1".
ws.InsertDataTable(dataTable, "A1", true);
// Stream file to browser.
Response.Clear();
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment; filename=Employee.xls");
ef.SaveXls(Response.OutputStream);
Response.End();
first you have to add the following to the page directive to avoid runtime error
EnableEventValidation ="false"
add gridview to aspx page
the session "dsource" is passing the datasource from advanced search page containing the connection string and select command
then here is the code behind
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Threading;
using System.IO;
using System.Reflection;
public partial class csresults : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
gridview1.DataSource = Session["dsource"];
gridview1.DataBind();
}
public override void VerifyRenderingInServerForm(Control control)
{
}
protected void Button2_Click(object sender, EventArgs e)
{
HtmlForm form = new HtmlForm();
string attachment = "attachment; filename=Patients.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter stw = new StringWriter();
HtmlTextWriter htextw = new HtmlTextWriter(stw);
form.Controls.Add(gridview1);
this.Controls.Add(form);
form.RenderControl(htextw);
Response.Write(stw.ToString());
Response.End();
}
}

Resources