Pages

Tuesday, August 30, 2011

Recursively Retrieving All Directory and File Information

Populates a T-SQL DB table from gathering all directory and file saved to specified directory on a local hard drive.

Below is the code that recursively retrieves all directory and file information from the /media/ directory:

   1: public partial class Recursive : System.Web.UI.Page
   2: {
   3:     protected class Dirs
   4:     {
   5:         public string name { get; set; }
   6:         public List<Filez> files { get; set; }
   7:     }
   8:     protected class Filez
   9:     {
  10:         public string name { get; set; }
  11:         public string ext { get; set; }
  12:         public string path { get; set; }
  13:     }
  14:     List<Dirs> dirs = new List<Dirs>();
  15:  
  16:     protected void Page_Load(object sender, EventArgs e)
  17:     {
  18:         recursive("/media/");
  19:         
  20:     }
  21:  
  22:     protected void recursive(string path)
  23:     {
  24:         DirectoryInfo d = new DirectoryInfo(HttpContext.Current.Request.MapPath("~" + path));
  25:         Dirs tDirs = new Dirs();
  26:         tDirs.name = d.Name;
  27:         string dStr = "";
  28:         foreach (var item in d.GetDirectories())
  29:         {
  30:             dStr = path + item.Name + "/";
  31:             recursive(dStr);
  32:         }
  33:         tDirs.files = d.GetFiles().Select(f => new Filez { name = f.Name, ext = f.Extension, path = path }).ToList();
  34:         dirs.Add(tDirs);
  35:  
  36:     }
  37: }

Using Linq-Entities framework, the gathered information is saved to the database by calling addToDB():



   1: protected void addToDB()
   2: {
   3:     int width, height;
   4:     using (adminDBEntities contxt = new adminDBEntities())
   5:     {
   6:         foreach (var item in dirs)
   7:         {
   8:             foreach (var f in item.files)
   9:             {
  10:                 width = 0;
  11:                 height = 0;
  12:                 string path = HttpContext.Current.Request.MapPath("~" + f.path + f.name);
  13:                 if (".jpg .png .gif .bmp".IndexOf(f.ext) != -1)
  14:                 {
  15:                     using (System.Drawing.Image img = System.Drawing.Image.FromFile(path))
  16:                     {
  17:                         width = img.Width;
  18:                         height = img.Height;
  19:                     }
  20:                 }
  21:  
  22:                 MediaType mediaT = contxt.MediaTypes.Where(e => e.ext == f.ext).FirstOrDefault();
  23:                 if (mediaT != null)
  24:                 {
  25:                     mediaT.Medias.Add(new Media
  26:                     {
  27:                         name = f.name,
  28:                         path = f.path,
  29:                         width = width,
  30:                         height = height,
  31:                         iDate = DateTime.Now,
  32:                         uDate = DateTime.Now
  33:                     });
  34:                 }
  35:             }
  36:         }
  37:         contxt.SaveChanges();
  38:     }
  39: }

This blog is for my notes. Much of the code presented is work in process.

Saturday, August 27, 2011

VS 2010 C# Generic Handlers

When I create a new C# generic handler, tow files are created:

  • C# code page
  • Markup page

C# code page is where all C# source code goes, and the Markup page is XML declaration markup.

C# code page example:

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Linq;
   4: using System.Web;
   5: using adminDataContext;
   6: using System.Web.Script.Serialization;
   7:  
   8: namespace FileManager.ckeditor
   9: {
  10:     public class linkPages : IHttpHandler
  11:     {
  12:         protected class zPages
  13:         {
  14:             public int id { get; set; }
  15:             public int? categoryId { get; set; }
  16:             public string page { get; set; }
  17:             public string err { get; set; }
  18:             public string category { get; set; }
  19:         }
  20:  
  21:         private zPages requestVals = new zPages();

Markup page example:



   1: <%@ WebHandler Language="C#" CodeBehind="linkPages.ashx.cs" Class="FileManager.ckeditor.linkPages" %>

The Markup page resembles a code behind page where it is a sub file of the Code page with the Solution Explorer’s tree view.


Gaining access to the Martup page: Within the Solution Explorer’s tree view, right click on the Code page and select View Markup from the flyout.


Why I am writing this, because this is the second time wondering why that when I change the namespace name within the Code page, the application couldn’t find the changed  namespace. Searching the web revealed nothing too help me. Now that I have figured it out, I am documenting it just in case.


When I change the namespace in the Code page, I must also change it in the Markup page. Easy!!!! But I didn’t realize that their was a Markup page till I right clicked on the Code page and selecting View Markup.


All along I knew it had to something very simple, however this is the second time since I had not documented previous.


This blog is for my notes. Much of the code presented is work in process.

NULL Values LINQ to Entities

Working with two tables where the foreign key could be NULL, I noticed LINQ to Entities showed interesting results when it involved NULL values.

There is a lot of information on the web writing about the workaround. In particular this site was very clear:

Checking for possibly null values in LINQ


For my benefit, I have included these workarounds.

This will work:

   1: protected class zPages
   2: {
   3:     public int id { get; set; }
   4:     public int? categoryId { get; set; }
   5:     public string page { get; set; }
   6:     public string err { get; set; }
   7: }
   8:  
   9: adminDBEntities contxt = new adminDBEntities();
  10:  
  11: IEnumerable<zPages> query = contxt.WebPages.Where(p => p.categoryID == null).Select(p => new zPages { id = p.ID, page = p.name, categoryId = p.categoryID });

Within the zPages class, I have a nullable int (int?), which allows integers to have null values. The above query works because the Where clause explicitly equates to null: p.categoryID == null


However, if a variable is null and this variable replaces the explicit null, this causes problems:



   1: string CategoryId = null;
   2: IEnumerable<zPages> query = contxt.WebPages.Where(p => p.categoryID == CategoryId).Select(p => new zPages { id = p.ID, page = p.name, categoryId = p.categoryID });

Because the LINQ to Entities query will translate to:



   1: exec sp_executesql N'SELECT 
   2: [Extent1].[ID] AS [ID], 
   3: [Extent1].[name] AS [name], 
   4: [Extent1].[categoryID] AS [categoryID]
   5: FROM [dbo].[WebPage] AS [Extent1]
   6: WHERE [Extent1].[categoryID] = @p__linq__0',N'@p__linq__0 int',@p__linq__0=NULL

Notice the query equates @p__linq__0=NULL which will always return false. Testing for NULL within a SQL statement is @p__linq__0 IS NULL, however LINQ to Entities does not, which is why when involving variables with NULL values, the result’s collection return incorrectly.


The workaround:



   1: IEnumerable<zPages> query = contxt.WebPages.Where(p => CategoryId == null ? p.categoryID == null : p.categoryID == CategoryId).Select(p => new zPages { id = p.ID, page = p.name, categoryId = p.categoryID });

NOTICE within Where clause: 

p => requestVals.categoryId == null ? p.categoryID == null : p.categoryID == requestVals.categoryId

 {code.logos.com} writes that the above workaround generates some scary SQL, so they give another workaround:



   1: IEnumerable<zPages> query = 
   2:     (CategoryId == null ?
   3:     contxt.WebPages.Where(p => p.categoryID == null) :
   4:     contxt.WebPages.Where(p => p.categoryID == CategoryId)).Select(p => new zPages { id = p.ID, page = p.name, categoryId = p.categoryID });

This blog is for my notes. Much of the code presented is from my search of the web, which I will give reference not to all but some important sites.

Saturday, August 20, 2011

CKEDITOR Building Custom Plugin

For some reason I had a difficult time building a CKEDITOR plugin. I am not indicating that it is difficult in building a plugin.

“onLoad” is null or not an object


Why I had difficulties, because the simple syntax errors that I made, CKEDITOR displays a generic error dialog box for many simple syntax errors. CKEDITOR defaults to this error dialog with any plugin syntax error:

Untitled-1

This particular error dialog box message did not help with the many simple syntax errors that I was creating. I was tired.
This error message lead me down many wrong paths too correcting the problems, but now I have worked through many my troubles and I have a simple working copy, which I would like to post for my notes. However, I would first like to post a not so well documented problem that I came across.

My first problem:


There are two ways implementing CKEDITOR: Developer and Compressed.
Developer: CKEDITOR JS script isn’t compressed, which makes it readable and easy for error readability. Compress is the JS script is compressed and usually integrated when going live.
Separate script files:

Developer:
<script src="ckeditor_3.6.1/ckeditor_source.js" type="text/javascript"></script>

Compressed:
<script src="ckeditor_3.6.1/ckeditor.js" type="text/javascript"></script>

So, what I did was I worked with the Developer version by adding <script src="ckeditor_3.6.1/ckeditor_source.js" type="text/javascript"></script> to my header.
What I didn’t realize was CKEDITOR works with two different plugin folders. One plugin folder located in the CKEDITOR folder’s root, and another is located in the CKEDITOR _source folder.

Untitled-2


So, when I built my simple plugin, I built the plugin in the plugin folder within CKEDITOR folder's root, and because I included ckeditor_source.js to the header (uncompressed script), I received the above error dialog box many times. It took a good part of the day, before I realized CKEDITOR had another plugin folder, which was located in the CKEDITOR _source folder.
Correcting the error:
Working with Developer or uncompressed script (ckeditor_source.js), I must build the plugins within the _source plugin folder.
Working with compressed script (ckeditor.js), I must build the plugins within the root plugin folder.
What a day, but now I can move on!
I have had help from many websites:

Working copy:

Within the plugin.js file:
(function () {
var btnAction = { exec: function (editor) {
alert("Hello");
}
},
plugNme = "deDogs";
CKEDITOR.plugins.add(plugNme, {
init: function (editor) {
editor.addCommand(plugNme, btnAction);
editor.ui.addButton("deDogs", {
lablel:"deDogs",
icon:this.path + "check.png",
command: plugNme
});
}
});
})();

Within the config.js file:
CKEDITOR.editorConfig = function (config) {
config.extraPlugins = "deDogs";
config.toolbar = [["deDogs"]];
};
Took longer than I wanted, however I CKEDITOR rocks!