November 2008 - Posts

I had an import process that imported Excel data.  But I didn't want to hard-code the name of the table being imported.  To handle this, I found this article online by David Hayden with a solution: using the GetSchema method to get the schema of the underlying connected database.  Check it out at:  http://davidhayden.com/blog/dave/archive/2006/05/31/2975.aspx

Posted by bmains | with no comments
Filed under:

 I've been asked: "why use dynamic"?  I admit, there aren't a lot of areas where dynamic is really needed.  But there are some, and while I'd recommend limiting use of the dynamic keyword, I would never suggest not to ban it.  One of the uses where I found it handy is with wrapper classes.  I was developing a wrapper that looked like:

public class DataBoundWrapper
{
  private Control _control = null;

  public static DataBoundWrapper Create(GridView control)
  {
      return new DataBoundWrapper(control);
   }

  public static DataBoundWrapper Create(DetailsView control)
  {
      return new DataBoundWrapper(control);
   }
}

I left out the constructor, but basically it takes the control reference and assigns it to _control.  Now, to wrap I would do something like this:

public string DataSourceID
{
   get
   {
      if (_control is GridView)
          return ((GridView)_control).DataSourceID;
      else if (_control is DetailsView)
          return ((DetailsView)_control).DataSourceID;
    }
}

This is because the GridView and DetailsView do not define some of these properties in a base class; they each define the same properties repeatedly.  I personally think this is a limitation of the controls, but the design is what it is.  THe DataSourceID property may not be the best example, but other properties like AutoGenerateColumns (or Fields), paging properties, and many more properties are not defined in a common base class or interface.

So a way around this may be to change the definion to use:

dynamic _control;

And have each property use now instead:

public string DataSourceID
{
  get { return _control.DataSourceID; }
  set { _control.DataSourceID = value; }
}

Now the control's properties are linked dynamically at runtime, and because both controls have a DataSourceID, this works and is a lot easier to implement.

Posted by bmains | 1 comment(s)
Filed under:

When planning an ASP.NET AJAX solution, I'd recommend thinking about the total number of downloadable files that you site or class library will have.  This matters because it can affect performance.  Omal Al Zabir had a good overview of the problem and solution here.  One of the solutions he provides is the ability to combine scripts through a component.  The ASP.NET AJAX framework also added the ability to perform script combining as well, using the CombineScripts attribute of the ScriptManager, instead of the Scripts tag.

Outside of script combining, it's possible to have debug and release versions of your script.  The script manager component has a ScriptMode property that can be set to Debug or Release.  The ScriptManager knows whether the application is compiled in debug or release mode, allowing you to provide a collection of files in debug mode, while providing one single large file in release mode.

<asp:ScriptManager>
 <Scripts>
  <asp:ScriptReference Path="~/temp1.debug.js" ScriptMode="Debug" />
  <asp:ScriptReference Path="~/temp2.debug.js" ScriptMode="Debug" />
  <asp:ScriptReference Path="~/temp.js" ScriptMode="Release" />
 </Scripts>
</asp:ScriptManager> 

Here we have two debug scripts and one release script, as one script file has better performance.  The challenge can come when you manage two sets of scripts though.

Another aspect to keep in mind is putting the security features all in one file.  For instance, suppose for a blog section of the app, you have client-side AJAX features implemented, giving a few users the ability to perform some additional work.  For all other users, they won't have these abilities.  If you put the security features in a file: blogsecure.js, then what you can do is programmably control whether this script is provided to the user based on their role, using something like:

ScriptManager manager = ScriptManager.GetCurrent(this.Page);
if (this.User.IsInRole("BlogAdmins"))
    manager.Scripts.Add(new ScriptReference("~/blogsecure.js"));

That way, the script doesn't even run, and this can help prevent potential hacking while giving the user full fledged functionality of inserting, updating, and deleting data asynchronously via web service calls.

Posted by bmains | with no comments
Filed under:

Microsoft had some interesting presentations at the PDC in Los Angeles.  Some of the newest features are (found from this http://channel9.msdn.com/pdc2008/TL16/)

  • The new dynamic keyword, which allows for dynamic invocation of methods that you don't know at compile time.  This dynamic keyword isn't like var although it appears to be.  While var can represent any type, dynamic represents an object that will have its members dynamically invoked at run time.  This allows for greater flexibility because normally you'd have to write additional reflection code, and this shortcuts this.  There are some great examples.
  • Optional properties in C#, which I should say finally this comes over from VB.NET.  No longer do you have to specify every parameter in a method: you can use the following parameter syntax string name = "Brian" to assign a default value to a parameter in the method.  This means you can leave out values, or specify optional values explicitly with paramName:"value".
  • Eventually creating the C# compiler in managed code, making tapping into the compiler easier.

Some interesting stuff coming out of Microsoft.  Stay tuned.

Posted by bmains | with no comments
Filed under: ,

I recently got this error when applying a data fix.  The error was for an insert that contains a select, I used the values keyword like:

insert into <table> (<fields>) values (1, 2, (select ...), 4)

The article referenced below indicates doing:

insert into <table> (<fields>) select 1, 2,(select ...), 4 -- notice no parens for outer select

You can see that post here:

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=36506

Posted by bmains | with no comments
Filed under: