Every once in a while I come across a question like: "I have a web application that has a long running job. It could take up to a few dozen minutes to complete. How can I keep the client status updated and not causing a timeout?"
The problem with a web application like this is that if the job run too long, it will cause a script timeout error. One can opt to increase the script timeout to prevent this but when you actually have an error, it would take forever before the client is notified of the error.
People often think that AJAX is the only solution to this problem but it isn't. But with a little clever coding, combining javascript with server side code, you can accomplish this. You can apply this technique to classic ASP or ASP.NET or whatever techonolgy or language you choose to use.
What we need to do is find a way to continuously sending data to the browser so that it doesn't time out on us. with vbscript and vb.net Repsonse.Buffer & Response.Flush can take care of this quite nicely.
So now we have the data streaming to the client, how to we update the status? The answer is javascript. We can script a function and take advantage of the ability to dynamically changing the content of the div or span tag to update the status when we receive some data from the server.
Please examine the source code for the complete example.
Showing posts with label Tutorial. Show all posts
Showing posts with label Tutorial. Show all posts
Friday, January 18, 2008
Thursday, August 30, 2007
Disable Windows Error Reporting
Every now and then I run into one of these application or system error that pops up the Error Reporting dialog box. This is one way Microsoft is getting the feedback from users on how well (or not) their software is performing or interacting with other applications and improve their products.
This is fine with me but sometimes, it gets really annoying when the same error keeps occurring and you have to report, wait and then close the dialog box or choose not to report it. To get rid of this headache once and for all, you can turn off this feature completely.
1. Launch System Properties dialog box
2. Click on Advanced Tab
3. Click on Error Reporting button
4. Select "Disable error reporting" radio button
5. Click OK twice to exit
You can launch System Properties dialog box through the Control Panel or by Right Click on My Computer then select Properties. Another way to bring it up is to press the [Windows][Break] key combinations.
This is fine with me but sometimes, it gets really annoying when the same error keeps occurring and you have to report, wait and then close the dialog box or choose not to report it. To get rid of this headache once and for all, you can turn off this feature completely.
1. Launch System Properties dialog box
2. Click on Advanced Tab
3. Click on Error Reporting button
4. Select "Disable error reporting" radio button
5. Click OK twice to exit
You can launch System Properties dialog box through the Control Panel or by Right Click on My Computer then select Properties. Another way to bring it up is to press the [Windows][Break] key combinations.
Sunday, June 10, 2007
Programmatically Add Controls At Runtime
As I have experienced it, in some situations, you might not know the types of control or how many controls will be needed at design time. In these cases, you'd have to create the controls programmatically and dynamically add them to the form at runtime.
Adding controls at runtime is pretty simple:
Dim newControl as New Windows.Forms.CheckBox
newControl.Name = "chkCheckAll"
newControl.Text = "check all"
newControl.Top = 10
newControl.Left = 10me.Controls.Add(newControl)
The code snippet above will declare a checkbox and add it to the current form.
If you don't know the type of control then it'd be best to declare the new control as a generic control and then later modify to suite your need.
Dim newControl as Windows.Forms.Control
If createCheckBox Then
newControl = New Windows.Forms.CheckBox
Else
newControl = New Windows.Forms.RadioButton
End If
I find the .Tag property particularly helpful when working with controls dynamically. You can use it to store specific information about the control and you it later on when you process the control.
This blog only touches some of the properties for CheckBox & RadioButton as I try to keep the blog short. You should further explore their other properties to gain more understanding about them.
Now that you know how to add controls at runtime, let's see how you can programmatically remove them.
Adding controls at runtime is pretty simple:
Dim newControl as New Windows.Forms.CheckBox
newControl.Name = "chkCheckAll"
newControl.Text = "check all"
newControl.Top = 10
newControl.Left = 10me.Controls.Add(newControl)
The code snippet above will declare a checkbox and add it to the current form.
If you don't know the type of control then it'd be best to declare the new control as a generic control and then later modify to suite your need.
Dim newControl as Windows.Forms.Control
If createCheckBox Then
newControl = New Windows.Forms.CheckBox
Else
newControl = New Windows.Forms.RadioButton
End If
I find the .Tag property particularly helpful when working with controls dynamically. You can use it to store specific information about the control and you it later on when you process the control.
This blog only touches some of the properties for CheckBox & RadioButton as I try to keep the blog short. You should further explore their other properties to gain more understanding about them.
Now that you know how to add controls at runtime, let's see how you can programmatically remove them.
Wednesday, May 30, 2007
Programmatically Ping A Networking Device
I would say Ping is one of the most popular software tool used to troubleshoot network issue. When a networking device or server goes down, one of the first things network engineers or system admininistrator do is try to ping it and see if it can be reached.
This blog will show you two ways to ping a device so you can integrate the ping feature into your application.
The first and easiest way to issue a ping command is to call the Ping method in the My.Computer.Network class:
If my.Computer.Network.Ping("www.google.com") Then
MsgBox("device up")
Else
MsgBox("device down")
End If
The drawback of this method is that it only gives you a boolean as the result to indicate whether the device is up or down.
The second way is to call the Ping.Send() method in the System.Net.NetworkInformation namespace which returns a PingReply through which you could get more detailed information about the ping:
Dim myPing As New System.Net.NetworkInformation.Ping
Dim PR As System.Net.NetworkInformation.PingReply
PR = myPing.Send("www.yahoo.com")
If PR.Status = IPStatus.Success Then
MsgBox("Reply from " & PR.Address.ToString & ": BYTES=" & PR.Buffer.Length & " TIME<" & PR.RoundtripTime & "ms TTL=" & PR.Options.Ttl)
Else
MsgBox(PR.Status.ToString)
End If
There you have it. Two ways of pinging a networking device. I recommend that you further study the PingReply object to get more information from the ping reply.
This blog will show you two ways to ping a device so you can integrate the ping feature into your application.
The first and easiest way to issue a ping command is to call the Ping method in the My.Computer.Network class:
If my.Computer.Network.Ping("www.google.com") Then
MsgBox("device up")
Else
MsgBox("device down")
End If
The drawback of this method is that it only gives you a boolean as the result to indicate whether the device is up or down.
The second way is to call the Ping.Send() method in the System.Net.NetworkInformation namespace which returns a PingReply through which you could get more detailed information about the ping:
Dim myPing As New System.Net.NetworkInformation.Ping
Dim PR As System.Net.NetworkInformation.PingReply
PR = myPing.Send("www.yahoo.com")
If PR.Status = IPStatus.Success Then
MsgBox("Reply from " & PR.Address.ToString & ": BYTES=" & PR.Buffer.Length & " TIME<" & PR.RoundtripTime & "ms TTL=" & PR.Options.Ttl)
Else
MsgBox(PR.Status.ToString)
End If
There you have it. Two ways of pinging a networking device. I recommend that you further study the PingReply object to get more information from the ping reply.
Parsing Text File
I often come across various postings in different forums asking for simple things like reading information from text file and parsing data from the text. Well, I get kind of tired of answering those question so I decided to write a blog about it and then point them to the blog. So here it is.
Now the text file named C:\Test.txt I have two simple line:
Firstname: Brian
Lastname: Dao
and I want to grab "Brian" & "Dao" out to use for some other purpose. The code is listed below:
Dim mySR As System.IO.StreamReader
Dim myFirstName, myLastName, myTempStr As String
mySR = System.IO.File.OpenText("C:\Test.txt")
If Not mySR.EndOfStream Then
myTempStr = mySR.ReadLine
myFirstName = Mid(myTempStr, InStr(myTempStr, "Firstname:", CompareMethod.Text) + 10)
myTempStr = mySR.ReadLine myLastName = Mid(myTempStr, InStr(myTempStr, "Lastname:", CompareMethod.Text) + 9)
End If
txtOutput.Text = myFirstName & vbCrLf & myLastName
mySR.Close()mySR = Nothing
Note that this doesn't take care of the leading space which could be trimmed by:
myTempStr.Trim
or
myTempStr = Trim(myTemStr)
Another way of accomplishing the same thing would be to call the Replace method and pass in the required parameters. The code sample below show two different ways you can call the Replace medhod.
myFirstName = Replace(myTempStr, "FirstName:", "", , , CompareMethod.Text)
myFirstName = myTempStr.Replace("FirstName:", "")
For those who are new to all this, be sure to look up the following string processing methods: InStr, InStrRev, Replace, Mid.
Now the text file named C:\Test.txt I have two simple line:
Firstname: Brian
Lastname: Dao
and I want to grab "Brian" & "Dao" out to use for some other purpose. The code is listed below:
Dim mySR As System.IO.StreamReader
Dim myFirstName, myLastName, myTempStr As String
mySR = System.IO.File.OpenText("C:\Test.txt")
If Not mySR.EndOfStream Then
myTempStr = mySR.ReadLine
myFirstName = Mid(myTempStr, InStr(myTempStr, "Firstname:", CompareMethod.Text) + 10)
myTempStr = mySR.ReadLine myLastName = Mid(myTempStr, InStr(myTempStr, "Lastname:", CompareMethod.Text) + 9)
End If
txtOutput.Text = myFirstName & vbCrLf & myLastName
mySR.Close()mySR = Nothing
Note that this doesn't take care of the leading space which could be trimmed by:
myTempStr.Trim
or
myTempStr = Trim(myTemStr)
Another way of accomplishing the same thing would be to call the Replace method and pass in the required parameters. The code sample below show two different ways you can call the Replace medhod.
myFirstName = Replace(myTempStr, "FirstName:", "", , , CompareMethod.Text)
myFirstName = myTempStr.Replace("FirstName:", "")
For those who are new to all this, be sure to look up the following string processing methods: InStr, InStrRev, Replace, Mid.
Adding Attributes To An Existing XML Document
In the previous blog, I've demonstrated how to add a node or an XML element to an existing document. In this blog, I'll introduce two ways to add an attribute to an existing node.
The first way we will use the SetAttributeNode method of the DocumentElement object to add an XMLAttribute as seen below:
Dim objDocument As New XmlDocument
objDocument.LoadXml("<Cars><Make>Toyota</Make></Cars>")
Dim objAttribute1 As XmlAttribute
objAttribute1 = objDocument.CreateAttribute("Type")
objAttribute1.InnerText = "Foreign"
objDocument.DocumentElement.SetAttributeNode(objAttribute1)
The second way is to add an attribute is to call the Append method of the Attributes collection class:
Dim objAttribute2 as XmlAttribute
objAttribute2 = objDocument.CreateAttribute("Body")
objAttribute2.Value = "Sedan"
objDocument.DocumentElement.FirstChild.Attributes.Append(objAttribute2)
and finally we can show the output to the textbox with the statement:
txtOutput.Text = objDocument.InnerXml
In the end, if you run these two code blocks together, your output should look something like this:
<Cars Type="Foreign"><Make Body="Sedan">Toyota</Make></Cars>
The first way we will use the SetAttributeNode method of the DocumentElement object to add an XMLAttribute as seen below:
Dim objDocument As New XmlDocument
objDocument.LoadXml("<Cars><Make>Toyota</Make></Cars>")
Dim objAttribute1 As XmlAttribute
objAttribute1 = objDocument.CreateAttribute("Type")
objAttribute1.InnerText = "Foreign"
objDocument.DocumentElement.SetAttributeNode(objAttribute1)
The second way is to add an attribute is to call the Append method of the Attributes collection class:
Dim objAttribute2 as XmlAttribute
objAttribute2 = objDocument.CreateAttribute("Body")
objAttribute2.Value = "Sedan"
objDocument.DocumentElement.FirstChild.Attributes.Append(objAttribute2)
and finally we can show the output to the textbox with the statement:
txtOutput.Text = objDocument.InnerXml
In the end, if you run these two code blocks together, your output should look something like this:
<Cars Type="Foreign"><Make Body="Sedan">Toyota</Make></Cars>
Labels:
System.XML,
Tips,
Tricks,
Tutorial,
VB.net,
Visual Basic.net,
xml
Tuesday, May 29, 2007
Adding Nodes using InnerText Property and CreateTextNode Method
In this blog, I'll discuss two simple ways of adding a node to an existing XML document.
Before we do anything, we will need to declare an XMLDocument and call the LoadXML method which takes an XML string as the parameter to populate the document
Dim objDocument As New System.Xml.XmlDocument
objDocument.LoadXML("<Cars></Cars")
Doing this, we will obtain a document with the root node as Cars and no child element.
In the first method, we will create the element using the CreateElement method, then create an XMLText object and set the value using CreateTextNode method, add the text object to the XML element before adding it to the document as follow:
Dim objElement As System.Xml.XmlElement
Dim objText As System.Xml.XmlText
objElement = objDocument.CreateElement("Make")
objText = objDocument.CreateTextNode("Honda")
objElement.AppendChild(objText)
objDocument.DocumentElement.AppendChild(objElement)
'these last too lines produce the same result as the previous two
'objDocument.DocumentElement.AppendChild(objElement)
'objDocument.DocumentElement.LastChild.AppendChild(objText)
The second method is much simpler. We create an element using the CreateElement method of the XMLElement object, set its value in the InnerText property and add it to the document using AppendChild method:
Dim objElement As System.Xml.XmlElement
objElement = objDocument.CreateElement("Make")
objElement.InnerText = "Toyota"
objDocument.DocumentElement.AppendChild(objElement)
There you have two ways of adding a new element to an existing XML document. I prefer the second method as it is much simpler, less coding and easier to understand.
TIPS: You can quickly show the content of the XML document by sending its content to a textbox using the following statement:
txtOutput.Text = objDocument.InnerXml
Before we do anything, we will need to declare an XMLDocument and call the LoadXML method which takes an XML string as the parameter to populate the document
Dim objDocument As New System.Xml.XmlDocument
objDocument.LoadXML("<Cars></Cars")
Doing this, we will obtain a document with the root node as Cars and no child element.
In the first method, we will create the element using the CreateElement method, then create an XMLText object and set the value using CreateTextNode method, add the text object to the XML element before adding it to the document as follow:
Dim objElement As System.Xml.XmlElement
Dim objText As System.Xml.XmlText
objElement = objDocument.CreateElement("Make")
objText = objDocument.CreateTextNode("Honda")
objElement.AppendChild(objText)
objDocument.DocumentElement.AppendChild(objElement)
'these last too lines produce the same result as the previous two
'objDocument.DocumentElement.AppendChild(objElement)
'objDocument.DocumentElement.LastChild.AppendChild(objText)
The second method is much simpler. We create an element using the CreateElement method of the XMLElement object, set its value in the InnerText property and add it to the document using AppendChild method:
Dim objElement As System.Xml.XmlElement
objElement = objDocument.CreateElement("Make")
objElement.InnerText = "Toyota"
objDocument.DocumentElement.AppendChild(objElement)
There you have two ways of adding a new element to an existing XML document. I prefer the second method as it is much simpler, less coding and easier to understand.
TIPS: You can quickly show the content of the XML document by sending its content to a textbox using the following statement:
txtOutput.Text = objDocument.InnerXml
Labels:
System.XML,
Tips,
Tricks,
Tutorial,
VB.net,
Visual Basic.net,
xml
Introduction to XML
For those of you just starting out with XML, I highly recommend that you check out the video tutorial(s) below:
VB
Working with XML & VB.net
C#
Working with XML & C#
VB
Working with XML & VB.net
C#
Working with XML & C#
Labels:
C#,
CSharp,
System.XML,
Tutorial,
VB.net,
Visual Basic.net,
VS.net,
xml
Subscribe to:
Posts (Atom)
