Sorting a DataTable

Normally if you were trying to sort a DataTable you would do see something like

[csharp]
DataTable table = retrieveTable();
DataView view = new DataView(table);
view.Sort = “Name”;
datagrid1.DataSource = view;
datagrid1.DataBind();
[/csharp]

Doing this works fine for UI cases, but if you need to get access to the sorted table you’ll want to do this.

[csharp]
DataTable unsortedTable = retrieveTable();
DataTable sortedTable = new DataTable();
foreach (DataRow row in unsortedTable.Select(string.Empty, “Name”)) {
sortedTable.ImportRow(row);
}
[/csharp]

And then you can mess with your sortedTable object.

Advertisements

Getting data out of a locked MS Access MDE

Problem: I have an access 97 MDE file (compiled MDB) which uses an external data source (.DAT ?) on a network via linked tables. It has user security on it and I need to get the data out into SQL server. Because of the user security the upsizing wizard with Access 2000 cannot convert the database before upsizing it.

Solution: If you have a login to the access database then open the MDE with shift held down, or press F11 once you’re in the MDE to bring up the table/query lists.

Select the table you want to export, hit File, Save As/Export and choose ODBC. Create an ODBC connection to your SQL server and export the data. This copies the data to SQL server.

However, you may get ‘overflow’ messages back with no detail as to what went wrong. If this happens export the same table to a temporary Excel spreadsheet. The export process this time will create a temp table called TABLENAME_export_errors. Fix those errors in your table, and you can export to ODBC again.

Popups that resize themselves

In your ASPX page, add the following such that it renders in the top-right of the popup window

And add this code such that it renders in the bottom-left of the popup window

Then add the following Javascript to your page and make a call to resizeThisWindow().

// Default window dimensions to add to the size we 'read' from the divs var _modifyX = 18; var _modifyY = 30;  function resizeThisWindow() {   var divX = document.all['divMarkerX'];   var divY = document.all['divMarkerY'];    var x = getAbsX(divX);   var y = getAbsY(divY);    //stop it becoming too big, for little monitor support   if ( x > 800 ) {     x = 800;   }    if ( y > 600 ) {     y = 600;   }      window.resizeTo(x + _modifyX, y + _modifyY);   //alert(x +" " + y); }   function getAbsX(elt) {   return parseInt(elt.x) ? elt.x : getAbsPos(elt,"Left"); }   function getAbsY(elt) {   return parseInt(elt.y) ? elt.y : getAbsPos(elt,"Top"); }   function getAbsPos(elt,which) {   iPos = 0;   while (elt != null) {     iPos += elt["offset" + which];     elt = elt.offsetParent;   }   return iPos; }

The modify X and Y values take into account default window scrollbar widths etc therefore users with non-standard settings may see things differently.

If you use master pages, put the above into a seperate template for popup windows and override the default X and Y in the implementation pages if they use scrollbars etc.

The Enter button in ASP.NET

Taken from this blog is a technique which will postback the form when you hit enter. If you use the script below, it will work in any browser (although bypassing any onClick code for the button specified).

[javascript]
function clickButton(e, buttonid)
{
var bt = document.getElementById(buttonid);
if (typeof bt == ‘object’)
{
try
{
if(navigator.appName.indexOf(“Netscape”)>(-1))
{
if (e.keyCode == 13)
{
bt.click();
return false;
}
}
if (navigator.appName.indexOf(“Microsoft Internet Explorer”)>(-1))
{
if (event.keyCode == 13)
{
bt.click();
return false;
}
}
}
catch (er)
{
// invoke the default postback handler for buttons that render as tags
__doPostBack(buttonid,”);
}
}
}
[/javascript]

Backup!

Socialanimal.com – the images at least – will be offair for a while until I get my server sorted out. I lost a hard drive last week which contains all the website, images, my email, contacts, etc. Until I get it fixed this blog is all you’ve got.

Al