LessThanDot Site Logo

LessThanDot

A decade of helpful technical content

This is an archive of the posts published to LessThanDot from 2008 to 2018, over a decade of useful content. While we're no longer adding new content, we still receive a lot of visitors and wanted to make sure the content didn't disappear forever.

C# Sorting winforms controls using linq

Use the power of linq Create a form with some controls on it and a textbox called textbox1. then but this in the load event of the form textBox1.Clear(); IEnumerable<Control> query = from p in this.Controls.OfType<Control>() orderby p.TabIndex select p; foreach (Control c in query) { textBox1.AppendText(c.TabIndex + " " + c.Name + Environment.NewLine); }``` and you probably need these usings. ```csharp using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms;``` At first I couldn’t get it to work until I found this [LINQ – Query Windows Forms Controls by Sam Allen][1] and then the light came. I was missing the TypeOf method. This is really cool. No more reflection needed to get that control you realy need. [1]: http://dotnetperls.com/Content/LINQ-Windows-Forms.aspx

Read More...

How to rename a column in a SQL Server table without using the designer

If you have a table and you want to rename a column without using the designer, how can you do that? First create this table CREATE TABLE TestColumnChange(id int) INSERT TestColumnChange VALUES(1) SELECT * FROM TestColumnChange As you can see the select statement returns id as the column name. you can use ALTER table ALTER Column to change the dataype of a column but not the name. Here is what we will do, execute the statement below

Read More...

Bazaar Version Control System – better than Subversion ?

Version Control? A Version Control System (VCS) is an essential tool for a development team (or even individual developer) to manage their code. The more it can allow multiple developers to work together easily on the same code and manage the various states of that code, the better. One of the most popular Version Control systems in the past was CVS, however in recent times this has been superseded by Subversion (SVN), which is a highly popular VCS that is well supported by a variety of client tools and is often embedded within software configuration management applications to manage file versioning (e.g. trac, collabNet, etc)

Read More...

PHP: Writing a DAL the DAO way part 2

[Part one of this series.][1] I’m fairly new to the php development thing. But I’m learning fast. Of course I’m having to deal with newbie problems along the way. Today I was adding a class to my DAL. Nothing special, mainly copy paste stuff. But php wasn’t being very cooperative, it kept showing me an empty page whenever I added (require_once(“filepath”)) that file to my factory. Not very amusing. So after a little searching I found out that php doesn’t support classes with the same name. But how am I supposed to know that the same name already exists somewhere? In short you can’t.

Read More...

Solution folders are not being sorted alphabeticaly in Visual studio 2008

The Solution folders an apparently also the projects are not always sorted alphabetically. This is very annoying since my brain took years to get to this stage and it will take years to get used to them not being that way. And guess what I’m not the only one that has noticed this problem. There was a post on the MSDN forums An there is even a feedback about it but it is already closed as not being reproducible. So I guess I have to learn to live with this new feature.

Read More...

You may receive an error message, or the computer may stop responding, when you host Web applications that use ASP.NET on a computer that is running Windows Server 2003

Got an email from a friend who is suffering from this problem Here are the symptoms When you host Web applications that use Microsoft ASP.NET on a computer that is running Microsoft Windows Server 2003, you may experience decreased performance. This issue may occur when you host the Web applications in multiple application pools on a multiprocessor computer. Additionally, you may experience one or more of the following issues when available memory is low:

Read More...

Nice articles on The Codeproject this week

Because I want you all to save some time ;-). I selected a couple of very nice articles that I read on the codeproject site this week. First up [NHibernate Best Practices with ASP.NET, 1.2nd Ed. By Billy McCafferty]1. This is a great article about nHibernate but not only for you ASP.Neters but also for windowsforms and WPF users. nHibernate or any other ORM is a must have for any OO developer. I think that a good DAL (DataAccessLayer) should be usable for no matter what you throw at it. And this just confirms it 😉

Read More...

Printing to a zebra printer from VB.Net

This code is based on code I found from Rick Chronister, but I can’t find the article anymore. It was also using a deprecated method. I adapted to look like this. Imports System.IO Imports System.Runtime.InteropServices Namespace ZebraLabels ''' <summary> ''' This class can print zebra labels to either a network share, LPT, or COM port. ''' ''' Programmer: Rick Chronister ''' </summary> ''' <remarks>Only tested for network share, but in theory works for LPT and COM.</remarks> Public Class ZebraPrint #Region " Private constants " Private Const GENERIC_WRITE As Integer = &H40000000 Private Const OPEN_EXISTING As Integer = 3 #End Region #Region " Private members " ''' <summary> ''' ''' </summary> ''' <remarks></remarks> Private _SafeFileHandle As Microsoft.Win32.SafeHandles.SafeFileHandle ''' <summary> ''' ''' </summary> ''' <remarks></remarks> Private _fileWriter As StreamWriter ''' <summary> ''' ''' </summary> ''' <remarks></remarks> Private _outFile As FileStream #End Region #Region " private structures " ''' <summary> ''' Structure for CreateFile. Used only to fill requirement ''' </summary> <StructLayout(LayoutKind.Sequential)> _ Public Structure SECURITY_ATTRIBUTES Private nLength As Integer Private lpSecurityDescriptor As Integer Private bInheritHandle As Integer End Structure #End Region #Region " com calls " ''' <summary> ''' ''' </summary> ''' <param name="lpFileName"></param> ''' <param name="dwDesiredAccess"></param> ''' <param name="dwShareMode"></param> ''' <param name="lpSecurityAttributes"></param> ''' <param name="dwCreationDisposition"></param> ''' <param name="dwFlagsAndAttributes"></param> ''' <param name="hTemplateFile"></param> ''' <returns></returns> ''' <remarks></remarks> Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Integer, ByVal dwShareMode As Integer, <MarshalAs(UnmanagedType.Struct)> ByRef lpSecurityAttributes As SECURITY_ATTRIBUTES, ByVal dwCreationDisposition As Integer, ByVal dwFlagsAndAttributes As Integer, ByVal hTemplateFile As Integer) As Microsoft.Win32.SafeHandles.SafeFileHandle #End Region #Region " Public methods " ''' <summary> ''' This function must be called first. Printer path must be a COM Port or a UNC path. ''' </summary> Public Sub StartWrite(ByVal printerPath As String) Dim SA As SECURITY_ATTRIBUTES 'Create connection _SafeFileHandle = CreateFile(printerPath, GENERIC_WRITE, 0, SA, OPEN_EXISTING, 0, 0) 'Create file stream Try _outFile = New FileStream(_SafeFileHandle, FileAccess.Write) _fileWriter = New StreamWriter(_outFile) Catch ex As Exception System.Windows.Forms.MessageBox.Show("Can not find printer.", "Warning", Windows.Forms.MessageBoxButtons.OK, Windows.Forms.MessageBoxIcon.Error, Windows.Forms.MessageBoxDefaultButton.Button1) End Try End Sub ''' <summary> ''' This will write a command to the printer. ''' </summary> Public Sub Write(ByVal rawLine As String) If _fileWriter IsNot Nothing Then _fileWriter.WriteLine(rawLine) End If End Sub ''' <summary> ''' This function must be called after writing to the zebra printer. ''' </summary> Public Sub EndWrite() 'Clean up If _fileWriter IsNot Nothing Then _fileWriter.Flush() _fileWriter.Close() _outFile.Close() End If _SafeFileHandle.Close() _SafeFileHandle.Dispose() End Sub #End Region End Class End Namespace You use it like so.

Read More...

Nice article on ASP.NET MVC

It is out there and we need to learn it before it is to late. This article tells you how to do it better. I also made an entry on dzone.

Read More...

Interview/Exam questions part 3

[Read the previous post.][1] So lets move on to question number 5. The question was “If you have a collection of Time elements what do you have to do to put SportsTime elements in it.“ Of course the answer is nothing because we used inheritance we can have SportsTime elements in a Time collection. It’s called polymorphism and it’s one of the strong points of OOP. Question number 6 “I have a collection of Time which also contains SportsTime elements. I want to execute the method ToUniversalTime on it. Will I always get the same result. I yes elaborate and explain what you have to do to get the correct result. If No eleaborate.“

Read More...