Riaan Lehmkuhl's Blog

Subversion, Progamming, Tips & Tricks and whatever else springs to mind.
23Feb

Windows.Forms Binding errors with .Net 2.0 (Part 4)

23 February 2009 20:53 by Riaan Lehmkuhl

The problem:
In a DataGridView control, after adding a new row, and clicking away, the new row just disappears without any visible error.

The solution:
This is most likely caused by an error being raised due to a data input error. Im my case it was because of a non visible column not allowing nulls.
To find out if this is the cause of yoyr issue, put a break point in the DataError event handler. Even though the error might have beeen written to the row's ErrorText, it is never displayed because the row disappears.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
19Aug

MSTSC /console not working anymore

19 August 2008 21:13 by Riaan Lehmkuhl
When using the "Virtual Server Administration Website" to admin your Virtual Server 2005 R2 virtual servers one needs to be logged on to the console of the host server or you get a HTTP 500 Internal Server Error message. For this reason I've always used the mstsc /console command to log in to session 0 of the host server, but to my dismay this just didn't seem to work anymore.
I opened the command prompt and typed in mstsc /?. Lo and behold, the console option is gone:
---------------------------
Remote Desktop Connection Usage
---------------------------
MSTSC [] [/v:] [/admin] [/f[ullscreen]]
[/w: /h:] [/public] | [/span] [/edit "connection file"] [/migrate]
"connection file" -- Specifies the name of an .rdp file for the connection.
/v: -- Specifies the remote computer to which you want to connect.
/admin -- Connects you to the session for administering a server.
/f -- Starts Remote Desktop in full-screen mode.
/w: -- Specifies the width of the Remote Desktop window.
/h: -- Specifies the height of the Remote Desktop window.
/public -- Runs Remote Desktop in public mode.
/span -- Matches the remote desktop width and height with the local
virtual desktop, spanning across multiple monitors if necessary. To span
across monitors, the monitors must all have the same height and be aligned
vertically.
/edit -- Opens the specified .rdp connection file for editing.
/migrate -- Migrates legacy connection files that were created with
Client Connection Manager to new .rdp connection files.
---------------------------
OK   
---------------------------
I noticed the /admin switch, which seemed to do the trick.
After some more research I've found that this was apllied with XP SP3 and supposed to be because of some new security features in Windows Server 2008 and Vista SP1. Due to this fact this trick won't work on these.

Currently rated 5.0 by 2 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
24Jun

Ode to Code, a Geek Poem

24 June 2008 23:21 by Riaan Lehmkuhl
I've just discovered the coolest poem 'Ode to Code, a Geek Poem' on Raphael Koster's Website:
Ode to Code: A Geek Poem
Just think:
The twine of sine
and cosine, twang of tangents,
tangles of angles and twirls of tris,
the way each curve is wavelength,
like a sound is wavelength, light is
wavelength. A four forty’s tone
is blue, its hertz a wiggle,
wobble, flow from
high to low, a
nanometer’s
drunken walk
the shade of skies.

Perhaps by this was Schumann
driven mad; the way the math invades,
pervades, like A four forty in his ear
for years: a cosmic radio of audio
uncaused by any known thing.
Oh, the song was blue,
but blues were
something
Schumann
never heard.
Or always did.
Or thought he always did.

The azimuth, horizon, incidence;
The cadence, coda, recapitulation.
These are all the whirlwind tang of life:
From helices in mitochondria to lacy
fractal leaves to strings vibrating
quarks, and time we see
cross-sectioned.
Here we
have the arc
of it, the seconds. Mark.

And now, we twist our code
in loops, recurse in tighter spirals, flow
through chains of consequence — input
output GIGO FILO — at play with toys
that mimic magic, reify and
retro-fy, a Bezier here,
vector there,
a wave
of bosses, twirl
Of blues, a count of lives, all binary.

Signs, sines, sprites, twines, tangents, tunes, time. In rhyme.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
19Jun

Adding an Ordinal column using a sub query

19 June 2008 23:10 by Riaan Lehmkuhl
This is just a simple way of adding an ordinal column to a SQL result using a sub query. This method depends on the Primary Key field of the table. I used the Northwind database for this example.
The idea is to select the Products with an ordinal numbered column...
Ordinal ProductID ProductName CategoryID
1 11 Queso Cabrales 4
2 12 Queso Manchego La Pastora 4
3 31 Gorgonzola Telino 4
4 32 Mascarpone Fabioli 4
5 33 Geitost 4

To do this I've created the following stored procedure with the CategoryID is an optional parameter:
CREATE PROCEDURE [dbo].[sp_SelectProducts]
(
@CategoryID INT = NULL
)
AS
DECLARE @sql NVARCHAR(4000)
SET @sql = N'
SELECT top 5 (SELECT COUNT(*) 
FROM Products p 
WHERE p.ProductID <= Products.ProductID '
IF (@CategoryID IS NOT NULL) BEGIN
SET @sql = @sql + N' AND p.CategoryID = Products.CategoryID '
END
SET @sql = @sql + N') AS Ordinal,
ProductID,
ProductName,
CategoryID,
QuantityPerUnit,
UnitPrice,
UnitsInStock,
UnitsOnOrder,
ReorderLevel
FROM Products
WHERE Discontinued != 1'
IF (@CategoryID IS NOT NULL) BEGIN
SET @sql = @sql + N' AND CategoryID = ' + CAST(@CategoryID AS NVARCHAR)
END
EXEC sp_executesql @sql;
For each product that is returned we get it's ordinal position by counting the ProductIDs already returned and it self.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
12Jun

Microsoft Excel - How to check if a value exists in a range

12 June 2008 23:04 by Riaan Lehmkuhl
I had to compare some simple lists. I could have used a database, loaded the lists and used sql to do the comparisons. Not in the mood for that, then let's just use Excel.
We have the following two lists in a spreadsheet and we want to know which of the values in column B exists in column A:
  A B C
1 Fusce Vestibulum  
2 mauris mattis  
3 nulla Fusce  
4 pellentesque consequat  
5 iaculis libero  
6 aliquam eu  
7 eu sem  
8 tincidunt Nullam  
9 consequat iaculis  
10 metus elit  


After some digging around in the Excel functions, I found VLOOKUP that does the trick:
  A B C
1 Fusce
Vestibulum
=VLOOKUP(B1,$A$1:$A$10,1,FALSE)
2 mauris mattis #N/A
3 nulla Fusce Fusce
4 pellentesque consequat consequat
5 iaculis libero #N/A
6 aliquam eu eu
7 eu sem #N/A
8 tincidunt Nullam #N/A
9 consequat iaculis iaculis
10 metus elit #N/A


In column C it prints the search value if it was found, or "#N/A" if not found. This is ok, but we can make it look a bit nicer by adapting the formula a little bit:
  A B C
1 Fusce
Vestibulum
=ISNA(VLOOKUP(B1,$A$1:$A$10,1,FALSE)=B1)=FALSE
2 mauris mattis FALSE
3 nulla Fusce TRUE
4 pellentesque consequat TRUE
5 iaculis libero FALSE
6 aliquam eu TRUE
7 eu sem FALSE
8 tincidunt Nullam FALSE
9 consequat iaculis TRUE
10 metus elit FALSE


Using the updated formula, "TRUE" is printed if the value in column B is the same as the value returned from VLOOKUP otherwise "#N/A" is returned. This in turn is handled by the ISNA function, that's return value is then inverted so that the "TRUE" for "#N/A" is displayed as "FALSE" (the value was not found).

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
12Jun

Simple lamda expression example in C#

12 June 2008 22:58 by Riaan Lehmkuhl
A friend asked me for an example of a lamda expression, and after going through my code and searching the net, I discovered this little gold nugget:
A lambda expression is a way of creating a function without giving it a name.
Suppose you defined: bool blah(n) { return n%2 == 1; } That's a function named blah. (n => n%2 == 1) int[] numbers = {5,4,1,3,9,8,6,7,2,0 }; int oddNumbers = numbers.Count(n => n % 2 == 1); "numbers" is the sequence. It's an array, implementing IEnumerable<int>.
If you just call numbers.Count() you'll get 10. The version with the lambda expression is saying "only count elements which match this predicate".
It's sort of similar to calling: numbers.Where(n => n % 2 == 1).Count();
Reference:
http://www.eggheadcafe.com/software/aspnet/31850595/confused-about-the-lamda.aspx

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
11Jun

The UNIX date command and Date manipulation

11 June 2008 22:44 by Riaan Lehmkuhl
You need to do something on a daily with a file in a directory named yyyy-mm-dd.txt (year-month-day.txt).
That's easy enough:
#!/bin/ksh
# just get the current date
today="`date +%Y-%m-%d`"
filename="${today}.txt"
echo $filename
Oops, you need the file from yesterday. Not so simple any more as the unix date command just sets or prints the date.
Let's do some of the usual date math (month ends, leap years, etc.).
#!/bin/ksh
# default is one to travel back in time to yesterday
daystogoback="1" 
# to be a little dynamic, we can use an argument to go back n days.
if [ $# -eq "1" ]; then
   daystogoback="$1"
fi
# get the current year month and day
nowy=`date +%Y`
nowm=`date +%m`
nowd=`date +%d`
# get rid of the month's possible leading zero
nowm=`echo $nowm*1|bc`
# if today's day is greater than the number of days to go back
# we really can just take the shortcut and subtract the days to go back
# because we will stay in the current month and nothing fancy is needed
if test $nowd -gt $daystogoback
then
        nowd=`echo $nowd-$daystogoback|bc`
else
# if we are in January, we have to go to the previous year 
# and set the month to December
# else we can just subtract the month
        if test $nowm -eq "1"
        then
                nowy=`echo $nowy-1|bc`
                nowm="12"
        else
                nowm=`echo $nowm-1|bc`
        fi
# now we can calculate the day, but first we have to see
# how many days in the month...
# we default to 31 and do nothing more for the months with 31 days
# we have to check for February and then for a leap year (28/29)
# the rest of the months will have 30 days
        newd="31"
        case "$nowm" in
                "1") ;;
                "3") ;;
                "5") ;;
                "7") ;;
                "8") ;;
                "10") ;;
                "12") ;;
                "2")    if test "0" -eq "`echo $nowy%4|bc`"
                                then
                                        newd="29"
                                else
                                        newd="28"
                                fi ;;
                *) newd="30" ;;
        esac
# subtract the current day from the days to go back 
# and then from the total days in the month
        nowd="`echo $daystogoback-$nowd|bc`"
        nowd="`echo $newd-$nowd|bc`"
fi
# now we just do some formatting for the month and day
if test $nowm -lt "10"
then
        nowm="0$nowm";
fi
if test $nowd -lt "10"
then
        nowd="0$nowd";
fi
# our new date
filedate="$nowy-$nowm-$nowd"
# and the file we need to access...
filename="${filedate}.txt"
echo $filename
Always fun playing with dates.

References:
KSH script BASICS
Wikipedia: Korn shell

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
26Mar

View running 4GL SQL queries

26 March 2008 22:39 by Riaan Lehmkuhl
It has been a while since i have had to work with Informix / 4GL. I have already forgotten tha simple commands to view the current running SQL query with an executing 4GL script. So this is more of a note to myself, but if it helps you, I'll be overjoyed.
First find the correct process:
> ps -ef | grep glgo root 7618 7368 63 22:58:19 pts/ta 0:21 fglgo the_4gl_name_here
Then get the correct session using the pid from the previous result:
> onstat -g ses | grep 7618 1644203 root ta 7618 myserver 1 200704 187952 off
Now get the current executing sql of the session id above:
> onstat -g sql 1644203
If you would like the display to be automatically refreshed, just add a 'r' switch:
> onstat -gr sql 1644203
I hope I will remember to come and look for this next time I need it in a few months time.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
09Jan

Accessing fields in the BindingSource.Current property

09 January 2008 22:18 by Riaan Lehmkuhl

I was using the Current property of a BindingSource object in VB.Net for sometime now using inplicit conversions to access fields:

MyBindingSource.Current(0) = someValue While in C# we need to explicitly cast the Current property to some type and use that. I tried using typeof(...) and got this peculiar result...
typeof(MyBindingSource.Current) 'Myproject.Form1.MyBindingSource' is a 'field' but is used like a 'type'

Then I tried .ToSting(), and this had a more promising result...
MyBindingSource.Current.ToString() "System.Data.DataRowView"

So, if you want to access a field in a BindingSource:
DataRowView drv = MyBindingSource.Current as DataRowView; drv[0] = someValue; //or assignment (if the field's type is an int) int someValue = Convert.ToInt32(drv[0]);
Hope i save someone five minutes.

Currently rated 5.0 by 2 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
04Jan

Windows.Forms Binding errors with .Net 2.0 (Part 3)

04 January 2008 22:13 by Riaan Lehmkuhl
On to the next hurdle (sigh).

The problem:
In a DataGridView, when deleting the last remaining data row, the UserDeletedRow event does not fire.

The solution:
To work around this we need to add some logic to the UserDeletingRow event handler to count the number of rows and call the event handler when necessary... private void DataGridView1_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e) {    if (DataGridView1.RowCount == (DataGridView1.AllowUserToAddRows ? 2 : 1))       DataGridView1_UserDeletedRow(DataGridView1, new DataGridViewRowEventArgs(e.Row)); } private void DataGridView1_UserDeletedRow(object sender, DataGridViewRowEventArgs e) {    // do something here... }

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Riaan Lehmkuhl


Me, a disorder of the brain that results in a disruption in a person's thinking, mood, and ability to relate to others.

Recent comments

Comment RSS

Thingies

Calendar And Month List

<<  September 2010  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

View posts in large calendar

Disclaimer & Privacy

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Privacy:
We use third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.

Most comments

Cool Quote

I know that you believe that you understood what you think I said, but I am not sure you realize that what you heard is not what I meant. - Robert McCloskey