Manipulation with cells, generated by GridView DataControlField derived classes.

Sometimes you need to customise DataControlField derived field (e.g. HyperLinkField or ButtonField )to show it differently then default behavior, but you don’t want to create template for the field. If you customization requirements are compex, use TemplateField.
Below there is an example, how you can access cell, generated by  HyperLinkField and change content of the cell.
E.g  for HyperLinkField column you normally  want to show date as Hyperlink, but if date is in past,show text.  

protected void gvMyGVName_RowDataBound(object sender, GridViewRowEventArgs e)
{

 

    //The method is created as an example and  was never tested(and even compiled)

    if (e.Row.RowType == DataControlRowType.DataRow)

    {

        DataRowView drv = e.Row.DataItem as DataRowView;

        DataRow dataRow = drv.Row;

 

        if((DateTime)(dataRow[“MyDateColumn”])<=DateTime.Now)

        {

            DataControlFieldCell cell = GridViewHelper.CellBySortExpression(e.Row, “MyDateColumn”) as DataControlFieldCell;

            GridViewHelper.DataControlField_SetNullDisplayText(cell, “Expired”);

        }

    }

 

}  

    //Function belongs to my GridViewHelper class.

 

        /// <summary>

        /// Hide link and add text control

        /// </summary>

        /// <param name=”cell”></param>

        /// <param name=”nullDisplayText”></param>

        public static void DataControlField_SetNullDisplayText(DataControlFieldCell cell, string nullDisplayText)

        {

 

        //TODO: implement derived from HyperLinkField class and set nullDisplayTex property

            //       TableCell cell = (TableCell) control;

            Debug.Assert((cell.ContainingField is HyperLinkField) || (cell.ContainingField is ButtonField));//

            if(cell.ContainingField is HyperLinkField)

            {

                if (((cell.Controls.Count < 1) || !(cell.Controls[0] is HyperLink)))

                {

                    throw new ApplicationException(“HyperLinkField_WrongControlType”+ cell.ToString());

                }

                HyperLink link = (HyperLink)cell.Controls[0];

                link.Visible = false;

            }

            if(cell.ContainingField is ButtonField)

            {

                if (((cell.Controls.Count < 1) || !(cell.Controls[0] is LinkButton)))

                {

                    throw new ApplicationException(“ButtonField_WrongControlType” + cell.ToString());

                }

                LinkButton link = (LinkButton)cell.Controls[0];

                link.Visible = false;

            }

            Label label = new Label();

            label.Text = nullDisplayText;

              label.Visible = true;

           cell.Controls.Add(label);

        }

My Javascript Tips

Check if object is null or undefined.

I had an understanding that to check if variable not null in JavaScrip, you need to check

if (typeof SomeObject !=’undefined’) .

But it is wrong, because null is not the same as ‘undefined’.

From http://weblogs.asp.net/bleroy/archive/2005/02/15/Three-common-mistakes-in-JavaScript-_2F00_-EcmaScript.aspx

The shortest way to deal with this, and also the one that best expresses your intention of checking if an object is safe to use is probably to just rely on the type-sloppiness of JavaScript and count on it to evaluate null and undefined as false in a boolean expression, like this:

if
(SomeObject.foo) {

My other Javascript related posts:

Javascript variable declaration scope is different from C#.

Javascript to re-calculate summary based on user drop-down lists selections

Differences in Internet Explorer and FireFox CSS and Javascript

Helper function to Print Page using JScript(including inside frame)

My JScriptHelper class

Issues converting Visual Studio 2005 Projects to Visual Studio 2008.

 
There were a few Web Application Projects that failed to convert without any reason.
The Upgrade file was just like this:   
 
<?xml-stylesheet type=’text/xsl’ href=’_UpgradeReport_Files/UpgradeReport.xslt’?>
<UpgradeLog> <Event ErrorLevel=3 Project=TSA.Testing.AssemblyTest2 Source=mainTSATestingTSA.Testing.AssemblyTestTSA.Testing.AssemblyTest2.csproj Description=Not Converted>
</Event></UpgradeLog>

One of the project on my local machine had the following line
  <Import Project=”$(MSBuildExtensionsPath)MicrosoftVisualStudiov8.0WebApplicationsMicrosoft.WebApplication.targets” Condition=”” />

When I removed Condition=””,  it was successfully converted to 2008.

For other projects we were not able to find the reason of failure, and we created blank Web Application Projects and import existing files, that belonged to original projects.

  Another issue was that projects with XSD files had generated files modified and corrupted. I found that this issue happens on my machine only, but not  on other machines. I’ve noticed that Xsd files have XsdCodeGen Custom tool specified in the project.My XsdCodeGen has been install a long time ago from  code samples of Code Generation in the .NET Framework Using XML Schema(http://msdn.microsoft.com/en-us/library/aa302301.aspx).Conversion Wizard invoked the XsdCodeGen and tried to re-generate c# code , but failed(It seems that XsdCodeGen doesn’t work with includes) .

I’ve uninstalled the XsdCodeGen from my machine and generated files were left unmodified.

If Web Reference URL in the Setings(and APP.Config) is not correct(e.g. out of date), Conversion Wizard tries to re-generate Web Service client. It doesnt report an error, but subsequent comilation failed:

Error 1 Custom tool error: Unable to import WebService/Schema. The object ‘Settings’ already contains a property … not of type WebServiceUrl. in Reference.map 

 It is important to ensure, that Web Reference URL is correct. I beleive that Conversion Wizard should NOT re-generate clients, or at least it should be optional. 

Setup Project Issues

After re-creating of a new VS 2008 WAP project, My setup project failed with the error:
ERROR: Unable to build project output group ‘Content Files from WebApplicationName
Despite the APPLIES TO section(referring to VS2002-2003), it is still valid for VS 2008, and Microsoft didn’t add any output to point, which file are missing, As recommended by Scott Hanselman, you still have to turn on ‘Show All Files‘ and open EVERY folder until you find one with the Yellow Yield Icon.
There was a thread http://channel9.msdn.com/forums/TechOff/259614-VS2008-Web-Setup-Project-Build-Fails/ that looked relevant to my error, but actually wasn’t applicable to my problem.
Also my setup projects were broken, because they refer to old WAP projects. I had to remove old “Project outputs” and re-add new ones.

Generics function to work with the list of rows of Typed Dataset

I wanted to create common function, that will work with the list of rows of Typed  Dataset, e.g. List<MyTypedDataSet.MyTableDataRow). 
I’ve created a function that expects as a parameter List of base DataRow objects, i.e. List<DataRow>. But compiler showed error, that it unable to cast List<MyTypedDataSet.MyTableDataRow) to List<DataRow>. It does make sense, but what are the valid options?
The one, that I’ve used, is actually collect MyTableDataRow into the list of base DataRow records.
E.g use something like the following
List<DataRow> rows= new List<DataRow>();
rows.Add(myTableDataRow); 
 
Another option is to create generic function, where typed parameter has constraint-derived from base DataRow class.
Below are tho methods, that implement 2 possible options.
        public static void DeleteRows(List<DataRow> rows, bool hideExceptions)
        {
            foreach (DataRow row in rows)
            {
                try
                {
                    row.Delete();
                }
                catch (Exception exc)
                {
                    if (hideExceptions == true)
                    {                    //nothing to do
                        Debug.Assert(false, “Why ” + exc.ToString());
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }
        public static void DeleteRows<TRow>(List<TRow> rows, bool hideExceptions)  where TRow: DataRow
        {
            foreach (DataRow row in rows)
            {
                try
                {
                    row.Delete();
                }
                catch (Exception exc)
                {
                    if (hideExceptions == true)
                    {                    //nothing to do
                        Debug.Assert(false, “Why ” + exc.ToString());
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }

Use WCF Message Logging

The post based on How to enable WCF tracing article
Open WCF Service Configuration Editor under VS 2008 Tools menu   or directly from installation folder (typically C:Program FilesMicrosoft SDKsWindowsv6.0BinSvcConfigEditor.exe – subfolder v6.0 can be different depending on which version of Windows SDK has been installed)
 
To enable WCF message logging
  1. Click the Diagnostics node, and expand it.

  2. Enable Message Logging on the right Tab

  3. To edit Message Logging settings, click the Message Logging node. You can edit the settings in the property grid.
  4. In particular ensure that MaxSizeOfMessageToLog is big enough to log all your messages.
The possible options are described in Enabling Message Logging and Recommended Settings for Tracing and Message Logging. I found that it is usually good to set
         logMessagesAtServiceLevel=”true”
         logMessagesAtTransportLevel=”false”
Otherwise you see 2 duplicate entries logged at different levels.
 
 
To view trace file: 

Start Service Trace Viewer Tool (typically located at “C:Program FilesMicrosoft SDKsWindowsv6.0AbinSvcTraceViewer.exe”). 

Open message trace file, that was configured in WCF Service Configuration Editor previously(usually located in the root of your application).

 Select Message View tab, scroll to the bottom and click one of latest messages.If required press F5 to refresh list.

 In the Detail Pane (the bottom right pane in the view) use  Message  tab to see actual messages. 

If you need to trace WCF on Machine, where no Visual Studio/SDK installed, you can just copy SvcConfigEditor.exe and SvcTraceViewer.exe to the folder of your choice and run them without any
additional dependencies.
 
BTW, I’ve submitted MS Connect suggestion to Add items “Save Message As” and “Print Message” to Detail Pane