Monday, March 28, 2011

JavaScript Vs JScript Vs VBScript

My last post was about Web Scripting using Scripting Languages and today I am going to write about three commonly using languages in Web Scripting. Those are JavaScript, JScript and VBScript. Sometimes you must have wondered what is this JavaScript and JScript, because their names even feel similar too. So first I will start with JavaScript.

JavaScript

JavaScript is a light weight and very simple Scripting Language that has Object Oriented capabilities and most importantly it is a cross platform Scripting Language. These scripts can be integrated directly into the HTML code. I am pretty sure some of you must be thinking JavaScript has something to do with the Java programming language. Actually it has nothing to do with Java and these two are completely different from each other. JavaScript was originally developed by Netscape. In that time Java was a very popular programming language through out the industry. And the funny thing is, JavaScript got it named as JavaScript by Netscape as a marketing strategy to increase the popularity of JavaScript language. Anyhow I don't want to remind you, now JavaScript is one of the most popular Scripting Languages. Here is a simple example showing a text in a web page using JavaScript.

<html>
<body>
<script type="text/javascript">
{
document.write("Hello JavaScript");
}
</script>
</body>
</html>

JScript

Actually JScript is just the Microsoft's version of JavaScript. Microsoft thought of implementing their own version of JavaScript and they named it as JScript. But in some situations JScript is not behaving like JavaScript. I will give you a simple example. Let's declare a variable called temp in JavaScript code and create a object within the page having the field id as "temp".

var temp = 'Hello';

And trying to set the value of object temp in the page to the value of variable temp.

document.getElementById('temp').innerHTML = temp;

In here, JavaScript is working fine, but the JScript gives a error. The reason behind this is, in here JScript does not recognize the difference between var temp and document.getElementById('temp'). So he treats as both of them are referring to one single object. But JavaScript recognises the temp variable and the object within the page having id "temp" as two entirely separate fields. So that is a one significant difference between JavaScript and JScript. Another one is JScript supports additional commands that allow access to ActiveX and the local computer when comparing to JavaScript. But these commands are intended for use on intranet sites and for the browser Internet Explorer.

Anyway here is a simple example showing a text in a web page using JScript.

<html>
<body>
<script type="text/JScript">
{
document.write("Hello JScript");
}
</script>
</body>
</html>

VBScript

VBScript is Microsoft's Scripting Language just as JScript. It is actually a child of Microsoft's Visual Basic for use with web pages and other applications that uses Microsoft ActiveX controls. VBScript is very similar to JavaScript, but VBScript syntaxes are more similar to Visual Basic.

Comparison of JavaScript and VBScript

Similarities
  • Both languages are easy to learn and do not require any expensive development tools.
  • Both can be used to enhance web pages.
  • Both can abuse and run malicious scripts on clients' machines.
Differences
  • JavaScript is the default scripting language for browsers but VBScript must be specified as the scripting language.
  • JavaScript has cross-platform support from all popular browsers while VBScript is supported MS IE only. VBScripters would thus loose a sizable audience.
  • One of the most significant issues with JavaScript is that there were different releases of the language since its inception (version 1.0). Similarly, different versions of browsers exist on users machines. Therefore, code written for one version may not necessarily work on another. More testing would be necessary thus, development time increases.
  • JavaScript is case sensitive but VBScript is not this would not be prone to as many syntax errors like missing >
  • JavaScript uses the same character for concatenation as it does for addition (the + character) while the '&' concatenating character is used in VBScript. This is another source of errors in JavaScript.
Here is a simple example showing a text in a web page using VBScript.

<html>
<body>
<script type="text/vbscript">
{
document.write("Hello VBScript");
}
</script>
</body>
</html>

Hope you learn something about JavaScript, JScript and VBScript out of this post. Feel free to correct me and leave your comments.

Happy Coding.

Regards,
Jaliya

Saturday, March 26, 2011

Scripting Languages - Web Scripting

Since it is almost the week end and thought to write a small post about one of the most interesting and useful areas in Computer Programming Languages which is Scripting Languages. Scripting Language is basically a high-level programming language(more closer to human language rather than to the machine language) which is interpreted by another program at run time and not by the computer it self. When the script (program written in Scripting languages) got called it gets interpreted into computer commands.

There a lot of advantages of using Scripting Languages. I will list the down the two most inportant ones.
  • Most important thing is it's friendliness with other programming languages. Because of that they can be used to integrate larger systems. 
  • Other thing is Scripting Languages has nice I/O capabilities, including pipes, network sockets, file I/O, and file system operations. Because of that they can be used to communicate with external devices.
There are many types of Scripting Langauges such as Web Scripting, Windows Scripting, Game Scripting, Unix Scripting etc.

Today I am mostly focusing on a specific type of Scripting Language, which is Web Scripting. Web Scripting has two main sides which is Client Side scripting and Server Side scripting. Client Side scripting is, a collection of computer programs on the web that are executed in the client-side, by the user's web browser. User loads the HTML Web page which contains a script and then the script starts it's execution sitting inside the client machine's browser. Examples for the Client Side scripting languages are JavaScript, VBScript etc.

Server Side scripting is used to control the behaviour of the Server and manipulating the data on the Server. Examples for the Server Side scripting language are PHP, ASP, JSP, Perl, Python, RubyOnRails, CGI etc. In either case, the Web browser is used to access the program.

Web scripts are normally interpreted by the Web browsers. Web browsers interpret scripts along with Hypertext Markup Language (HTML), the language in which Web pages are written.

Feel free to correct me and give me your feedback.

Happy Coding.

Regards,
Jaliya

Monday, March 21, 2011

Trigger a Scanner using a button in InfoPath form - C#

As I have mentioned in one of my previous post I was spending some quality time with a scanning requirement. Requirement is simple, which is in a SharePoint form there should be separate buttons for scanning multiple documents. There are a lot of plugins  for SharePoint that can trigger a scanner and the most funniest thing is they only provides a limited functionality. And that is, first we should insert a item and then select the item and do the scanning. We can scan multiple documents and the same time it attaches them to the item. (people who might not familiar with SharePoint, might not understand what i told above about the scanning plugins for SharePoint).

But my requirement was not that. The form should have multiple buttons for trigger the scanner. Finally I planned to do some programming with InfoPath and somehow getting my thing done. Finally I have managed to develop multiple buttons to do multiple scanning and attaching to the same document same time, but of course it has some boundaries. I will explain the boundaries later in my post.

I have used Windows Image Acquisition Library 2.0 which can be downloaded for free from Microsoft. It has nice capabilities as well as some limitations. The one of the main limitations is Windows Image Acquisition Library is used for mainly capturing images from imaging devices such as scanners and cameras. So from what I have done, i was only able to get the output of the scanned documents as image type outputs such as JPEG, PNG etc. And the other main limitation is, it does not provide multi page scanning and the reason behind that is again WIA is used for mainly capturing images.

If I talk about the main boundaries of my scanning requirement development, first one is InfoPath form should be uploaded as a form template and should be set to open in the Client Application. The reason for this is if we enabled the InfoPath form to open in the browser, that means the InfoPath form is running on the Server. But the Client machine does not allow the Server to communicate with the Client machine's ports which simply means we cant trigger the scanner. So the InfoPath form should be opened in the Client machine. The other boundary is with the WIA, which is what I have explained above.

I am thinking and doing some things to overcome these boundaries. My solution for communicating with the Client machine from the Server is developing a service or a Client application which is fully separated from the Server and forget about the InfoPath form. Then design a form as a Visual Web Part using Visual Studio and deploy it to the Server. And then using a javascript or a vbscript, trying to communicate with the service or the Client Application, which is so far some what successful.

And for the second boundary, still I have not tried anything. I thing for that either I will have to convert JPEG/PNG into PDF file and add the functionality to add multiple pages to a single document, which seems pretty hard. So I think I might have to change my track into TWAIN scanning.

Will update all of you as soon as I have manage to overcome these boundaries. But I am fully happy with what I have done, because I am not using any of the plugins which you will have to buy to use it.

Happy Coding.

Regards,
Jaliya

Wednesday, March 16, 2011

Disconnected Data Access Architecture in ADO.NET

Have you ever faced the question that the Application stays connected to the DataBase system even when it is not using the DB services. It really is a headache to the Database as well as to the developer, because it will waste the Database resources and it will decease the performance of the Database. For this ADO.NET has provided us a nice concept called Disconnected Data Access Architecture.

First of all I will give you a basic idea about what ADO.NET is. I am sure most of you might have used or currently using ADO.NET. ADO.NET is a Object Oriented set of libraries or components which allows developers to fill the gap between Application and the data source, which simply means it provides the interface for the Application to Retrieve, Update and Manipulate data in the data source.

ADO.Net Primary Objects

Disconnected Data Access Architecture in ADO.NET

In traditional data access components, first we initiate a connection to the Database and pass SQL queries to the database and make things happen. But the problem here is, Application is always connected to the Database even when it is not communicating with the Database and if the Database is communicating with the Database most of the time it is for querying and viewing data.

To overcome this ADO.NET uses DataSets. When the Application needs to contact Database, it automatically connects to the Database and retrieve data from the Database and stores data in a DataSet which is a local buffer of persistent data. As soon as storing data is completed in the DataSet the connection gets terminated. Then the Application can do all the retrieving, updating and manipulating data from and to the DataSet, which relies completely out side of the Database. After the manipulation data in the DataSet is completed, we can synchronize them back with the Database.

And that is a brief introduction about how Disconnected Data Access Architecture works in ADO.NET. Please feel free to correct me.

Happy Coding.

Regards,
Jaliya

Sunday, March 13, 2011

.NET Reflection, Assemblies, Value types and Reference types

Since I am less busy today, thought to write another post about one of the most powerful and pretty interesting mechanisms in .NET framework which is .NET Reflection.

.NET Reflection allows the developer to observe and modify the program's structure at run time. The most interesting thing in Reflection is we can get all the information about loaded assemblies and types at Run Time.

When talking about assemblies, using assemblies we can obtain all the information about the fully functionality of a any .NET application. Actually .NET Reflection provides a developer a API (Application Programming Interface) to take a closer look at assemblies. Other than that, reflection API also allows developer to dynamically create assembly in memory and use it in the program code. The namespace for using .NET Reflection is System.Reflection.

When talking about Types, Types can be either Value types or Reference types. If you are wondering what are Value types and Reference types I'll give you all a brief introduction.
  • Value Types
    • Enumerators, Structs, and Primitives(except 'string').
    • Value types are stored in Stack
    • Value types cannot be null.
    • When passing a variable that is a value type, it passes that variable's value and not a reference to its underlying object.
  • Reference Types
    • Classes, Arrays, Delegates, Interfaces and string are reference types.
    • Reference types are stored in Heap.

There are a lot of things which can achieved using Reflection. I'll share some of the important ones.
  1. You can use reflection to create type instances at run time, invoke them and most importantly access them is run time. 
  2. Using reflection, you can find out the details of an object's methods in terms of its access modifier ( private, public etc.)
  3. You can discover the name and types of parameters in a methods signature.
For more details information about .NET Reflection visit MSDN, 

And that's how I understands .NET Reflection. Feel free to correct me or give me your feedback.

Happy Coding.

Regards,
Jaliya

Visual Studio Debugging - Attach to Process

Debugging!!!

I am sure this is the every good programmers way of finding and fixing bugs. Now I am going to give you all a brief idea about this nice feature called Attach to Process in Visual Studio. You can find this under Debug menu in Visual Studio. We can use Attach to Process to break into a running application and begin a debug session. This feature can be used mainly for debugging web applications which can be a  running instance of a web application or a web service hosted by IIS. ASP.NET applications will run in either an aspnet_wp.exe process (for IIS 5.x), or a w3wp.exe process (for IIS 6.0 and after).

Now when we click the Attch to Process it will display the following window.

Attach To Process
In here it shows all the available processses. There are several options you can select. If the program that you want to debug is running on another computer, you must first select the remote computer by selecting the Qualifier.

In the Attach to box, make sure that the type of code you will debug is listed. The default Automatic setting tries to determine what type of code you want to debug. If the automatic setting is not appropriate click Select and Select the code type and click Ok. In here I am selecting Managed (v4.0) code, Native code and T-SQL code. Actually when we click on the process, you can see the code type changes according to the selected process.

Select Attach to
Managed (v4.0) code, Native code, T-SQL code


We can select either to show processes from current user or show processes from all users by clicking the tick on Show processes from all users.

And the next option is bit important and that is Show processes in all sessions. Sometimes when you click the Attach to Process you might not see a single w3wp.exe running. The reason behind this can be one of the following.
  • The web application/web service might not be running.
  • Show processes in all sessions might not be ticked.
So if your web application/web service is already running and still you can't see the w3wp.exe under Available Processes you should tick the Show processes in all sessions and now you will be able to see running w3wp.exe process. You can always click Refresh to get tge current list of processes.

Now open the .cs file you want to debug using Visual Studio and set a break point to where you want to start debugging. Then go to Attach To Process and select the w3wp.exe and in your web application/web service go to the page where you have set the break point. Now when you enter to the break point, you can start debugging.

Things to Note:

You might wonder how to identify Which w3wp.exe PID corresponds to which application pool ?

     1.   In IIS 6 (Windows 2003):

The easy way to solve this problem was to call a Visual Basic script file that was located in the C:\Windows\system32\ folder. The file was called iisapp.vbs, and the output of this command listed the instances of w3wp.exe present on the system, their PIDs and to which application pool they belonged to.

     2.   In IIS 7 (Windows 2008) and IIS 7.5 (Windows 2008 R2)

The Visual Basic script file has been removed. Most of the IIS 6 script files that were dedicated to a single task (like iisapp.vbs) have been integrated into a command line utility that can be used to administer the IIS server as a whole. This utility is called appcmd.exe and it is located in the C:\Windows\system32\intesrv\ folder.

CAUTION: to access the tool you must launch the command line prompt with administrator rights, otherwise it will not work. To do this simply right click on a command line short cut and chose 'Run as Administrator' from the context menu.

Then navigate to the folder where appcmd is located (C:\Windows\system32\intesrv\) and run the following command: 
appcmd list wp –
This will list all the instances of the w3wp.exe process (worker process) that are present on your server with their respective PIDs and application pools.

For example, lets say you have several w3wp.exes and one's PID is 6772. Type the following command.
appcmd list wp 6772
It will show what application pool it is belonged to.

You can always type iisreset from C:\Windows\system32\intesrv\ to reset and IIS and update the PIDs.

And that's a brief introduction about Visual Studio Attach to Process feature. Appreciate your feedback.

Happy Coding.

Regards,
Jaliya

Monday, March 7, 2011

Submitting data to a SharePoint 2010 List using InfoPath - CAML

After moving around with some heavy tasks in SharePoint I wanted to Submit data to a SharePoint list through a InfoPath web form which has a Submit button in it. Did some googling and saw a way of submitting data through SharePoint in built web services. Thought of trying it and started to work on it. But after some time trust me, what i felt was this way is not hard, neither so easy.

So I thought of giving you all a step by step guide to learn how to do this without much frustration.
For this task, you will need SharePoint 2010 and Microsoft Office InfoPath Designer 2010 installed in your machine. You will need to have good understanding about CAML(Collaborative Application Markup Language), knowledge of the methods of the SharePoint Lists web service and that would be all and you are ready to go.

First of all CAML is an XML based markup language used with the family of Microsoft SharePoint technologies. Unlike plain XML, CAML contains specific groups of tags to both define and display (render) data. To get more information visit,
http://en.wikipedia.org/wiki/Collaborative_Application_Markup_Language

To get more information about the SharePoint Lists web service, in your IE navigate to "http://servername/_vti_bin/lists.asmx?WSDL" and you will be able to see all the web methods associated with the Lists web service. For our purpose we will be using UpdateListItems web method.

Now let's start. I am assuming you as a beginner, so I am adding a lot of step by step screen shots to ease your work.

1. Create a Custom List in a SharePoint Site. I have named it as Employee. Now create some columns as per your need. Note that there will be a default column called 'Title' and lets not change it and keep it that way to store EmployeeName, I have created EmployeeAddress, EmployeeNo, EmployeeEmail as columns which are Single text. Note that when Column names does not contain any spaces and later I will describe the reason to put them in that way.

2. Open Microsoft Office InfoPath Designer 2010 and select a Blank Form and click Design Form. Then do as follows.

Form Options

Security and Trust

Full Trust

Programming

Design

Design

Design

Renaming the button


Add fields






3. In SharePoint site under Employee list, in ribbon under List Tools select List and then select List Settings. In that page click Advanced Settings and select Allow management of content types to Yes.

List Settings



4.  Again go to List Settings click Metadata navigation settings and copy the list GUID.

Copy list GUID

5. Go back to InfoPath form right click on listID and paste GUID under value field.




6. Open Notepad and type,

<?xml version="1.0" encoding="UTF-8" >?
<Batch>
<Method ID="1" Cmd="New">
     <Field Name='Title' />
     <Field Name="EmployeeAddress" />
     <Field Name="EmployeeNo" />
     <Field Name="EmployeeEmail" />
</Method>
</Batch>

Save the file as an xml file and name it Submit.xml.

7. Now lets create Data Connections. Go to InfoPath form and click Manage Data Connections. First we need to create a Receive Data connection. I am naming it as 'Submit'.


Receive Data

XML Document

Browse for the created Submit.xml file


Naming the connection as 'Submit'

8. Now we need to create a Submit Data Connection. Again in Manage Data Connections click Add.

Add

Submit Data

To a Web Service

http://servername/_vti_bin/lists.asmx?WSDL

UpdateListItems

listname

listID

Include : Text and child elements only

Updates

Secondary


Batch

Include : XML Subtree, including selected element


Web Service Submit


Edit Form Code

9. Click Edit Form Code and btnSubmit_Clicked event will be created. In that block, type the following code.

try
{
    XPathNavigator root = MainDataSource.CreateNavigator();

    // Retrieve the values for the separation list item
    string listID = root.SelectSingleNode("/my:myFields/my:listID", NamespaceManager).Value;
    string eNameDS = root.SelectSingleNode("/my:myFields/my:EmployeeName", NamespaceManager).Value;
    string eAddressDS = root.SelectSingleNode("/my:myFields/my:EmployeeAddress", NamespaceManager).Value;
    string eNoDS = root.SelectSingleNode("/my:myFields/my:EmployeeNo", NamespaceManager).Value;
    string eEmailDS = root.SelectSingleNode("/my:myFields/my:EmployeeEmail", NamespaceManager).Value;

    if (listID == null)
    {
    
    }
    else
    {
        //This is CAML xml file. it contains batch and method nodes
        XPathNavigator batch = DataSources["Submit"].CreateNavigator();

        batch.SelectSingleNode("/Batch/Method/Field[@Name='Title']", NamespaceManager).SetValue(eNameDS);
        batch.SelectSingleNode("/Batch/Method/Field[@Name='EmployeeAddress']", NamespaceManager).SetValue(eAddressDS);
        batch.SelectSingleNode("/Batch/Method/Field[@Name='EmployeeNo']", NamespaceManager).SetValue(eNoDS);
        batch.SelectSingleNode("/Batch/Method/Field[@Name='EmployeeEmail']", NamespaceManager).SetValue(eEmailDS);

        DataConnections["Web Service Submit"].Execute();
}
catch (Exception ex)
{
    ex.Message.ToString();
}

10. And finally thats all. Preview the form and try submitting the data. After that publish the form and upload the form template into SharePoint and start using.

There are few important things to remember carefully. 
  • There is a significant difference between Display name and the Field name. When we are doing the coding we are using the value under the Field name. If you can remember, I have mentioned to keep the default 'Title' column as it is, because if we rename the 'Title' column it will just change the display name. The Field name will be remain as 'Title'.
  • No of updating list fields should be the same as no xml field names.
  • When I am creating columns in SharePoint list, if you can remember I have included no spaces between words. Thats because it will add characters into Field names. So when we are referring to Field names from looking at Display names, the Field name is actually differs from the Display name.
  • List fieldname should be same as xml fieldname.
  • Make sure when creating the Submit Data connection 'Web Service Submit' to select following things,
    • listName - listID : Include : Text and Child elements only
    • updates - Batch : Include : XML subtree, including selected element

Appreciate your feedback. 

Happy Coding.

Regards,
Jaliya

Saturday, March 5, 2011

Programmatically Scanning APIs (TWAIN & WIA) for C#

I have been struggling for some time to develop a program to trigger a scanner. Now I am going to share some of the things I learned about programmatically scanning. There are variety of scanners and they use different kind of protocols to communicate with the computer. As many of the applications programs needed scanning feature in their applications, developers had to develop Application Programming Interfaces(API) for scanners. The API would be the interface that would be dealing with any kind of scanner. Simply means the application does not have to know how the hardware part in the scanner is working to access it directly. They just interacts with the scanner interface.

There are two main Standard Software Protocols or APIs that can be used to acquire images from a scanner.
  1. TWAIN
    • The word TWAIN is not an official acronym, however, it is widely known as "Technology Without An Interesting Name."
    • For more information visit, http://en.wikipedia.org/wiki/TWAIN
     2.   WIA (Windows Image Acquisition) (Sometimes also called as Windows Imaging Architecture)
    • Microsoft driver model and Application Programming Interface (API) for Microsoft Windows Me and later Windows operating systems that enables graphics software to communicate with imaging hardware such as scanners, digital cameras and Digital Video-equipment.
    • For more information visit, http://en.wikipedia.org/wiki/Windows_Image_Acquisition
In my next posts I will post, how these APIs has helped me to develop my program.

Happy Coding.

Regards,
Jaliya