KBD

Keith Devens .com

Friday, May 16, 2008 Flag waving
Although at the beginning the paradigm was worth consideration, now the entire effort in the primeval soup paradigm is self-... – Hubert P. Yockey (Information Theory and Molecular Biology, p336)

Tag: .NET

Parents:

Children:

Page 1 →

Daily link icon Friday, January 18, 2008

  1. 15 Seconds : Tuning Up ADO.NET Connection Pooling in ASP.NET Applications.

       (0) Tags: [ASP.NET]

Daily link icon Friday, January 11, 2008

  1. ASP.NET AJAX Control Toolkit.

       (2) Tags: [ASP.NET, Javascript]

Daily link icon Tuesday, July 24, 2007

  1. John Lam on Software: A first look at IronRuby. To read.

       (0) Tags: [IronRuby]

Daily link icon Friday, February 23, 2007

  1. How To Copy DataRows Between DataTables by Using Visual C# .NET:

    Before you use the ImportRow method, you must ensure that the target table has the identical structure as the source table. This sample uses the Clone method of DataTable class to copy the structure of the DataTable, including all DataTable schemas, relations, and constraints.

       (0) Tags: [.NET]

Daily link icon Friday, February 2, 2007

  1. Ruby/.NET Bridge.

       (0) Tags: [.NET, Ruby]

Daily link icon Wednesday, November 8, 2006

  1. IronPython. The official home page of IronPython. Google is out of date.

       (0) Tags: [IronPython]

Daily link icon Wednesday, August 30, 2006

Json.NET - Newtonsoft

Json.NET - Newtonsoft, a C# implementation of JSON. Looks really good from their example... about to try it out.

Update: based on testing so far, it works great. Their example output is incorrect however. It uses single quotes, when only double quotes are allowed in JSON. But the code in fact does the right thing.

Note that the library will generate invalid JSON and include language-specific types in it, such as a date. And that's actually desirable, since if you're just using it as serialization for your own code you get what you want, and can be careful and not use language-specific types if you need to share with others.

Daily link icon Thursday, June 22, 2006

  1. String.IsNullOrEmpty can lead to runtime Null exceptions (via Adam). Nice.

       (0) Tags: [.NET]

Daily link icon Thursday, June 8, 2006

  1. Geekpedia • ASP.NET: The name 'Session' does not exist in the current context. I think this'll happen in particular if you're trying to access Session from a class defined in App_Code. Use HttpContext.Current.Session instead.

       (0) Tags: [ASP.NET]

Daily link icon Wednesday, June 7, 2006

  1. PageMethods, well-defined URLs for your ASP.NET sites by metaSapiens. Looks pretty neat. It seems to work something like the URI to object method call frameworks that exist for other languages.

       (0) Tags: [ASP.NET]

Daily link icon Tuesday, May 2, 2006

  1. MonoRail:

    MonoRail (former Castle on Rails) is a MVC web framework inspired on Action Pack. The Action Pack way of development is extremely productive, very intuitive and easily testable.

    MonoRail differs from the standard WebForms way of development as it enforces separation of concerns; controllers just handle application flow, models represent the data, and the view is just concerned about presentation logic. Consequently, you write less code and end up with a more maintainable application.

       (0) Tags: [ASP.NET, Ruby on Rails]
  2. Cassini Web Server - Free Redistributable ASP.NET Web Server from UltiDev LLC.

       (0) Tags: [ASP.NET]

Daily link icon Monday, April 17, 2006

How to do clean URIs in your ASP.NET application

It's actually very simple to do clean URIs in an ASP.NET application. In your Global.asax file:

string root = "/rootofyourapp";

void Application_Start(object sender, EventArgs e){
    Dictionary<string,string> mapping = new Dictionary<string,string>();
    mapping[root+"/page"]=root+"/page.aspx";
    Application.Add("mapping",mapping);
}

void Application_BeginRequest(object sender, EventArgs e){
    HttpContext context = HttpContext.Current;
    string path = context.Request.Path;
    Dictionary<string,string> mapping =
        (Dictionary<string,string>)Application["mapping"];
    
    if(mapping.ContainsKey(path)){
        context.RewritePath(mapping[path]);
    }else{
        Response.AddHeader("Content-type","text/plain;charset=UTF-8");
        Response.Write("Invalid request: "+path+"\n\n");
        Response.End();
    }
}

And to forward all requests through to a single URI:

void Application_BeginRequest(object sender, EventArgs e){
    HttpContext.Current.RewritePath("~/Default.aspx");
}
  1. MySpace does 1.5 billion page views per day using ASP.NET 2.0. They just switched from ColdFusion, a Java based technology, and their server load went down dramatically. Of course it's not surprising that switching from Java would make your app run faster, but what makes this so ironic is that they're still largely using ColdFusion, but using a .NET-based reimplementation called BlueDragon. So, a .NET-based reimplementation of ColdFusion runs dramatically faster than the original Java-based implementation.

       (2) Tags: [.NET, ColdFusion, Java]

Daily link icon Friday, April 14, 2006

  1. ViewState Decoder (2.1) by Fritz Onion. Also see this article by Paul Wilson (who apparently used to have a similar tool online that's no longer available) about ASP.NET's viewstate process (to read).

       (0) Tags: [ASP.NET]

Daily link icon Tuesday, April 4, 2006

Pretty-print .NET objects?

Does anyone know of a way to pretty-print .NET objects? We've covered before how .NET objects don't stringify into anything good, so I'm looking for a pretty-printer to aid in debugging. .NET has such good reflection that I'm surprised I can't find a pre-existing library or built-in .NET facility. Do I have to take a half hour and write my own pretty-printer?

Update (2:29pm): There we go, this only took me about 20 minutes:

using System;
using System.Text;
using System.Reflection;

class foobar{
    string foo, bar, baz;
    bar b = new bar();
}
class bar{
    string roar, blah;
}

namespace PrettyPrinter{
    public class PrettyPrinter{
        public static void Main(string[] args){
            foobar f = new foobar();
            Console.WriteLine(pp(f));
            int i = 2;
            Console.WriteLine(pp(i));
        }
        public static string pp(object o){
            StringBuilder sb = new StringBuilder();
            pp(sb, o, 0);
            return sb.ToString();
        }
        public static void pp(StringBuilder sb, object o, int level){
            Type t = o.GetType();
            FieldInfo[] members = t.GetFields(
                        BindingFlags.Public
                        | BindingFlags.NonPublic
                        | BindingFlags.Instance
            );

            foreach(FieldInfo info in members){
                object val = info.GetValue(o);
                sb.Append('\t',level);
                sb.Append(info.ToString()).Append(": ");
                if(val == null){
                    sb.AppendLine("null");
                }else if(info.FieldType.IsClass){
                    sb.AppendLine("{");
                    pp(sb, val, level+1);
                    sb.Append('\t',level).AppendLine("}");
                }else{
                    sb.AppendLine(o.ToString());
                }
            }
        }
    }
}

Output:

C:\DEV\Projects\test\bin\Debug>test
System.String foo: null
System.String bar: null
System.String baz: null
bar b: {
        System.String roar: null
        System.String blah: null
}

Int32 m_value: 2

Update (4:57pm): Since of course that was a first pass, here's a more correct version that handles circular references, and for Keith also dumps XML if you happen to pass it an XmlNode Smiley

using System;
using System.Xml;
using System.Text;
using System.Reflection;
using System.Collections.Generic;
using pp = PrettyPrinter.PrettyPrinter;

namespace PrettyPrinter{
    public class PrettyPrinter{
        private static Type str = typeof(string);
        private static Type xml = typeof(XmlNode);

        public static string pp(object o){
            if(o == null)
                return "null";

            StringBuilder sb = new StringBuilder();
            sb.Append(o.GetType()).Append(": ");
            pp(new Dictionary<object, bool>(), sb, o, 0);
            return sb.ToString();
        }

        public static void pp(
                Dictionary<object, bool> seen_objects,
                StringBuilder sb, object o, int level
        ){
            Type t = o.GetType();
            if(t == str || t.IsValueType){
                sb.AppendLine(o.ToString());
            }else if(xml.IsInstanceOfType(o)){
                sb.AppendLine(((XmlNode)o).OuterXml);
            }else if(t.IsClass){
                if(seen_objects.ContainsKey(o)){
                    sb.AppendLine("RECURSION!");
                    return;
                }
                seen_objects.Add(o, true);
                FieldInfo[] fields = t.GetFields(
                    BindingFlags.Public
                    | BindingFlags.NonPublic
                    | BindingFlags.Instance
                );
                sb.AppendLine("{");
                foreach(FieldInfo info in fields){
                    object val = info.GetValue(o);
                    sb.Append('\t',level+1).Append(info).Append(": ");
                    if(val == null){
                        sb.AppendLine("null");
                    }else{
                        pp(seen_objects, sb, val, level+1);
                    }
                }
                sb.Append('\t',level).AppendLine("}");
            }
        }
    }
}

class foobar{
    string foo, bar, baz = "roar";
    public testing.bar b = new testing.bar();
}

namespace testing{
    class bar{
        string roar, blah;
        public foobar f;
    }
    class baz{
        public baz b = null;
    }
}

public class Test{
    public static void Main(string[] args){
        foobar f = new foobar();
        f.b.f = f;
        Console.WriteLine(pp.pp(f));

        int i = 2;
        Console.WriteLine(pp.pp(i));

        XmlDocument doc = new XmlDocument();
        doc.LoadXml(@"<?xml version=""1.0""?>
            <foo><bar><baz>roar</baz></bar></foo>"
        );
        Console.WriteLine(pp.pp(doc.ChildNodes[1]));

        testing.baz b = new testing.baz();
        b.b = b;
        Console.WriteLine(pp.pp(b));
    }
}

Output:

C:\DEV\Projects\test\bin\Debug>test
foobar: {
        System.String foo: null
        System.String bar: null
        System.String baz: roar
        testing.bar b: {
                System.String roar: null
                System.String blah: null
                foobar f: RECURSION!
        }
}

System.Int32: 2

System.Xml.XmlElement: <foo><bar><baz>roar</baz></bar></foo>

testing.baz: {
        testing.baz b: RECURSION!
}

(code reformatted to not break my layout)

Daily link icon Monday, April 3, 2006

  1. Mike Stall's .NET Debugging Blog : How to embed IronPython script support in your existing app in 10 easy steps. I don't agree with everything he did here, but it's a darn useful reference and introduction, and he has some neat tricks.

       (0) Tags: [IronPython]

Daily link icon Friday, March 24, 2006

.NET standard library has holes

The .NET standard library is good, but it feels like it has lots of "holes" in it, even in 2.0. For instance, why is the Converter delegate used separately on Array's and List's ConvertAll rather than being defined on a Collection or an Enumerable so that I could use ConvertAll on things like Dictionary.KeyCollection? The answer, of course, is that Collection and Enumerable are actually ICollection and IEnumerable and are not classes.

So, Microsoft didn't choose to duplicate ConvertAll everywhere it'd be useful, just in a couple of places. What this means as well is that even though you can use a foreach loop on something, you can't necessarily do a ConvertAll on it (or use the other higher-order methods). In contrast, look at Ruby, where as long as you have an each method (IIRC), you get everything associated with it for free.

Also see Adam's description of GetValue and SetValue on properties and accessors, another case where Microsoft has the same method names for similar functionality on multiple classes but no common interface or base class between them, so they can't be used interchangeably. (Insert comment about the utility of duck-typing here...)

As an aside, I also feel like it's just darn hard to find things reliably in Microsoft's documentation.

Daily link icon Saturday, March 18, 2006

  1. ASP.NET 2.0 Training Center: Cross Training for Web Developers:

    This section of the ASP.NET 2.0 Training Center helps the PHP developer learn more about PHP, and also add to their skillset by mastering ASP.NET 2.0. In the coming weeks, you'll learn how to get up to speed with ASP.NET 2.0, and you'll find details about interoperability between PHP and ASP.NET 2.0.

    To help you, you'll also find a series of webcasts specifically created, from the ground up, by world-class experts and presenters to teach ASP.NET to PHP developers.

    Started reading the first article on the page and it's excellent so far.

       (0) Tags: [ASP.NET, PHP]

Daily link icon Thursday, February 23, 2006

TableAdapter vs DataAdapter

If you're not sure whether to use a TableAdapter or a DataAdapter, this tells you which one you should use:

The typical Visual Studio mechanism for executing Transact-SQL queries and for filling datasets is the TableAdapter.

You can execute SQL statements or stored procedures against a data source using TableAdapters or command objects (for example, SqlCommand). To load data into datasets created using design tools in Visual Studio, use TableAdapters. To load data into datasets created programmatically, use data adapters. If your application does not use datasets, use command objects to execute SQL statements or stored procedures directly against a database.

If you create a dataset with a Visual Studio design-time tool (such as the Dataset Designer or the Data Source Configuration Wizard), then you use a TableAdapter to fill it. TableAdapters execute your SQL statements or stored procedures.

If you create a dataset without design-time tools, then you must use data adapters to fill and update data. (TableAdapters are not actual classes in the .NET Framework, so they are not suitable for working with datasets that have been created without the use of design-time tools. For more information on loading data into datasets with either TableAdapters or data adapters, see How to: Fill a Dataset with Data.

Daily link icon Wednesday, February 22, 2006

  1. Brad Abrams: The change from Hashtable to Dictionary.

       (0) Tags: [.NET, C#]
  2. GameDev.net - Using Lua with C#:

    Embedding Lua into C# with LuaInterface is so easy that it's almost embarrassing.

    Rock. Very good article.

       (0) Tags: [.NET, C#, Lua]

Daily link icon Monday, February 20, 2006

  1. Aspect Orienting .NET Components, by DR. Viji Sarathy Smiley Via Michael. To read.

    Update: To quote Michael, "I would never use that technique. Ever."

    Update: Eric Gunnerson had a post a year and a half ago on Microsoft's "wait and see" attitude towards AOP. Good comments. Having to inherit from ContextBoundObject is unworkable.

       (2) Tags: [.NET, Aspect Oriented Programming]
  2. A Design Rationale for C++/CLI (PDF), by Herb Sutter, via PLNews. Could be an interesting read.

       (0) Tags: [.NET, C++]

Daily link icon Monday, February 13, 2006

  1. I hate ASP.NET with a passion.

    I still think C# is a good language, and the .NET class libraries and VM are good stuff, but ASP.NET is an abortion.

       (54) Tags: [ASP.NET, C#, Programming]

Daily link icon Friday, February 10, 2006

Dump a db table in ASP.NET

One of the first things I do whenever I'm working with a new database technology (JDBC, ADO, ADO.NET, etc.) is write a table dumper to learn the API. It's also usually because I usually find myself without access to any handy db tools, so I have to write my own. Here's a dumper for ASP.NET/ADO.NET:

void DumpTable(System.Data.IDataReader reader){
    System.Data.DataTable fields = reader.GetSchemaTable();
    Response.Write("<table border='1'>");
    Response.Write("<tr>");
    foreach(System.Data.DataRow field in fields.Rows){
        Response.Write("<th>");
        Response.Write(HttpUtility.HtmlEncode(field["ColumnName"].ToString()));
        Response.Write("</th>");
    }
    Response.Write("</tr>");
    int numfields = reader.FieldCount;
    while(reader.Read()){
        Response.Write("<tr>");
        for(int i=0; i<numfields; i++){
            Response.Write("<td>");
            string s = reader[i].ToString();
            Response.Write(s.Length == 0 ? "&nbsp;" : HttpUtility.HtmlEncode(s));
            Response.Write("</td>");
        }
        Response.Write("</tr>");
    }
    Response.Write("</table>");
}

There may be a better way to do some of that (if there is, let me know). Just for reference, to connect and get a table dump (happens to be Oracle, but there's an equivalent for every db API in ADO.NET):

OracleConnection conn = new OracleConnection(
   "Data Source=dbtable;User Id=user;Password=pass"
); // wrapped to not break my layout
conn.Open();
OracleCommand cmd = new OracleCommand("SELECT * FROM table WHERE ROWNUM<100", conn);
OracleDataReader reader = cmd.ExecuteReader();

And of course, after dumping:

conn.Close();

That handy WHERE ROWNUM < n bit from here.

Update: For informational purposes, here's a similar utility function for PHP I have sitting around in my db library (modified slightly):

<?php
function printTable(&$table){
    if(!
$table){
        echo 
'Empty table';
        return;
    }
    echo 
'<table border="1">',"\n<tr>\n\t";
    foreach(
array_keys($table[0]) as $key)
        echo 
'<th>',htmlspecialchars($key),'</th>';

    foreach(
$table as $row){
        echo 
"\n</tr>\n<tr>\n\t";
        foreach(
$row as $data)
            echo 
'<td>',htmlspecialchars($data),'</td>';
    }
    echo 
"\n</tr>\n</table>\n\n";
}
?>

Daily link icon Tuesday, February 7, 2006

  1. SqlDataSource paging is lame:

    Note that the paging operation in the preceding example is being performed entirely by the GridView control over the DataView returned by SqlDataSource, which supports the ICollection interface. In this case, the GridView retrieves all of the data records from the data source, renders a subset of the rows, and then discards the remaining rows. This is sometimes called "UI paging" because the logic for paging is happening in the rendering layer of the GridView control. While convenient for paging over arbitrary collections, this is not necessarily the most efficient way to page over data. It is also possible to configure paging at the data source interface level, so that GridView requests only as many rows as it needs from the data source in order to render the current page. The SqlDataSource control does not support interface-level paging at this time.

       (0) Tags: [ASP.NET, Programming]

Daily link icon Saturday, December 31, 2005

  1. [IronPython] 1.0 Beta 1 is out now! (via PLNews). "We've just released the first beta of IronPython 1.0 and are entering the home stretch to a 1.0 final build." Good news!

       (1) Tags: [IronPython, Programming]

Daily link icon Thursday, November 24, 2005

PLNews: IronPython Demo Video Available

PLNews: IronPython Demo Video Available. To watch. Argh, you need Internet Explorer to play it. Bah.

Oh my goodness. First time through the video IE blocked the popup the video requires half-way through. Then I temporarily enabled popups in IE, and this time around the Google toolbar bundled with my computer (never turned it off since I've only used IE once to download Firefox) blocked it. Argh! And I can't even get to the Google toolbar for this window since the window has no toolbars. They could have just done the normal thing and opened up a Windows Media Player instance and been done with it.

Hey, neat presentation so far. But why does everyone who works for Microsoft sound like they've drunk the Kool-aid?

Whoa, I didn't know you could do the following in the Python interactive shell:

>>> foo = ["foo", "bar", "baz"]
>>> foo
['foo', 'bar', 'baz']
>>> bar = _
>>> bar
['foo', 'bar', 'baz']

Very cool when he debugged Python, going through stack frames back into the C# code that called it.

Page 1 →
May 2008
SunMonTueWedThuFriSat
 123
45678910
11121314151617
18192021222324
25262728293031



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 1 posts

Recent comments XML

"IMDB for music"

IMDB for Music? It looks to be a​couple of years old...​http://MusicTell.co...

Ken Empie: May 14, 9:57pm

Generated in about 0.543s.

(Used 11 db queries)