Ok, a low-tech post this time. I’ve wasted the better part of my last few days battling Share Point permissions. My goal was to write a web part that lets users add an entry to a Share Point list. So, in case you (or me) need users to add items to a list, this could save us a lot of time…
Read the rest of this entry »
How not to Code: The String Identity Function
9 12 2008Yesterday I started working on a small system that’s been having some problems lately. Reviewing the code, I quickly came across the following function:
public static string IsNullToString(string _str)
{
string name = null;
if (_str != null)
{
name = _str.ToString();
}
return name;
}
Now, it seems that if that function receives a null it returns a null, and for other string it’s returning that string. Remembering that String is a sealed class and cannot be inherited, and that String.ToString() does very little, I fail to see why this function is used every time a string is printed out, 108 times in the code…
See Also:
Comments : Leave a Comment »
Tags: code, csharp, How not to Code, Identity Function, string
Categories : How not to Code, Programming
Using jQuery to Filter Table Rows
15 09 2008I’m retouching an old system, and though it could use a filter on it’s report page. The report has a large table with about 300 rows. After some play with jQuery, I came out with this little filter.
The project is using the .net GridView control, so I had limited control over the output HTML code. Still, I think this code can work for most tables. One thing to notice: you should use the class “filterable” on your table or on one of its parents for the code to work.
First, we need a text box:
Filter: <input type="text" id="FilterTextBox" name="FilterTextBox" />
And the code:
$(document).ready(function(){
//add index column with all content.
$(".filterable tr:has(td)").each(function(){
var t = $(this).text().toLowerCase(); //all row text
$("<td class='indexColumn'></td>")
.hide().text(t).appendTo(this);
});//each tr
$("#FilterTextBox").keyup(function(){
var s = $(this).val().toLowerCase().split(" ");
//show all rows.
$(".filterable tr:hidden").show();
$.each(s, function(){
$(".filterable tr:visible .indexColumn:not(:contains('"
+ this + "'))").parent().hide();
});//each
});//key up.
});//document.ready
Explanation:
- Create Index Column – lines 2-7 – The jQuery “:contains()” selector is case sensitive, so at the first step I create another column on the table, than contains the whole row’s text on lowered case. On line 3 you may have noticed I’m filtering only rows that has <td>, to avoid hiding the header column (presumably, the header only has <th>). Later, I could make searches in this column alone. This works as long as the text on the table doesn’t change.
- Bind the Key-Up Event – lines 8-16.
- Make an array with all filter keywords – line 9 – Get all word from the text box and put them in an array. Again, I’m using toLowerCase() because “:contains” is case sensitive.
- Hide rows – lines 12-15 – I’m using jQurey’s each function to go through the array, and hiding all rows that don’t contain the current keyword (Assuming AND between all words).
That’s it. Now rows on our table can be filtered.
Comments : 5 Comments »
Tags: code, filter, javascript, jquery, table
Categories : Programming
Converting DataSets to Strongly-Typed DataSets
11 09 2008One of the first things shown to me when I started working in my company was Microsoft’s Data Access Application Block. One thing that bothered me was that I constantly had to convert DataSets it returned to strongly typed DataSets used in our programs, which usually meant merging the returned data set with a newly created DataTable. To relieve myself of those repeating tedious 6 lines of code, I made this little class that converts untyped DataSets and DataTables to a strongly typed DataTable.
In a first attempt to solve this problem, I used reflation to create an instance of the typed DataTable, so I had to pass the method the type of the DataTable, and cast it back to itself.
This is a more elegant solution, using generics:
/// <summary>
/// Helper methods and functions.
/// </summary>
/// <typeparam name="T">A strongly type DataTable.
/// A DataTable of type T will be returned from the DataSet.
/// </typeparam>
public static class DataSetAdapter<T>
where T : DataTable, new()
{
/// <summary>
/// Convert the first DataTable from a DataSet to a
/// strongly-typed data table.
/// </summary>
public static T convert(DataSet dataSet)
{
if (dataSet == null)
return null;
if (dataSet.Tables.Count == 0)
return null;
DataTable dataTable = dataSet.Tables[0];
return convert(dataTable);
}
/// <summary>
/// Convert an ordinary DataTable to a strongly-typed
/// data table.
/// </summary>
public static T convert(DataTable dataTable)
{
if (dataTable == null)
return null;
T stronglyTyped = new T();
// add data from the regular DataTable to the
// strongly typed DataTable.
stronglyTyped.Merge(dataTable);
return stronglyTyped;
}
}
The use of the class if pretty straightforward, just pass the DataSet and the Type:
DataSet employeesDataSet = OracleHelper.ExecuteDataset( connectionString, storedProcedure, parameters); EmployeesDataTable employees = DataSetAdapter<EmployeesDataTable>.convert(employeesDataSet);
Links:
Comments : Leave a Comment »
Tags: code, csharp, dataset, datatable, generics
Categories : Programming
Google Chrome, First Impression
4 09 2008Like many of us, I’ve spent much time yesterday evaluating Google Chrome. Here are some of my thoughts about Chrome:
While many features are missing from Chrome, I should note the exceptional design and
attention to details. Chrome is so usable, you can easily miss its features. For example, try to open many tabs, and than start closing them by middle clicking, or the close button. While you close tabs, Chrome does not resize them until you move away, so after one tab is closed, the next appears on the same spot. And the link destination status-bar, moving out of the window gracefully when the mouse is near it.
And “find in page” highlights the word automatically, and also highlights the scrollbar to where these results are. Nice one. These are small details, of course, but I think they show how well the interface is though out.
I was fairly impressed by the so-called omnibar, Chrome’s Url bar. I didn’t find it a lot better that Firefox’s bar though. Also, a lot has been said of Chrome’s speed and memory footprint. I’ll admit, I wish Firefox load pages this fast…
Some obvious things I miss on Chrome:
- List of open tabs – I could not find a list of all open tabs. Ctrl+Esc shows a list of all processes, but I cannot select tabs from there.
- Multiple Tabs – I can move one tab to a new window, but what about a group of tabs, or uniting windows? I’d expect selecting tabs with Shift or Ctrl, but no.
- Non-standard Interface – Google redraws the interface, and the obviously didn’t want to include a status bar at the bottom of their window. One thing they forgot: that little gripper at the corner that resizes the window. Not visible even with both scollbars visible.
- Popup blocker – The blocked windows looks like a title of a window on the bottom of the current tab, hiding the bottom of the page and the horizontal scrollbar. I think the Firefox \ Explorer way is more comfortable, although they can move the information bar to the bottom, if they like. Chrome’s way is distracting, almost like a popup. and gets in the way, like a popup.
- Default language – I’m Israeli, that’s true. But I use an English system. Chrome default language was Hebrew, without any warning. The setup was English, and the download site. Even after I changed the interface to English, I had to remove Israeli search engines and manually change the omnibar’s search to English.
- Plugins! – It’s a different web with adblock. And I miss changing tabs with the mouse wheel.
- Rss support – I click on an RSS file, and I see it’s content as if it was HTML. Ok, I don’t think a browser needs an RSS reader. I don’t even think browsers need bookmarks. But at least ask me what I want to do with it, or display XML nicely. Well, I’m sure this feature will come.
And lastly, some (two) links:
Chrome security hole
About pages – comments have some bugs, with a sure way to crash Chrome. It’s ok. It’s new.
Comments : Leave a Comment »
Tags: Chrome, Firefox, Google Chrome, Review, Web
Categories : Web
Using Sharepoint’s File Type Icons
25 08 2008We have an old web part that displays documents from a list, and shows an icon next to file. After reading the list, the web part has a long “if-else” block that checks the files extension and displays the proper image. The images come from sharepoint template directory. I need to update this web part to display more file types icons. At first we though to create a list that maps images to extensions, but after little research I’ve decided to read sharepoint’s docicon.xml file.
First, we need to find the file docicon.xml. The file usually sits under
C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\template\xml\docicon.xml
The xml directory is not mapped on the IIS, but we can still find it using this little trick:
string path = MapPathSecure("~/_layouts/");
path = Path.Combine(path, @"..\xml\docicon.xml");
Now all that’s left is to read the xml and add the data to a collection:
(This is actually the first time using XmlDataReader, but how wrong can I be?)
First, we define two members, for all files and the default icon, and then go through the XML nodes:
protected NameValueCollection fileTypeIcons;
protected string defaultIcon;
protected void readIconsXml()
{
XmlReader reader = XmlReader.Create(path);
reader.ReadToFollowing("ByExtension");
//find the first child. This doesn't skip the first node.
reader.ReadToFollowing("Mapping");
while (reader.ReadToNextSibling("Mapping"))
{
readOneNodeMapping(reader);
}
//find the default icon
reader.ReadToFollowing("Default");
reader.ReadToFollowing("Mapping");
defaultIcon = reader.GetAttribute("Value");
}
protected void readOneNodeMapping(XmlReader reader)
{
string tempKey = null;
string tempValue = null;
tempValue = reader.GetAttribute("Value");
tempKey = reader.GetAttribute("Key");
if ((tempKey != null) && (tempValue != null))
fileTypeIcons.Add(tempKey, tempValue);
}
And that’s it. We have all icons defined in the site ready in our project.
Comments : 2 Comments »
Tags: code, csharp, sharepoint, web part, xml
Categories : Sharepoint

