Cheers!
.Net, ASP.Net, C#, VB.net, SQL Server, Xml, CSS, Design Patterns related tips, tricks, code snippets, articles, links, thoughts, etc. from Binu & Subi Thayamkery.
.net etc.
Binu Thayamkery is a seasoned software architect with more than 13 years of experience in developing enterprise grade connected systems using Microsoft Technologies. In his current position as a lead consultant-solution architect with Prudential Financial, he is working on architecture of next generation investment reporting framework using .net 3.5/WCF/AJAX, etc. He holds a Masters Degree in Computer Science from Colorado State University. Subi Thayamkery is an experienced software developer with more than 8 years of developing various application software systems ranging from workflow automation systems to compliance management tools. She currently works as a technology consultant for Prudential Financial where she helps develop a new system for corportate governance department. She holds an Electrical Engineering degree from New Jersey Institute of Technology.
Thursday, May 03, 2007
What is *.VSHost.Exe?
Cheers!
Friday, January 19, 2007
Windows Service - Re-install error in Windows 2000
Trick is to re-start the machine before you try to re-install it.
For more details refer to this Microsoft link: http://support.microsoft.com/kb/823942
Note: To install/uninstall a windows service (developed using .net), you use installutil.exe
Cheers!
Friday, January 05, 2007
Windows Service and .Net
windows service simple and straight forward.
What process is a good candidate to be written as a Windows service?
A long running process or a repeating process that requires no user interaction, no
user interface, not even a console - just sit and do some work kind of process - you write it as a
windows service. Just make sure that you document your service well and publish, so that every one
knows the existence of it. Because for the server ops people, when they take inventory of stuff running
in a server, a custom windows service is not the first one makes the list. This might result in your
service not started after a server re-start and so on.
So it is a good practice to setup your service to start automatically, also if your service does not
require any special permissiona run it under local account. If it requires special permissions like
accessing a file share, create a service account with least required access and run service under that
account...
Next post I will discuss writing a simple service...
Cheers!
Thursday, January 04, 2007
ASP.Net consuming Java WebService - Some observations
The underlying connection was closed: Could not establish trust relationship with remote server.
Possible Solution:Add a class that implements ICertificatePolicy
public class PolicySubClass : ICertificatePolicy { public bool CheckValidationResult( ServicePoint srvPoint , X509Certificate certificate , WebRequest request , int certificateProblem) {
//Return True to force the certificate to be accepted. return true;
} }
Then in your class code, addSystem.Net.ServicePointManager.CertificatePolicy = new PolicySubClass();2) If you are accessing your web service via a proxy, you might get this error
The underlying connection was closed: The server committed an HTTP protocol violation.
If you google this issue, you will find suggestions to fix this in couple of different ways.First approach is to modify web.config to add section like this:
In my case, this did not help, I had to do this in my code,
WebProxy proxyObject = new WebProxy("http : // myproxy:10001", true); wsproxy.Proxy = proxyObject;
here, wsproxy is the your web service proxy variable.
3) For many of the connection based errors/issues, many suggest to subclasss your web serviceproxy class to set the HTTP Keep Alive to false
public class ProxySubClass : MyWebServiceProxy { public ProxySubClass(){}
protected override System.Net.WebRequest GetWebRequest(Uri uri) { System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest) base.GetWebRequest(uri); webRequest.KeepAlive = false; return webRequest;
} }
4) As a rule of thumb, when ever there is a connection issue to a web service, start by pasting the web service URL to your web browser address window and try to access the wsdl.If this works, then start trying the above said tweaks!
Cheers!
Monday, October 23, 2006
Create a Printer friendly page for DataGrid
1. Add a new aspx page, call it Print.aspx
2. Add javascript to trigger IE print, on body load,
--body onLoad= "self.print()" --
--body--
3. Now comes the data, pass the underlying datasource for the grid to this page (use the method you like, use a session variable (bad), use a unique keyed cache item (ok), get it from parents property (good). Once you have this data, the following code block does the trick. Key is the RenderControl method of datagrid. It uses a HtmlTextWriter object to write out the HTML snippet of the grid. If we create the grid with plain vanila settings for color and edges, it will be "printer friendly".
On Page load add (don't forget to pass the dataview,
Response.Write(CreatePrinterFriendlyPage(dview).toString());
private StringBuilder CreatePrinterFriendlyPage(DataView dv)
{
DataGrid dg = new DataGrid();
dg.DataSource = dv;
dg.HeaderStyle.Font.Name = "Arial";
dg.HeaderStyle.Font.Bold = true;
dg.HeaderStyle.Font.Size = FontUnit.XSmall;
dg.ItemStyle.Font.Name = "Arial";
dg.ItemStyle.Font.Size = FontUnit.XXSmall;
dg.DataBind();
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter(tw);
dg.RenderControl(hw);
StringBuilder sb = new StringBuilder();
sb.Append (tw.ToString().Replace("\"","'").Replace("\r","").Replace("\n","").Replace("\t",""));
return (sb);
}
Happy coding !
Monday, October 02, 2006
System.Data.OracleClient requires Oracle client software version 8.1.7 or greater.
"System.Data.OracleClient requires Oracle client software version 8.1.7 or greater."
Misleading....beacuse I know that I have ver 9.2 installed!
Luckily, Google is there to help, issue is related to permissions! and the solution is goven in this good blog post by Roy Tore Gurskevik,
http://www.dotnetjunkies.com/WebLog/rtgurskevik/archive/2005/01/19/45958.aspx
Tuesday, August 08, 2006
For ASP.Net Developers
Keep Sites Running Smoothly By Avoiding These 10 Common ASP.NET Pitfalls
A must read !
ADO.Net and Sybase ! Strange behavior !
I am using .Net v1.1, Sybase db ver 12.5 with Sybase ASE OleDb Provider Ver 2.70.
When I create parameters for my command (CommandType is StoredProcedure), it expects me to create the params in the same order as specified within the stored procedure. I am creating the params using names and not by ordinal! There is no error thrown even when the params are in different order, values simply gets assigned to the wrong params !!
Did any one of you have seen similar issue ??
Thursday, June 01, 2006
Infragistics Net Advantage 2005 Vol 3 - Tips and Tricks - Part 1
If you are upgrading Infragistics from an older version to newer version, make sure to upgrade your project files using .Net Project Upgrade Utility (also found in Infragistics Program Group). Make sure that you have all the latest hot fixes applied (to avoid nasty surprises). A complete installation will also install
help files, regsiter them propery, it also comes with a nice Samples Browser with a lot of working samples.
2) First time when you starting your development with Infragistics, (for example UltraWebgrid), you set your references to the required assemblies, dragged the control on to your form and ran it. If you get a runtime error pointing to missing "BorderStyle" class/namespace reference, you probably need to set "CopyLocal" property for the WebUI assembly to True. To do this locate Infragistics.WebUI.Shared.v5.3, right click and Properies, set CopyLocal to true.
3) Infragistics provides you a very rich UI experience. How does it do it? It uses a mix of client side javascripting and server side programs. Infragistics comes with a neat collection of client side javascript library for each of its controls. This provides rich client side features. It is AJAX enabled, and makes out-of-band calls to server to load data when the control's properties are set to use that feature. When you intsall the product (example: Ver 2005 Vol 3)
you will notice that a virtual directory named "ig_common" is created for you. This virtual directory hosts the required javascript files and other resources that are required for running Infragistics controls.
4) Discussion of the previous point leads us to the deployment of applications that uses Infragistics, For a simple deployment follow these steps,
- Copy the Infragistics Assemblies that are used by your application to a install folder in your server. These are signed assemblies. Add these to Global Assembly Cache (GAC).
- Copy the directory that is pointed by ig_common virtual directory in your dev
- Create a virtual directory in your server, point it to the folder explained in the previous step.
- Build your application that uses Infragistics, deploy it to the server ( which ever way you choose, xcopy, copy project..)
5) Page.SmartNavigation - Set this to False from design view. I have seen many developers complaining about this feature in v1.1. Setting this property to true is not recommended (so I was told by Infragistics Support once!)when your page has Infragistics components. If you want to preserve the scroll position on your page use some Javascript technique.
6) UltraWebGrid - Simple Binding - Works pretty much the same way as a regular ASP.Net grid. Please remember that UltraWebgrid can have multiple bands (virtual grids within grid) and columns collection is within the Band. Another important property is DisplayLayout. Most of the UI elements are defined within this. If you are binding the grid on the fly relying on the underlying datasource for columns, Set DisplayLayout > AutoGenerateColumns to True. You can also choose to add columns design time, setting the data field names for each columns, in that case set AutoGenerateColumns to False.
Example:
myGrid.DataSource = myDataSet.Tables[0].DefaultView;
myGrid.DataBind();
Once the DataBind() is done, the grid fires "InitalizeLayout" event. This is one event you can hace code to handle some useful
properties of the grid. This can be a good place to handle setting the column widths, making some columns hidden, setting "keys"
for columns, turning on/off sorting for columns, handling header captions and style to name a few.
//Do not forget to import the namespace
using Infragistics.WebUI.UltraWebGrid;
using Infragistics.WebUI.Misc;
:
:
//Examples of setting keys for columns; you can use the key to grab handle to this column
myGrid.Bands[0].Columns[0].Key = "Check";
myGrid.Bands[0].Columns[1].Key = "Id";
//Setting Align
myGrid.Bands[0].Columns[0].CellStyle.HorizontalAlign = HorizontalAlign.Center;
//Setting width
myGrid.DisplayLayout.Bands[0].Columns[0].Width = Unit.Pixel(30);
//setting click action on header
myGrid.DisplayLayout.Bands[0].Columns[0].Header.ClickAction = HeaderClickAction.NotSet;
//Setting Header Caption
myGrid.DisplayLayout.Bands[0].Columns[0].Header.Caption = "My Caption";
//set sort for all bands and columns of the grid to "No Sort"
myGrid.DisplayLayout.HeaderClickActionDefault = HeaderClickAction.NotSet;
More coming sooon....
Thursday, May 25, 2006
HOWTO: Get rid of " Could Not Establish Trust Relationship with Remote Server" error
System.Net.ServicePointManager.CertificatePolicy = new MyPolicy();
Code for the class is given below,
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class MyPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint
, X509Certificate certificate
, WebRequest request
, int certificateProblem) {
//Return True to force the certificate to be accepted.
return true;
} // end CheckValidationResult
} // class MyPolicy
You could also check for specific errors/warnings in the CheckValidationResult method, in the above given example, it overrides any error!
Happy coding:)
Tuesday, May 16, 2006
Attention!!! all Infragistics Net Advantage Users
This hot fix fixes at least couple of issues I have encountered,
- Infragistics script library code (js code) going into indefinite loop resulting in your web server going toast! This happens when you are doing add/edits directly into the UltraWebgrid...When this happens your CPU just peaks to 100% all eaten by your iis process
- Again, Utrawebgrid eating all the memory when it is heirarchial and paging is enabled..(I am not sure about the sequence to re-produce the error!!)
So....Apply the hot fix! and keep coding ;-)
Cheers!
Visual Studio.NET - Web Reference Static v/s Dynamic
In order to start using a web service, you should make a reference to this from Visual Studio,to acheive this,
- On your VS Solution Explorer, select your project, expand to References,Right click and click on Add Web References
- Type in URL of your web service and click on Go
- This will bring up your Web service's wsdl, you can change your web reference name to anything you want and click on Add Reference
Thats it, at this point, Visual Studio does all the work required (like creating your proxy class) and you are set to call your web service's web methods.
Now for some important and interesting observations!
The web reference you just created is a static reference, meaning it is always linked to the web service URL you just typed in. You can verify this by right clicking on your web reference to view the property sheet. You will notice that the URL Behavior is set to "static" with the Web Reference URL pointing to the URL you typed in. This is fine as long as you have only one environment (to TEST,QA,Stage, PRoduction....). But when you have to move your application through various environments in its life cycle, you will need to change this reference and rebuild the application. This leads us to the URL Behavior of "dynamic", when dynamic is set, your refrence.cs file (proxy) will include a property to read the URL from a Config File.
Code will look something like this...
public MyWebReference() {
string urlSetting = System.Configuration.ConfigurationSettings.AppSettings["MyWebRef"];
if ((urlSetting != null)) {
this.Url = string.Concat(urlSetting, "");
}
else {
this.Url = "http://myWS/myWS.asmx";
}
}
Depending on the type of Visual Studion Project your web service client is, it works slightly different,
- If your app is a web application and making the web reference from your application, this App Setting entry will go into its web.config file
- If your app is windows application, making a dynamic reference will create an app.config entry and when built will be part of your app.exe.config
- If you are referencing web service from a Class library project, the entry should be made to the client of that class library, if web project is using this class library then entry should be in web config or if its a win app using the class library, app setting entry will goto app.config!
Happy coding !!
Cheers!
Thursday, May 04, 2006
Response.Redirect not working in server?
As a work around, set your SmartNavigation to false, before calling Response.Redirect
Page.SmartNavigation = false;
Response.Redirect("yourpage.aspx");
Wednesday, April 12, 2006
Convert String to Proper Case / Title Case
In VB 6.0, we use StrConv() function to convert to proper case.
1 Dim stringVariable as String
2 stringVariable = "hello world"
3 stringVariable = StrConv(stringVariable,vbProperCase)
4 Debug.Print stringvariable 'Prints Hello World
In VB.Net, we could still use the same function, use the Microsoft.VisualBasic Namespace.
1 Dim stringVariable as String
2 stringVariable = "hello world"
3 stringVariable = StrConv(s,VbStrConv.ProperCase)
4 System.Diagnostics.Debug.Writeln (stringvariable) 'Prints Hello World
If you are using c#, you could use the same technique, only point to note is that you will need to pass the locale ID to the StrConv() function (optional param is not possible)
1 string stringVariable = "hello world";
2 stringVariable = Strings.StrConv(s,VbStrConv.ProperCase,new CultureInfo("en-us").LCID);
3 System.Diagnostics.Debug.Writeln (stringvariable); //Prints Hello World
A little more elegant solution is to use TextInfo class to achieve the same.
C# code is shown below,
1 string stringVariable = "hello world";
2 stringVariable = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(Input.ToLower());
3 System.Diagnostics.Debug.Writeln (stringvariable); //Prints Hello World
Happy Coding!
Cheers!
Tuesday, March 28, 2006
How to find out which webcontrol generated postback?
of Javscript that looks like this,
1 function __doPostBack(eventTarget, eventArgument)
2 {
3 var theform = document.Form1;
4 theform.__EVENTTARGET.value = eventTarget;
5 theform.__EVENTARGUMENT.value = eventArgument;
6 Form1.submit();
7 }
When any controls generates a postback (example: Button Click), this Javscript is called and values are set to the hidden variables named __EVENTTARGET and __EVENTARGUMENT. __EVENTTARGET is the control that is doing the postback and __EVENTARGUMENT holds any additional information for that event. Now, with this information, it is easily understood how to use this at server side Page_Load event to determine which control caused the postback.
This is done by examining the request obejct to see the values in __EVENTTARGET.
1 char[] delim = {'_'};
2 string[] pbCtrl = Request.Form["__EVENTTARGET"].Split(delim);
3 System.Diagnostics.Debug.WriteLine (pbCtrl[0]);
4
pbCtrl[0] should contain the ID of the control that triggered this postback!
Cheers!
Friday, March 03, 2006
ASP.Net - Some performance myths.
- C# code is faster than Visual Basic code - False! Rob says that there is a grain of truth in this mainly because VB.Net allows performance hindering actions like not explicitly declaring types.I think both should be comparable because all the .net languages are ultimately creatingt code targeting the framework CLR and CTS. If you follow good programming practices, VB.Net and C# should provide with same features and performance (pretty much!)
- Code-Behind is faster than Inlince code - False! Rob points out that sometimes it is advantageous to just change inline (or add inline) code because touching code behind means a Dll rebuild. If you ask me, I would say NO to inline code, haven't we had enough of spaghetti coding with all than classic ASP?
- Components are faster than Pages- False! True in classic ASP, no longer in ASP.Net, since page is also a class, it provides the same performance as components. Again from a modular design point of view, lets go with components!
Cheers!
Wednesday, March 01, 2006
Clear your web applications Cache
Being said that, I find sometimes a need to clear my web applications cache without affecting anything. In order to do that, this is the trick I have come up with (nothing genius here :)
I created a secure page (admin login required) with a "Clear All Cache" Button,
and here is what I do in that button click!
private void ClearCache_Click(object sender, System.EventArgs e)
{
IDictionaryEnumerator CacheEnum = Cache.GetEnumerator();
string cacheKey="";
while (CacheEnum.MoveNext())
{
cacheKey = Convert.ToString(CacheEnum.Key);
Cache.Remove(cacheKey);
}
}
Wednesday, February 15, 2006
.Net Framework Tools Part 2
Type library importer imports the type library information for a COM components into equivalent definitions in command language runtime library. This is useful when you want to use your legacy COM components with your new .Net application (think COM Interop). If you are someone like me who uses Visual Studio.Net, you can do this by adding a reference to the COM components from your IDE. For others, sample usage is:
tlbimp myTLB.tlb /out:myNet.dll
For more information on this tool goto: http://msdn.microsoft.com/library/en-us/cptools/html/cpgrfTypeLibraryImporterTlbimpexe.asp
Type library exporter is opposite of importer, it is useful to create a type library for your .netr assembly. Again this is used in a COM interop scenario when your COM application (ex: your old VB 6.0 App) wants to use some new functionality that your developed using .Net ( I see this in lot of places where critical VB 6.0 apps still being used and new development moved to .net and this approach is usually part of the migration strategy)
Usage is:
tlbexp myNet.dll /out:myTLB.tlb
One important information to note here is that this tool creates the TLB but does not register it. For that you should use another tool (discussed next) , RegAsm.exe
For more information on this tool goto: http://msdn.microsoft.com/library/en-us/cptools/html/cpgrfTypeLibraryExporterTlbExpexe.asp
Assembly Registration Tool (RegAsm.exe)
This tool reads the metadata within the .net assembly and creates the necessary information within the registery so that a COM client can consume services of a .Net component. This tool can optionaly create a TLB also. Afer you register your assembly using this tool, you can install it into Global Assembly Cache also so that it can be activated from any client. How do we do it? Use GacUtil.exe which will be discussed next :)
Usage for RegAsm.exe:
regasm myNet.dll
or when you want to create a TLB, useregasm myNet.dll /tlb:myTLB.tlb
For more information on this tool goto: http://msdn.microsoft.com/library/en-us/cptools/html/cpgrfAssemblyRegistrationToolRegasmexe.asp
Global Assembly Cache Tool (GacUtil.exe)
This tool is used to view and manipulate the contents of the Global Assembly Cache. Like we explained earlier, one use is to install new .net assembly into GAC. This can also be used to uninstall an assembly. Examples of simple usages are:
For install
gacutil /i myNet.dll
For uninstall
gacutil /u myNet.dll
For more information on the tool and its usage goto:http://msdn.microsoft.com/library/en-us/cptools/html/cpgrfGlobalAssemblyCacheUtilityGacutilexe.asp
WinZip command line and unzip script
Option Explicit
On Error Resume Next
Dim oArgs
Set oArgs = WScript.Arguments
if oArgs.Count <> 2 Then
WScript.Echo "Usage: zUnzip <ZIP FILENAME> <PATH TO UNZIP>"
WScript.Quit
End if
Dim ZipFileName
Dim PathName
ZipFileName = oArgs.Item(0)
PathName = oArgs.Item(1)
'Unzip given zip file to specified path
Dim objShell
Set objShell = WScript.CreateObject("WScript.Shell")
Dim cmdScript
cmdScript = "%comspec% /c wzunzip.exe " & ZipFileName & " " & PathName
objShell.Run(cmdScript), 1, True
WScript.Quit
Some points worth mentioning,
objShell.Run(cmdScript), 1, True
In this command, second parameter for the Run Method is window style, if it is 1 it shows a window, use 0 for not showing the command window
Also note %comspec% /c in the command, this variable gets you the command interpreter for your OS, for example for Win2K, it gives you "cmd" and /C switch closes the secondary session after execution.
Cheers!
Thursday, February 09, 2006
Copyright 2008 Binu Thayamkery/Subi Thayamkery. All code snippets, samples provided AS-IS. Use at your own risk !