Saturday, May 30, 2009

Vista's GUI Scheduled chkdsk doesn't seem to do the job

I ran into problems on my hard disk and Vista's GUI chkdsk didn't prove up to the task of getting it healthy again. Booting to repair mode from the Vista install DVD and running "chkdsk /P" from a command prompt did the trick and showed what it was doing as well.

UPDATE: It seems I had a bad disk drive. I replaced it, got Norton Disk Doctor running, installed Vista SP2 and a bunch of updates and now chkdsk seems to do the job on boot that I expected it to. I'm not sure what fixed it. I'm still getting some errors on the drive, so stay tuned.

My HP 9700 Vista machine was hanging every night. I checked the error logs and found that there were disk corruption errors logged shortly before each hang. I used the GUI to schedule a chkdsk. You do this by opening "Computer", right clicking on the drive you want to check and select properties. On the tools tab, select "Check Now ..." in the section "This option will check the volume for errors.". In the resulting dialog "Automatically fix system errors" is already checked and you can optionally check "Scan for and attempt recovery of bad sectors". Clicking start will inform you that "Windows can't check the disk while in use" and ask you if you want to check for hard disk errors the next time you "start your computer". NOTE: My experience is that it doesn't mean reboot, but start your computer from the Dead Off position. I tried just the first one but the system hung that night and then I tried the second one but it didn't seem to run on the reboot.

I didn't much like this because it seemed to hang a long time on a blank screen at startup, but not long enough to really have gone through and checked all the sectors on the disk. It was impossible to tell because you get zero feedback during this process of staring at the blank screen.

I found this not too satisfactory so I decided to run chkdsk from the command prompt. I thought I would just go the whole way and not use the Vista that was on my hard drive. I booted from a Vista install DVD, went into the repair option, and selected a command prompt. I decided that "chkdsk /B" which means "NTFS only: Re-evaluates bad clusters on the volume (implies /R)" would be the way to go. The "/R" option decodes to "Locates bad sectors and recovers readable information (implies /F)". The "/F" option means "Fixes errors on the disk". This resulted in a very long process but was really good about telling everything that is going on. Now my disks are clean and healthy (I hope)!

I decided that Vista just couldn't be trusted to keep my disks in good health, so I purchased a copy of Norton SystemWorks (direct from Symantec for $49 downloadable). I hope that does the job. If it doesn't, I'll blog about it. I'll also blog about it if I find things I particularly like.

Read more!

Sunday, May 24, 2009

How to create/configure GridView columns at runtime based on the data to be bound

It turns out that there is a way to avoid putting your data structure directly into the markup in cases where you can't have the columns automatically generated for you when you bind to an ASP.NET GridView (e.g. you want a custom format or you want to put in a button). It's not too hard to do and can be very flexible.

I was enjoying the wonderful abilities of LINQ to save effort in creating new record structures to bind to a dataview. For example:
var ps = from p1 in pa
select new { p1.VANID, VoterID = p1.StateFileID,
Precinct =
Name = p1.LastName + ", " + p1.FirstName,
p1.Party, YOB = p1.DateOfBirth.Year,
VoteAddress = p1.Addresses == null ? string.Empty :
(from ad in p1.Addresses
where ad.AddressType == "Voting"
select ad.StreetAddress + "; " +
PrefPhone = p1.Phones == null ? string.Empty :
(from ph in p1.Phones
where ph.PhoneType == "Preferred"
select ph.Number).FirstOrDefault()),
Email = FormatUtils.ReturnValueOrEmptyIfNull(p1.Email)
Obviously this was a lot of stuff to collect together in a nice set and it was great to be able to simply bind it to the GridView and have my columns etc. happen automagically:

gvResultsList.DataSource = ps;

However, for the next step, I needed to convert the first field to a button so a more detailed look at a particular row of data could be viewed. I briefly considered putting the names in the aspx file like usual, but then I thought "What if I need to add/subtract to that query and the columns change?" -- then I would have to change things in both places and keep them in agreement. Ugh!

Searching the web I found a post that put me on the track for a solution:
(you can Google for "Get fieldnames from LINQ query")

I modified the solution for my purposes. The results:

// Check to make sure we only add the columns once
if (gvResultsList.Columns.Count == 0)
// materialize the query
var qryList = ps.ToList();

// get the first element
var firstElement = qryList[0];

// retrieve a Type reference to the element
Type elementType = firstElement.GetType();

// db columns are represented as object properties
PropertyInfo[] columns = elementType.GetProperties();

// work with PropertyInfo array
foreach (PropertyInfo col in columns)
// use parent class so can assign child classes
DataControlField bField = null;

// Only want one ButtonField column
if (col.Name == "VANID")
ButtonField butField = new ButtonField();
butField.CommandName = "select";
// the Name is the name of the property/column
butField.DataTextField = col.Name;
butField.HeaderText = col.Name;
butField.ButtonType = ButtonType.Button;
// Set a width wider than largest label generates
butField.ControlStyle.Width = new Unit("8.0em");
bField = butField;
BoundField bouField = new BoundField();
// the Name is the name of the property/column
bouField.DataField = col.Name;
bouField.SortExpression = col.Name;
bouField.HeaderText = col.Name;
bField = bouField;

// Use this to custom format specific data types
// PropertyType is the type of the property
//if (col.PropertyType == typeof(Decimal))
// bField.DataFormatString = "{0:N2}";

gvResultsList.Columns.Insert(gvResultsList.Columns.Count, bField);

gvResultsList.DataSource = ps;

Read more!

Monday, May 18, 2009

Things I learned reading Programming Entity Framework by Julia Lerman

In my previous blog post, I mentioned that I was reading Programming Entity Framework by Julia Lerman and that I had departed from reading the book to learn about Microsoft's SQL Server Profiler. Well, as I have continued with the book that has happened twice more. I've decided to make this blog post into a list of the "implicit links" in the book that I have followed. My references in no way should be thought to be exaustive or even to partially cover the subject. There are just links to posts that I found to be interesting or useful for my purposes. If you want an exhaustive list, I'm sure Google or Live Search can help you out.

I'll be updating this list from time to time as I run into other interesting things that I have been motivated to look up. Following the "implicit links" certainly makes reading the book take a *lot* longer, but I think it's worth it.

Here is my list of links:

SQL Server Profiler
T-SQL's scope_identity() function
Factory Classes
Miscellaneous Items

Read more!

Monday, May 11, 2009

SQL Server Profiler - Not all templates contain database filter

In my continuing studies on .Net development, I am reading Programming Entity Framework by Julia Lerman. In one of the early chapters Julie referenced the SQL Profiler. Not having ever tried that particular tool, I fired it up and gave it a try (naturally without reading anything first -- just testing to see how intuitive the tool might be, of course). I found it catching a lot of things, but wasn't able to find my test query from one of the book's exercises in there.

It turns out that you gotta pick the right template (and the default one isn't the right one any more like it was in SQL Server 2000) ...

I decided it must have a way of filtering, so I poked around and found a button with the word filter on it on the "Events Selection" tab. I thought I'd filter on the database, but discovered that there didn't seem to be a way to do it. I searched the web and found some examples, but they all seemed to have filter selections different from mine. I looked for videos, but the ones I saw also had the database nicely displayed in the Edit Filter dialog.

I finally found a post which explained that one had to have the database column set up to be displayed in the results before you could filter on it and the easiest way to do this was to start with the tuning template and follow the procedure:

Assuming you have the Tuning template selected, to filter:

  • Click the "Events Selection" tab
  • Click the "Column Filters" button
  • Select "DatabaseName", click the plus next to Like in the right-hand pane, and type your database name.
It's one of those things that becomes obvious once you realize it, but isn't the first time you are confronted with the big blank grey screen of SQL Server Profiler.

Read more!