Interesting tech stuff I come across day-to-day. Mainly around Biztalk and .NET 3.0

ASP .NET Treeview Control, weird problem when you Programmatically expand the nodes.

Wednesday, May 23, 2007

One of my supporting project required building a web site using TreeView control to show hierarchical data. I used "NavigateUrl" property while data binding, so that every node in the tree view will have proper link something like http://abc.com/bts/default.aspx instead of Javascript:__doPostBack. Even though all of my pages are going to have the treeview control on the left, I'll lose the expanded status because the control will be navigated to a brand new page and whole page will be loaded from scratch. So, my requirement is to set the treeview expanded status programmatically.

What do you think of this line of code?

TreeView1.FindNode("BizTalk Server|Planning and Architecture|Patterns").Expand();

("|" is the path separator)

Perfectly alright right? But, the output will only expand the node till "Planning and Architecture", I tried different things like putting the code in different event handlers like page_prerender, treeview_databound, navigating to the node via Nodes and ChildNodes property etc, etc. Whatsoever I couldn't make it work. Google search revealed similar problems but no solution. At last I managed to find the solution from the book "Professional ASP .NET 2.0" (Page: 525) the code should be written this way:

TreeView1.FindNode("BizTalk Server").Expand();

TreeView1.FindNode("BizTalk Server|Planning and Architecture").Expand();

TreeView1.FindNode("BizTalk Server|Planning and Architecture|Patterns").Expand();

Extract from the book:

"Note that you had to expand each of the nodes individually until you got to the "Planning and Architecture" node, If you simply used TreeView1.FindNode("BizTalk Server|Planning and Architecture|Patterns").Expand(); in the treeview1_DataBound method, the "Pattern" node would indeed be expanded, but the parent nodes above it ("Planning and Architecture" and "BizTalk Server") would not have been expanded and you wouldn't see the expanded "Patterns" node when invoking the page. (Try it; it's interesting.)"

What a weird programming style they have adapted for this? Is it not just common sense the control should expand all its parent node to show itself? The last sentence "Try it; it's interesting" is actually present in the book. I was almost about to buy a commercial product, just thinking there is a huge bug in the ASP .NET 2.0 Treeview control.

Nandri!

Saravana

Labels:




Create safe File Name from string using .NET 2.0

Saturday, May 19, 2007

Recently for one of my weekend project I wanted to create a safe file name from a string. In the past people used to do lot of regular expressions and conditional testing to meet this requirement. Still the filename won't be safe across multiple environments (Windows 2000/XP/2003 etc). But with .NET 2.0 using couple of inbuild functions (Path.GetInvalidFileNameChars and Path.GetInvalidPathChars) you can create safe filenames. Hope this piece of code will be useful to someone.

private string CreateValidFileName(string title, string extension)
{
string validFileName = title.Trim();

foreach (char invalChar in Path.GetInvalidFileNameChars())
{
validFileName = validFileName.Replace(invalChar.ToString(), "");
}
foreach (char invalChar in Path.GetInvalidPathChars())
{
validFileName = validFileName.Replace(invalChar.ToString(), "");
}

if (validFileName.Length > 160) //safe value threshold is 260
validFileName = validFileName.Remove(156) + "..."

return validFileName + "." + extension;
}

Nandri!

Saravana

Labels:




How to create an invisible .NET Application?

Monday, February 05, 2007

Formless application or in my terms invisible .NET applications. I recently end up in a situation where I want to run my console application on a regular schedule via Scheduled task. Problem with that is, it opens ups the schedule every time it runs the application.

There were few suggestions in the newsgroups to create a Windows NT service, or WinForms application and make the form invisible etc, etc. None of them were appropriate for me.

At last I ended up with a quick and efficient fix.

Go to project properties and set "Output Type" to "Windows Application" for your Console application.

Now, I can't see the console anymore, and the application runs in the background quietly.

Nandri!

Saravana

Labels:




GAC your assemblies with one click!!

Tuesday, December 19, 2006

We BizTalk developers often get frustrated with this assembly GACking stuff. Here is a cool registry setting which will make our life little bit easier.

Copy and paste the code into a notepad file and save it with extension .reg. Double click on the file and its all done.

-------------------------------------------------------

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\dllfile\shell\GAC-It\command]
@="c:\\windows\\Microsoft.NET\\Framework\\v1.1.4322\\gacutil.exe /i \"%1\""

-------------------------------------------------------

Then open explorer and navigate to your dll, right click on it, for your surprise you'll see a shortcut called GAC-It as shown below. You can put whatever name you want as highlighted in RED in the above script.

Found it from here

Labels:




System.Diagnostics.Process, RedirectStandardOutput process hanging

Thursday, May 25, 2006

In one of our internal web app's developement, we were executing an console applications with certain arguments synchronously and were trying to display the results in the front-end.

When we activate the process I can see the process getting kicked-off in the task manager and it never exits, after a time period (IIS timeout setting), the web app times out.

At the end we figured out, the reason is due to an dead lock condition between the parent process (w3wp) and the child process(console app).

Synchronous read operations introduce a dependency between the caller reading from the StandardOutput stream and the child process writing to that stream. These dependencies can result in deadlock conditions. When the caller reads from the redirected stream of a child process, it is dependent on the child. The caller waits on the read operation until the child writes to the stream or closes the stream. When the child process writes enough data to fill its redirected stream, it is dependent on the parent. The child process waits on the next write operation until the parent reads from the full stream or closes the stream. The deadlock condition results when the caller and child process wait on each other to complete an operation, and neither can proceed.

Solution is really simple, look at the code snippet below.

// Start the child process.
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "Write500Lines.exe";
p.Start();
// Do not wait for the child process to exit before
// reading to the end of its redirected stream.
// p.WaitForExit();
// Read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();


BTW, most of explanation is from MSDN, but it took me a while to figure out.

Labels: