Archive for the ‘.Net’ Category

Ghost Process Writing to Visual Studio 2008 Debug Immediate Window

November 4, 2009

If you’ve never seen something like this, you might not believe it.

In debugging a website running under, I had debug.print lines from another website appearing in the immediate window.
They stopped if:
1) I shut down IIS (of course, that killed my debugging as well)
2) I detached the asp_wp worker process from the debugger (and then I couldn’t debug my own project)
3) killing the asp_wp processes from Task Manager gave a brief respite, but the messages started again once the worker process was reactivated.

What I finally did, and this is a temporary fix, is to luckily guess the offending website under IIS, and rename the directory. Now, that website is not able to be browsed.

I had only one instance of IE or any other browser open during the time when I was trying to fix the problem. (generally I have many open)

I suspected some scheduled process browsing to the website – that is still a possibility.
Also, I suspect a coding practice of mine, which is to use some debug logging code in static (vb – shared) functions. Perhaps IIS is for some reason opening this virtual directory? (I know I said web site above, but actually I mean a virtual directory under the same website running in the same IIS .Net 2 process.)
Also, the virtual directory in question was the last one in alphabetical order of all my virtual directories.

I did not search Knowledge Base about this. It’s something I’ve lived with for weeks, at least, and just ignored, until this AM.

asp.net 1.1 set page title in code

October 29, 2009

This is easy:
in aspx

in form load:
sParam = Request.Params(“strkey”) & “”
‘Me.HtmlTitle.TagName = sParam
Session(“title”) = sParam

web service to return html , javascript converts return string

September 18, 2009

I had to make a kind of “quote of the day” web service. Then I changed it so that the returned text could be html formatted instead of just plain text.

That caused a problem for me, because while the html saved in my database looked good (bolds and line feeds etc), when my calling routine got the text out of the web service, it converted all the less than signs to < and the greater than signs to >.

I don’t know if this is a good solution, but here’s what it does:
INPUT STRING:

<?xml version=”1.0″ encoding=”utf-8″?>
<string xmlns=”http://tempuri.org/”>Yabba Dabba Doo.
&lt;br&gt;&lt;br&gt;

&lt;bold&gt;Fred Flintstone&lt;/bold&gt;</string>

OUTPUT STRING:

Yabba Dabba Doo.
<br><br>

<bold>Fred Flintstone</bold>

Here’s the routine:

 


// ---------------------------------------------------
// purpose - does a simple parse of the xml response to get the string we're interested in
function ajaxws_get_response_html(sXML) {
var sResponseText;
var iFirst = sXML.indexOf('org/">') + 6;
var iLast = sXML.indexOf('', iFirst);
sResponseText = sXML;

if (iFirst > 0 && iLast > 0) {
var iLength = iLast - iFirst;
sResponseText = sXML.substr(iFirst, iLength);
}

sResponseText = sResponseText.replace(/</g, '');
return sResponseText;
}

 

Here is where it fits into the set of routines to call a web service from the browser:

 

 

// purpose – ajax like routines to get data from the web service
var xmlhttp = null; // the init function will set this, it is the xml object that calls the server side function

var sReturned_Data;
sReturned_Data = ”;

var url_base = ”;
url_base = ‘ws/wsquotes.asmx/GetQuote’; // syntax for non-soap url
var debugjs;
debugjs = 0; // set the debug variable to 0 to turn off the alert messages
if (debugjs > 3) alert(‘page loaded, ajax_updater’);

// —————————————————
// purpose – try to guarantee that the activex object gets instantiated
// if ajax is not initiated when we call it the first time, then this sub makes sure that it gets initiated
function ajaxws_Init() {
try {
// Mozilla / Safari
xmlhttp = new XMLHttpRequest();
}
catch (e) {
// Explorer
var _ieModelos = new Array(
‘MSXML2.XMLHTTP.5.0′,
‘MSXML2.XMLHTTP.4.0′,
‘MSXML2.XMLHTTP.3.0′,
‘MSXML2.XMLHTTP’,
‘Microsoft.XMLHTTP’);

var success = false;
for (var i = 0; i 0) alert(‘ajaxws_CallWebServiceUpdater – id:’ );
if (debugjs > 0) alert(‘ajaxws_CallWebServiceUpdater – url:’ );

xmlhttp.open(“GET”, url, true);
xmlhttp.onreadystatechange = ajaxws_stateChanged; // http://snippets.dzone.com/tag/xmlhttp
xmlhttp.send(null);
// http://www.devguru.com/technologies/xmldom/QuickRef/obj_httpRequest.html
}

// —————————————————
// purpose – called by ajax event
function ajaxws_stateChanged() {
if (debugjs > 0) alert(‘ajaxws_stateChanged ‘);

if (xmlhttp.readyState == 4 || xmlhttp.readyState == “complete”) {
sReturned_Data = ajaxws_get_response_html(xmlhttp.responseText);
}
}

// —————————————————
// purpose – does a simple parse of the xml response to get the string were interested in
function ajaxws_get_response_text(sXML) {
return sXML;
var sResponseText;
var iFirst = sXML.indexOf(‘/”>’) + 3;
var iLast = sXML.indexOf(‘ 0 && iLast > 0) {
var iLength = iLast – iFirst;
sResponseText = sXML.substr(iFirst, iLength);
}
return sResponseText;
}

// —————————————————
// purpose – does a simple parse of the xml response to get the string we’re interested in
function ajaxws_get_response_html(sXML) {
var sResponseText;
var iFirst = sXML.indexOf(‘org/”>’) + 6;
var iLast = sXML.indexOf(”, iFirst);
sResponseText = sXML;

if (iFirst > 0 && iLast > 0) {
var iLength = iLast – iFirst;
sResponseText = sXML.substr(iFirst, iLength);
}

sResponseText = sResponseText.replace(/</g, ”);
return sResponseText;
}

(more…)

when visual studio debugging doesn’t work right

August 26, 2009

I have had issues both with visual studio 2008 using cassini and even also with visual studio 2003!

Here is the issue:
I need to trace into some code in a code-behind web page, but visual studio decides not to trace to my breakpoint, but just displays the page.

I think the root cause is related to:
1) multiple instances of aspnet_wp.exe that seem to somehow get created (I run the debugger literally hundreds of times a day, and the extra cassini instances do happen)
2) weird hidden iexplore.exe processes that are left hanging out there messing around with my new debug instance
3) some weird state that my local iis has gotten into (especially for 2003)

And that’s about the extent of the theory that I understand.

Fixes:
1) restart iis with net stop w3svc, net start w3svc (for 2003)
2) killing the unwanted cassini instances
3) killing iexplore.exe processes from task manager (this means you have to listen to pandora with Firefox!)
4) and/or restarting visual studio

mvc – RouteTable not defined

July 7, 2009

It’s kind of a bummer when you try to make you’re first asp.net mvc app, just select the defaults, and get a lot of weird errors.
For me, in global.asax, RouteTable and RouteCollection were not defined.
1) I noticed in comparing my global.asax to an example I found somewhere that the following lines were missing from mine:
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.Mvc
Imports System.Web.Routing

2) but to get system.web.routing to be valid, I had to include some references. This is what I found at
(http://www.packtpub.com/article/mixing-asp.net-webforms-and-asp.net-mvc)
“add a reference to the following three assemblies to your existing ASP.NET application:”

System.Web.Routing
System.Web.Abstractions
System.Web.Mvc

So now my “hello world” app actually builds.

Asp.Net MVC template problem

July 6, 2009

Text of the problem (as I type it in):
Error: this template attempted to load an untrusted component ‘Microsoft.VisualStudio.Web.Extensions, Version=9.0.0.0,Culture=neutral, PublicKeyToken=31bf3856ad364e35′. etc.

Resolution: downloaded and installed Asp.Net MVC 1.0 from http://www.microsoft.com/downloads/details.aspx?FamilyID=53289097-73ce-43bf-b6a6-35e00103cb4b&displaylang=en

Root Cause? perhaps I had a beta already installed that I had never attempted to use before.

html checkbox in 1.1 datagrid bound to data

May 20, 2009

If you’re used to .net server controls, you’ll probably want to set the checked property on a checkbox control to true. Well that won’t work.
It displays either checked or not checked depending on whether the string checked is one of its attributes.

Here is a way to make it happen in a templated column in a datagrid in old-school .net 1.1:


1) the templated column

<input type="checkbox" onclick="ajaxws_OnCheckBoxChanged(this)"
id='chkgrid$tDownloads$isHomeUse$sdKy$'

>

a) the line must be either the
string "checked" (without the quotes), or nothing at all. You cannot set checked=1, or checked=true, etc.
b) NOTE - we don't have quotes around the stuff, so you can not edit the page in design mode

2) therefore we convert the boolean to a string with a case statement when we do the database query:
(also take care for isNull)
CASE isNull(isHomeUse, 0)
WHEN 1 THEN 'checked'
ELSE ''
END AS isHomeUse

Note: I’m not so good at getting the Eval syntax to work. I don’t like returning an html string from my sql query, but at least this way works. I’d be interested in hearing from someone who can improve on this.
But I won’t hold my breath, since this code seems so 2002.

function to return the current value and id of all controls

May 14, 2009

I ended up not using this yet, but it returns a dictionary collection of all controls contained in a page, for example, and their current value. This includes textboxes, dropdowns, radios, and checkboxes.
I usually credit the original source for functions like these, but I lost where the recursive sub came from

' **************************************************************************
' purpose: function to call the recursive sub and then return the dictionary containing the results
Public Shared Function get_all_controls(ByVal container As Control) As Hashtable
htControls.Clear()
Dim strTemp As String = container.ID & " " & container.ClientID
strTemp = strTemp + container.UniqueID + " " + container.Page.Title
Diagnostics.Debug.Print(strTemp)
get_all_controls_recursive(container)
Return htControls
End Function

' **************************************************************************
' recursive sub to list all controls in a container
Public Shared Sub get_all_controls_recursive(ByVal container As Control)
Dim strTemp As String
Dim bFound As Boolean = False

For Each ctl As Control In container.Controls

bFound = False

strTemp = ctl.ID

If (Not bFound) Then
Dim txt As TextBox = TryCast(ctl, TextBox)
If (Not IsNothing(txt)) Then
htControls.Add(txt.ID, txt.Text)
bFound = True
End If
End If

If (Not bFound) Then
Dim ddl As DropDownList = TryCast(ctl, DropDownList)
If (Not IsNothing(ddl)) Then
htControls.Add(ddl.ID, ddl.Text)
bFound = True
End If
End If

If (Not bFound) Then
Dim chk As CheckBox = TryCast(ctl, CheckBox)
If (Not IsNothing(chk)) Then
htControls.Add(chk.ID, chk.Checked.ToString())
bFound = True
End If
End If

If (Not bFound) Then
Dim rdio As RadioButton = TryCast(ctl, RadioButton)
If (Not IsNothing(rdio)) Then
htControls.Add(rdio.ID, rdio.Checked.ToString())
bFound = True
End If
End If

If (ctl.Controls.Count > 0) Then get_all_controls_recursive(ctl)

Next

End Sub

get the current page name

May 14, 2009

I wanted to get the page name of the current *.aspx page that I was in. This is what I did (not rocket science, admittedly)

' *****************************************************************************
' purpose - return the aspx page name
Public Shared Function GetCurrentPageName() As String
Dim strRet As String = ""
'Return System.Web.HttpContext.Current.Request.Url.ToString
Dim sBasePath As String = System.Web.HttpContext.Current.Server.MapPath("")
Dim sFullPath As String = System.Web.HttpContext.Current.Request.ServerVariables("PATH_TRANSLATED")
sBasePath = sBasePath.ToLower() + "\"
sFullPath = sFullPath.ToLower()
strRet = sFullPath.Replace(sBasePath, "")
Return strRet
End Function

change the style at the server

May 14, 2009

I made a user control, and I wanted to add a ctl_Width property and apply the value to all the contained sub-controls.
But setting the “Width” property of each did not work – I had to change the style of each. So I made some functions to help me in setting the style attribute of each of the sub controls contained in my user control. Here are the functions:

' ------------------------------------------------------------------------------
' purpose - set some style at the server side
' how to call: iRet = SetStyleValue(ddlCombo, "width", sWidth, strError)
' ref: http://codebetter.com/blogs/brendan.tompkins/archive/2003/10/23/2827.aspx
Public Shared Function SetStyleValue(ByVal o As System.Web.UI.WebControls.WebControl, ByVal sKey As String, ByVal sVal As String, Optional ByRef strError As String = "") As Integer
Try
o.Style.Add(sKey, sVal)
Return 1
Catch ex As Exception
strError = ex.Message
Return -666
End Try
End Function

' *********************************************************************
' purpose - get the current style string for the control
' ref:http://codebetter.com/blogs/brendan.tompkins/archive/2003/10/23/2827.aspx
' how to call:
Public Shared Function GetStyleString(ByVal o As System.Web.UI.WebControls.WebControl) As String
Dim strStyle As String = ""
Try
Dim S As System.Web.UI.CssStyleCollection = o.Style
For Each key As String In S.Keys
strStyle += key + ":" + S(key) + ";"
Next
Catch ex As Exception
strStyle = "ERROR"
End Try
Return strStyle
End Function