Tuesday, February 22, 2005
sizeof (.net object) ?
After some googling found the following piece of code
long bytecount = System.GC.GetTotalMemory(true);
DataSet1 ds = new DataSet1();
ds.EnforceConstraints = false;
ds.Order_Details.BeginLoadData();
ds.Orders.BeginLoadData();
ds.ReadXml("c:\\test.xml");
bytecount = System.GC.GetTotalMemory(true) - bytecount;
MessageBox.Show("Loaded - Waiting. Total K = " + (bytecount/1024).ToString());
long bytecount = System.GC.GetTotalMemory(true);
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
xmlDoc.Load("c:\\test.xml");
bytecount = System.GC.GetTotalMemory(true) - bytecount;
MessageBox.Show("Loaded - Waiting. Total K = " + (bytecount/1024).ToString());
Thanks Barry Gervin, http://objectsharp.com/Blogs/barry/archive/2004/02/24/284.aspx
Thank god, Java developers too, doesn't have a simpler option for this and a solution i found thats used in Java world is much like Barry's code.
Not being skeptical here, but we tried using a memory analysis tool (Compuware's DevPartner) and also used the windows perfmon to cross check the value. Oops, none of the value match, no not some minor differences, it differed by a huge numbers. We had to take a expert guess, one which closely matched the memory size calculated from database columns being fetched, no i am not going to disclose which method was close, as i'm myself not sure which one is.
If you can think of something of perfect way to get the memory foot print of a .NET object, pls let me know with your comments here. I appreciate your help.
Saturday, February 19, 2005
.NET app - another running instance check and DTC exception
Process [] aoRunningInstance = Process.GetProcessesByName(Constant.APPLICATION_NAME);
if (aoRunningInstance.Length > 1)
{
throw new ApplicationException("Another instance of app is running. Aborting current instance.");
}
aoRunningInstance = null;
Everything went fine; it found another instance and aborted, but weirdly in a scenario
//Create a new Transaction
//Open SQL connection inside the Transaction
//Open DB2 connection inside the Transaction
Line where i try to open DB2 connection, an exception is thrown >>>>> [IBM][CLI Driver] CLI0126E Operation invalid at this time. SQLSTATE=HY011
It was found that if we comment out another instance check code, the error is not happening. #@$@#$@!#. Wait, fix to this error is even weirder, we open and close the DB2 connection before this another instance check code and click the error vanishes in thin air.
I found a another way of to check another running instance of the app from Updater Application Block,
private Mutex _appStartMutex;
private readonly Guid _mutexGuid = new Guid( new byte[] { 0x5F, 0x4D, 0x69, 0x63, 0x68, 0x61, 0x65, 0x6C, 0x20, 0x53, 0x74, 0x75, 0x61, 0x72, 0x74, 0x5F });
bool isOwned = false;
_appStartMutex = new Mutex( true, System.Reflection.Assembly.GetExecutingAssembly().FullName + _mutexGuid.ToString(), out isOwned );
if ( !isOwned ){
throw new ApplicationException("Another instance of app is running. Aborting current instance.");
}
This will not alow the application to run even if its run with different executable name or from a different location. But has overhead of Mutex, not sure if this will fix this the above error.
It indeed fixes the above issue and after some thought i think it will be performance effective to have Mutex instead of pulling every process name that is running machine, i always see a 100% spike if i do this using task manager.
Tuesday, February 15, 2005
ASP.NET Datagrid to Excel clipboard copy/paste: an ugly javascript solution
function GetExcelData(){
var content = clipboardData.getData("Text");
if (content!=null) {
var t = content.replace(/\n/g," \n");
t = t.replace(/\t/g," \t");
var myArray = t.split(/\n/);
var y = "<table align='center' border='1'>";
for (var i = 0; i < myArray.length; i++) {
y = y + "<tr>";
var myArray2 = myArray[i].split(/\t/);
for (var j = 0; j < myArray2.length; j++) {
y = y + "<td> " + myArray2[j] + "</td>";
}
y = y + "</tr>";
}
y = y + "</table>";
window.spnMsg.innerHTML = y;
}
else {
window.spnMsg.innerHTML = "No text found in clipboard.";
}
}
function PutExcelData(){ //... build a string in delimited format and put it in clipboard
Proposed solution is to provide a button "Copy", to copy the data from datagrid to clipboard and a "Paste" button to copy the data data from clipboard to datagrid.
Warning: Not sure if it is supported in all IE versions and other GOOD browsers like Firefox.
I found a workaround also, use a textarea form field that holds the text to be copied, select it by form.textarea.select() method and copy it to the clipboard using the execCommand like below, Unfortunately the select() method works with visible form objects only, that can be easily overcomed but putting that control in a div tag and making the div tag invisible.
document.myForm.invisibleTextArea.select();
document.execCommand('Copy');
It struck me after coding the solution, how easy it will be to exploit this and steal a sensitive information from clipboard (i know many copy/paste passwords, credit card nbrs!). I expect IE to atleast prompt user, before allowing this script to access clipboard.
Sunday, January 30, 2005
asp.net custom validator for mmddyy date format
To minimize the impact to the code, (almost all times, these reqts spring up only in user acceptance stage, where you have not time think a better solution; will most probably patch the code) i added the following java script, which on tab out of text box, put the slash in approp place, but unfortunately the asp.net validator fires before this onblur event and hence fails, so i had to create a custom validator which formats and also validates the date. I am posting that useless date time patch code below,
<asp:TextBox id="TextBoxDate" runat="server" Width="65" MaxLength="8" CssClass="textbox"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorDate" ControlToValidate="TextboxDate" Enabled="false"
Display="Static" ErrorMessage="Date is required and must be in MMDDYY or MM/DD/YY format." Runat="server" />
<asp:CustomValidator id="CustomValidatorDateCheck" runat="server" ErrorMessage="Date or Date format is invalid. Format is MMDDYY or MM/DD/YY." ClientValidationFunction="FormatAndValidateDate" ControlToValidate="TextBoxDate" />
/*
This is having client script for formating and validating date. The following date formats are accepted:
mmddyy, mmddyyyy, mm-dd-yyyy, mm/dd/yyyy, mm.dd.yyyy, mm dd yyyy,
mmm dd yyyy, mmddyyyy, m-d-yyyy, m/d/yyyy, m.d.yyyy,
m d yyyy, mmm d yyyy, m-d-yy, m/d/yy, m.d.yy, m d yy,
mmm d yy (yy is 20yy)
*/
function FormatAndValidateDate(oSource,oArguments) {
var szDate;
var szDateArray;
var szDay;
var szMonth;
var szYear;
var bFound = false;
var aszSeparatorArray = new Array("-"," ","/",".");
var iElementNr;
oArguments.IsValid = false;
if (oArguments.Value.length < 6){
return;
}
szDate = oArguments.Value;
szDate = szDate.replace(/^\s+/,'').replace(/\s+$/,'');
var szDateFormat = new RegExp("^(\\d{1,2})([-./ ]{0,1})(\\d{1,2})\\2((\\d{4})|(\\d{2}))$");
m = szDate.match(szDateFormat);
if (m == null) {
return;
}
for (iElementNr = 0; iElementNr < aszSeparatorArray.length; iElementNr++)
{
if (szDate.indexOf(aszSeparatorArray[iElementNr]) != -1)
{
szDateArray = szDate.split(aszSeparatorArray[iElementNr]);
if (szDateArray.length != 3)
{
return;
}
else
{
szDay = szDateArray[0];
szMonth = szDateArray[1];
szYear = szDateArray[2];
}
bFound = true;
}
}
if (bFound == false)
{
if (szDate.length > 5)
{
szDay = szDate.substr(0, 2);
szMonth = szDate.substr(2, 2);
szYear = szDate.substr(4);
}
}
if(szYear != null)
{
if (szYear.length == 2)
{
szYear = '20' + szYear; //If entered 2 digit yr, consider it as 21st century
}
}
//swap for US date format mm/dd/yy
var szTmp = szDay;
szDay = szMonth;
szMonth = szTmp;
szTmp = "";
if( (szMonth<10) && (szMonth.length == 1))
{
szTmp = "0" + szMonth + "/";
}
else
{
szTmp = szMonth + "/";
}
if( (szDay<10)&& (szDay.length == 1))
{
szTmp += "0" + szDay + "/";
}
else
{
szTmp += szDay + "/";
}
document.getElementById(oSource.controltovalidate).value = szTmp + szYear.substr(2, 2);
szMonth = szMonth - 1; // javascript month range : 0- 11
var tempDate = new Date(szYear,szMonth,szDay);
if ( (typeof(tempDate) == "object") && (getYear(tempDate.getYear()) == szYear) && (szMonth == tempDate.getMonth()) && (szDay == tempDate.getDate()) )
{
oArguments.IsValid = true;
}
}
function getYear(d) {
return (d < 1000) ? d + 1900 : d;
}
Thursday, January 20, 2005
asp.net form submision hijack
In a scenario, user enters a valid age and a employee number which not present in db, so page is processed and label is set with the error message "employee not in db". Now if he enters a valid employee number and invalid age (outside the range) and tabs out, since age range is invalid both client side range validator error and server side error messages are displayed. I was told that user is confused as he knows the employee number is correct but page displays a contradicting message. {my first thoughts is "Oh yeah... so..." } and was asked to remove the message before form is submited.
Initially thought of hijacking the asp.net form submission {for those who like to see how its done, i have included the script } but wrote a client script to clear the label and register using Page.RegisterOnSubmitStatement, since that is fired only when form is submitted, the on-blur of the age control doesn't invoke the script. I didn't had a clue if i can wire client events and when i found the option, i wrote a script to clear the server message.
//Wire the method to events
document.body.attachEvent('onkeydown',ClearServerMessage);
document.body.attachEvent('onmousedown',ClearServerMessage);
// Will hold the Label control client id
var strHTMLElementForServerSideMessage;
function ClearServerMessage(e){
//get the ServerSideMessage HTML element
strHTMLElement = document.getElementById(strHTMLElementForServerSideMessage);
//Clear only when the user does something with validation control elements, such as textbox's,.. if((window.event.srcElement.tagName == "INPUT")
|| (window.event.srcElement.tagName == "SELECT")
|| (window.event.srcElement.tagName == "A") )
{
//Check if not null and element of type SPAN, then clear off message
if( (strHTMLElement != null) && (strHTMLElement.nodeName == 'SPAN') )
{
strHTMLElement.innerHTML = "";
}
//UnWire the method from events, so it won't be fired again
document.body.detachEvent('onkeydown',ClearServerMessage);
document.body.detachEvent('onmousedown',ClearServerMessage);
}
}
//code behind
string szClrScript = "<script language=JavaScript>strHTMLElementForServerSideMessage=\"" + LabelServerMessage.ClientID + "\";</script>";
if(!Page.IsClientScriptBlockRegistered("szClrSCript"))
Page.RegisterClientScriptBlock("szClrScript", szClrScript);
The fun, form submission hijack script,
<script language="javascript">
// save the original function pointer of the .NET __doPostBack function in a global variable netPostBack
var netPostBack = __doPostBack;
// replace __doPostBack with your own function
__doPostBack = EscapeHtml;
function EscapeHtml (eventTarget, eventArgument)
{
// execute your own code before the page is submitted
document.all." + HtmlText.ClientID + ".value = escape(document.all." + HtmlText.ClientID + ".value);
// call base functionality
return netPostBack (eventTarget, eventArgument);
}
</script>
Friday, January 14, 2005
Thanks for all those who keep the divine language from being extinct. Kudos Project Madurai
Thursday, January 13, 2005
repeou nisbum
"News from Repeou:
The country Repeou threatens Microsoft to pay a fine as much as 10 percent of its global annual sales for monopoly defenses. The software giant is abusing its monopoly power by bundling several applications such as the Calculator and Paint with Windows.
The process against Microsoft was started by the company named Nisbum. Nisbum developed a great calculator but doesn't see a chance to sell this great product to the masses as long as Microsoft bundles the Calculator with Windows.
According to Repeou, Microsoft must offer at least two separate versions of Windows, one version without the Calculator.
Repeou is giving Microsoft a last opportunity to comment before the case is concluded."
After 24 hrs i could succesfully de-scramble the words "repeou nisbum" -> "europe ibm sun", a good prank on the recent ruling against Microsoft by European Union.
Tuesday, January 11, 2005
MCSD.net...

https://partnering.one.microsoft.com/authenticate/MCPCredentials.aspx
Transcript ID: 690126
Access Code: isasmcsd
70-315 Developing and Implementing Web Applications,
70-316 Developing and Implementing Windows-based Applications,
70-320 Developing XML Web Services and Server Components,
70-300 Analyzing Requirements and Defining .NET Solution Architectures &
70-340 Implementing Security for Applications
After 2 months of preparation and 20+ hrs of travel to/fro Sioux Falls, SD exam center, i am finally certified. Keeping the debate of worthness of the certification apart, the excercise proved to be a good test, to see where i stand in the crowd. With thousands of dumpsters, I am really skeptical of the certificate credits, but at end of the day, knowledge gained is all that matters and i am really satisfied what i gained from this certification excercise.
Monday, January 03, 2005
OpenXML and index scan issue
CREATE TABLE OpenXMLTest3
(
Col1 INT NOT NULL primary key,
Col2 CHAR(1) NOT NULL,
Col3 CHAR(10)
)
GO
CREATE INDEX IDX_OpenXMLTest3 ON OpenXMLTest3
( Col2, Col3 ) WITH FILLFACTOR = 90 ON [PRIMARY]
GO
Execution plan for the following code gives a index scan on IDX_OpenXMLTest3, but if we load a lot of data into this table, and get the execution plan, it shows a clustered index scan on primary key index. ???? Yes on the "primary key index" ????
DECLARE @hDoc INT
EXEC sp_xml_preparedocument @hDocOUTPUT, '?'
SELECT * FROM OpenXMLTest3 A,
OPENXML (@hDoc, 'ROOT/OpenXMLTest3',1)
WITH (COL2 CHAR(1), COL3 CHAR(10)) B
WHERE A.COL2 = B.COL2 AND
A.COL3 = B.COL3
When we dump the xml data into a table variable and join with that like one below, it was found to give an index seek on IDX_OpenXMLTest3. That is what we used to fix this isse as the table we are joining is a very big invoice table and OpenXML failed hands down.
DECLARE @Temp table (
Col2 CHAR(1) NOT NULL,
Col3 CHAR(10)
)
SELECT * FROM OpenXMLTest3 A, @Temp B
WHERE A.COL2 = B.COL2 AND
A.COL3 = B.COL3
Friday, December 31, 2004
p&p Data Access Application Block - Review
Almost all the sites will be portraying the salient benefits of this application block (AB), so I decided my review to hint on the issues {my perception} and enhancements needed. Year back I did a feature analysis with Ver 1.0 of the AB. After a year I decided to look back into this AB to see if there are any major improvements or feature additions. For this I used both the Ver 2.0 release from Microsoft and Ver 3.1 release from GotDotNet (GDN) workspace. There were few enhancements in GDN version, but it is anyone’s guess why Microsoft hasn’t take clue from GDN version.
I list out what i feel some of the missing features:
Critical
Resource clean-up
This one is very severe, there are many places where and object is created and not cleaned up properly, for example a SQLConnection object is created and some command is executed, but there is no clean-up of connection when there is an exception. A SQLCommand object created is not cleaned up anywhere. I still see lot of open issues in GDN bug tracker.
Command time-out
Though looks like a simple feature, this actually helps the system scalability. Nobody would like to end up figuring out long running queries and to fix scalability issue at a fag end of a project. Rather this should be configurable from application and also a way to set configurable max limit for each connection.
Logging execution information
It is critical for any data access layer to log the excution information like executing query, parameters and exception. This should be configurable for a connection both from application and externally, also it should be having multiple levels like TraceSwitch.
Support and Extensibility to any data store
Microsoft version is written very specific to SQLServer and designed with very less flexibility to extensions to other data store. GDN version has taken the first step in this by using an abstract ADOHelper.cs, but there is a long way to go.
Abstraction of ADO.Net implementation
I would still like to see more abstraction from ADO.net workings. Still developers need to work with connection and parameter objects in many situations. I would like to see helper methods for parameter creation that hides ADO.net implementations.
Non-Critical
Single store for connection string information
I learnt this from a client I worked with, the concept of having a logical data source and maintaining connection information for that in an external store {registry, xml config} is advantageous in many facets. First an application will not be able to arbitrarily connect to any data store. You can enforce constraints for a logical data source, like max command time out, you will be happy to see that you are not allowing a command to run for hours against a real-time db. Without any code/config changes, we can point an application to appropriate environments like dev/qa/prod.
Design flexibility & extensibility
The static nature of the design might suit very well for some situations.But I often end up in situation where I needed to store information specific to a connection like a transaction object, thus I feel an object would be more convenient for a data access block. But looking at all other application blocks, it seems obvious that Microsoft wants to go all out static design. Also the extension for other data store is not easy with the existing design.
Application configuration file issues in COM+ environment
GDN version is now using app.config for storing provider information. Having app.config for server-activated serviced component assembly means managing the app.config is going to be complex. With SOA wave all around us, this issue is now needs more attention. COM+ 1.5 provides an option of having an individual app.config for each server application package, better than a single app.config for all server-activated serviced components. But still managing these app.config files is still going to be tough; it’s not anymore a just XCOPY.
Existing feature’s of runtime discovery of stored procedure parameters, I would not recommend unless there is a very strong reasons to use this. Idea of parameter cache looks good, but practically I haven’t had a chance to use this feature, that too with help of OpenXML that use looks remote to me.
Everyone should agree that p&p application blocks are designed to be extensible and hence all of the missing features mentioned can be included by customizing the block. But the question is how much customization you need. I neither offend nor defend the usage of this application block; it depends on the architectural requirement. More than anything this component is an integral part of your enterprise architecture.





