Use ASP.net 2010 B to Ping an IP Address - asp.net

I'm trying to use a simple Ping technique to ping an IP Address. I have added Imports System.Net.NetworkInformation.Ping to my web page and tried to simply create a button that once clicked ping an IP address.
The problem that I have is an error message that states 'System.Net.NetworkInformation' is a namespace and cannot be used as an expression.
I'm new to this and have tried to replace this with something else, but I still cannot seem to get this to work. Would it be possible for someone to review this and let me know where I'm going wrong. Im using VB and VS2010. This is my code:
Imports System.Text
Imports System.Net.NetworkInformation.Ping
Partial Class Ping
Inherits System.Web.UI.Page
Protected Sub btnPing_Click(sender As Object, e As System.EventArgs) Handles btnPing.Click
Using System.Net.NetworkInformation
Ping(Ping = New Ping())
PingReply(pingReply = Ping.send("xxx.xx.xxx.xx"))
Console.WriteLine("Address: {0}", pingReply.address)
Console.WriteLine("Status: {0}", pingReply.Status)
End Using
End Sub
End Class

You would be better off trying the following:
Imports System.Text
Imports System.Net.NetworkInformation
Partial Class Ping
Inherits System.Web.UI.Page
Protected Sub btnPing_Click(sender As Object, e As System.EventArgs) Handles btnPing.Click
Dim vPing As New Ping
Dim vPingReply As PingReply = vPing.Send("xxx.xx.xxx.xx")
Console.WriteLine("Address: {0}", vPingReply.Address)
Console.WriteLine("Status: {0}", vPingReply.Status)
End Sub
System.Net.NetworkInformation is just a namespace, it just serves as a container for other classes.

You've named your page class Ping, which is the same name as the .NET class you are instantiating.
Partial Class Ping
Inherits System.Web.UI.Page
Elsewhere in your code, you try to use the .NET Ping class.
I know that this can cause issues in certain project types.
Try naming your page PingPage instead *.
*As well as the other suggestions here about Namespaces etc.

Related

Simple HttpResponse in ASPX VB

We have an old Asp.net 2.0 web service where I need to put in a simple HTTP response. In the below code, the function "APIValidation()" returns an int, 200 or 404. All I need to do is have it send an HttpResponse so my node web app can read the statuscode (and then do what it needs to do).
I have no idea how to do this (I'm a noob with ASP), the tutorials I find are too elaborate, it seems this could be solved in a few lines of code, I just don't know which.
You can see it in action here:
200 : http://registration.imprintplus.com/imprinttest/GlobalSrvSN.aspx?sn=29820C0792024CDC8D590BF14AF42490
404: http://registration.imprintplus.com/imprinttest/GlobalSrvSN.aspx?sn=invalid
The other option is for Node to be able to extract the 200 or 404 out of what the ASP service gives me. Either or works for me.
Option Explicit Off
Option Strict Off
Imports ActivationServer
Partial Class GlobalSrvSN
Inherits System.Web.UI.Page
Public Function APIValidation(ByVal sn As String, ByVal databaseSource As String) As String
Dim act As New LogicProtect_ActivationServer(databaseSource)
Return act.APIValidation("user", "user#company.com", sn)
End Function
End Class
thanks a million!
Unsure how you're calling that function so will assume a few things if that's the only code you have.
If you're calling it from the .aspx file then this would be an example:
<%# Page Language="vb" ....." %>
<%
Dim foo = APIValidation(Request("sn"), "other string")
Response.StatusCode = CInt(foo)
%>
If from "code behind" ([page].aspx.vb):
Option Explicit Off
Option Strict Off
Imports ActivationServer
Partial Class GlobalSrvSN
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim foo = APIValidation(Request("sn"), "other string")
Response.StatusCode = CInt(foo)
End Sub
Public Function APIValidation(ByVal sn As String, ByVal databaseSource As String) As String
Dim act As New LogicProtect_ActivationServer(databaseSource)
Return act.APIValidation("user", "user#company.com", sn)
End Function
End Class
Notes: Above is a very trivial answer to keep things simple. But:
above is really a "Web Forms Page" (not technically a "web service") which would be asmx (2.0)
Skipped any input validation (queryString), error checking/handling.
Response object is how you'd control HTTP Responses (headers, etc.)
Hth

Why am I getting an weird error on my asp.net webpage?

I am currently developing a asp.net webpage and a WCF publish subscribe service. The WCF service has been done up the subscriber is a winform app which is also done up. I am now trying to get the asp.net webpage to connect to my publish service for my WCF. However there is an weird error that I am getting. I have added the app.config and the generatedProxy.cs.vb to my asp.net project.
This is the code for my class
Public Class json
Implements IPostingContractCallback
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Response.ContentType = "application/json"
If Request.QueryString("action") = "postAlert" Then
Dim site As InstanceContext = New InstanceContext(New json())
Dim client As PostingContractClient = New PostingContractClient(site)
client.PublishPost("testing")
client.Close()
End If
End Sub
Public Sub PostReceived(ByVal postSampleData As String)
Console.WriteLine("PostChange(item {0})", postSampleData)
End Sub
the sub PostReceived is the callback method for my WCF service. It practically is meant to be do nothing as this is the Publisher, but I still have to implement it due to the WCF standards. The error that I am getting is
Class 'json' must implement 'Sub PostReceived(postSampleData As String)' for interface 'IPostingContractCallback'
How come i am getting the error when i have already implemented the sub as stated above?
Method signature must be: (Take a look at VB.NET Interfaces)
Public Sub PostReceived(ByVal postSampleData As String) Implements IPostingContractCallback.PostReceived
Console.WriteLine("PostChange(item {0})", postSampleData)
End Sub

VB.NET - Inherits System.Web.UI.WebControls.DropDownList doesn't call "Render" method

I have the following code, and when I put a Control of this type on a WebForm, it does not throw the Exception as you would expect it to, instead a <SELECT> is nicely rendered.
Imports System.ComponentModel
Imports System.Web.UI
<ToolboxData("<{0}:DropDownList runat=server></{0}:DropDownList>")> _
Public Class DropDownList
Inherits System.Web.UI.WebControls.DropDownList
Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
Throw New Exception("Hello World")
End Sub
End Class
However, I have this code, and it works just fine (in that the Exception does get thrown):
Imports System.ComponentModel
Imports System.Web.UI
<ToolboxData("<{0}:TextBox runat=server></{0}:TextBox>")> _
Public Class TextBox
Inherits System.Web.UI.WebControls.TextBox
Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
Throw New Exception("Hello World")
End Sub
End Class
Any ideas as to why?
Are you absolutely certain that you referenced the new class and not the standard asp.net one? That's the only reason I can think of that it wasn't called.
You might check the designer file to be absolutely certain. I have had numerous cases where I changed the control type in the source view, but the designer file was not updated until I went to the Design view and dirtied something.

How to print in VB?

I have a web application build using classic ASP and VB. How do I print a document from within the back end VB code?
Protected Sub btnPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrint.Click
End Sub
What i am trying to do is get user to click on button a letter is generated and sent to printer?
You could use the window.print javascript function which will open the print dialogon the client browser allowing him to choose the printer and print the page:
Protected Sub btnPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrint.Click
ClientScript.RegisterStartupScript([GetType](), "print", "window.print();", true)
End Sub
As a side note, what you have is not a classic ASP application with VB, you have a classic ASP.NET WebForms application using VB.NET as a code behind.
UPDATE:
As requested in the comments section here's how you could write a generic handler which will dynamically generate the HTML you would like to print:
Imports System.Web
Imports System.Web.Services
Public Class Print
Implements System.Web.IHttpHandler
Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
context.Response.ContentType = "text/html"
context.Response.Write("<html><body onload=""window.print();""><table><tr><td>value1</td><td>value1</td></tr></table></body></html>")
End Sub
ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property
End Class
Now simply navigate to /Print.ashx.
you could look into some reports frameworks like CrystalReports (used to come with VS but you have to download it from SAP nowadays.
Here is a nice tutorial for the report-engine that comes packed into Visual Studio (I have to admit I don't have much experience with this but it looks fine to me): Creating an ASP.NET Report

problem using winforms WebBrowser in asp.net

i am using the WebBrowser control in asp.net page. here is the simple code:
Public Class _Default
Inherits System.Web.UI.Page
Private WithEvents browser As WebBrowser
Dim th As New Threading.Thread(AddressOf ThreadStart)
Sub ThreadStart()
browser = New WebBrowser
AddHandler browser.DocumentCompleted, AddressOf browser_DocumentCompleted
browser.Navigate("http://www.someurl.com/")
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
th.SetApartmentState(Threading.ApartmentState.STA)
th.Start()
th.Join()
End Sub
Private Sub browser_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)
If browser.Document IsNot Nothing Then
Dim textbox As HtmlElement = browser.Document.GetElementById("txt1")
textbox.InnerText = "some text"
Dim button As HtmlElement = browser.Document.GetElementById("btn1")
button.InvokeMember("click")
End If
End Sub
End Class
the problem is that the webbrowser's DocumentCompleted event is not being handled. It looks like the page request finishes before anything else could happen.
what's the solution to this problem?
I really recommend reading this article(He won a price for it..)
Using the WebBrowser Control in ASP.NET
http://www.codeproject.com/KB/aspnet/WebBrowser.aspx
His solution is to create 3 threads for it to work..
I'm not sure but I have some concerns about the way you wrote your code.
You are creating and initializing your thread as soon as your class instance is created. This is before the form has been loaded.
I can't say for sure this couldn't work but I would definitely recommend creating the thread in your Load event handler, just before you use it.
I wrote some similar code in C# to generate a website thumbnail. Although that code does not use the DocumentCompleted event, I played with that event when I wrote it and it seemed to work okay. You can compare my code to yours.
Also, I should mention I have one hosting account where the code doesn't work. It seems to simply die when I call Thread.Join. However, it doesn't appear that's the issue you're running into.

Resources