Monday, May 12, 2008

C# Have a constructor call a constructor - constructor overloading

If you are a beginner C# programmer; especially coming from the VB/VB.net world, this could throw you if you do not know how. In VB, you can call a constructor from within another constructor by using me.New(arguments) statement.

In C#, you add the :this(arguments) to the constructor, and leave its body blank.

So:


public class ReportGenerator
(
public ReportGenerator(String A, String B, String C)
{
// actual constructor code goes here
}
// overloaded constructor, only two arguments
// String C can be passed as String.Empty, or a default Value "value"
public ReportGenerator(String A, String B)
:this(A, B, YourDefaultValueForStringC)
{
}
// another overloaded constructor, only one argument
// Send one additional default argument, to the constructor above.
public ReportGenerator(String A)
:this(A, YourDefaultValueForStringB)
{
}

}

Thursday, May 1, 2008

Windows SysInternals from Microsoft

Having used Sysinternals tools for a long time, I kept forgetting where they were. Seeing them now at the Sysinternals section on TechNet , this is a short blog post so I can always find it. Hope it is useful to you too.

From the Sysinternals website:


The Sysinternals web site was created in 1996 by Mark Russinovich and Bryce Cogswell to host their advanced system utilities and technical information. Microsoft acquired Sysinternals in July, 2006. Whether you’re an IT Pro or a developer, you’ll find Sysinternals utilities to help you manage, troubleshoot and diagnose your Windows systems and applications. If you have a question about a tool or how to use them, please visit the Sysinternals Forum for answers and help from other users and our moderators.

Friday, April 25, 2008

Looking for Northwind and Pubs databases for SQL 2005?

If you are like me testing out code with sample data from the Customers table in the Northwind database, and you sorely miss not having it in SQL Server 2005, now you can.

Simply download it from the following and install. Although it says SQL 2000, the scripts can be used with SQL 2005 and SQL 2005 Express editions.

Hope this helps someone.

Northwind and pubs Sample Databases for SQL Server 2000


http://www.microsoft.com/downloads/details.aspx?
FamilyId=06616212-0356-46A0-8DA2-EEBC53A68034&
displaylang=en


Wednesday, April 23, 2008

Read Excel File into ADO.Net Dataset without using Excel Automation

Need a quick way to suck an Excel file into an ADO.Net dataset without having to muck around with Excel Automation? Here's how.


' -- begin declare variables
Dim ds As DataSet
Dim dtTables As DataTable
Dim cn As System.Data.OleDb.OleDbConnection
Dim da As System.Data.OleDb.OleDbDataAdapter
Dim cmd As System.Data.OleDb.OleDbCommand
Dim strTable As String
Dim strXLConnString As String
' -- end declare variables

strXLConnString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=""YOUREXCELFILENAMEHERE"";Extended Properties=""Excel 8.0;IMEX=1;MaxScanRows=500;"""

' -- get tables (sheets) from our excel file
cn = New System.Data.OleDb.OleDbConnection(strXLConnString)
cn.Open()
dtTables = cn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, Nothing)

' -- now get the data: Rows(0) indicates first worksheet available.
strTable = dtTables.Rows(0)("TABLE_NAME").ToString
cmd = New System.Data.OleDb.OleDbCommand
cmd.Connection = cn
da = New System.Data.OleDb.OleDbDataAdapter(cmd)

' -- adjust your sql below if you know field (column) names
ds = New DataSet
cmd.CommandText = "SELECT * FROM [" & strTable & "]"
da.Fill(ds)



And that's it. The dataset ds now contains the data from the excel sheet.

Monday, April 21, 2008

Excel Like Filters using C1 True DBGrid

I have been exploring using Excel Style filters using the Component One (C1) True DBGrid. The samples that ship with the Grid show how to do this for one column at load time. The following code automatically generates Excel Like AutoFilter combo boxes for every column in your data grid. For best results, add this code in the DataSourceChanged event so that the filters get set up properly:


For each dc as C1.Win.C1TrueDBGrid.C1DataColumn in Me.C1TrueDBGrid1.Columns
Dim c As C1.Win.C1TrueDBGrid.C1TrueDBDropdown
c = New C1.Win.C1TrueDBGrid.C1TrueDBDropdown
CType(c, System.ComponentModel.ISupportInitialize).BeginInit()
' c
c.AllowColMove = True
c.AllowColSelect = True
c.AllowRowSizing = C1.Win.C1TrueDBGrid.RowSizingEnum.AllRows
c.AlternatingRows = False
c.ColumnCaptionHeight = 20
c.ColumnFooterHeight = 20
c.DataSource = Me.C1TrueDBGrid1.DataSource
c.DataMember = Me.C1TrueDBGrid1.Columns(c).DataField
c.ExtendRightColumn = True
c.FetchRowStyles = False
c.Location = New System.Drawing.Point(208, 112)
c.Name = "ddwn" & c
c.RowDivider.Color = System.Drawing.Color.DarkGray
c.RowDivider.Style = C1.Win.C1TrueDBGrid.LineStyleEnum.[Single]
c.RowSubDividerColor = System.Drawing.Color.DarkGray
c.ScrollTips = False
c.Size = New System.Drawing.Size(100, 88)
c.TabIndex = 1
c.Text = "ddwn" & c
c.Visible = False
Me.C1TrueDBGrid1.Controls.Add(c)
CType(c, System.ComponentModel.ISupportInitialize).EndInit()
With Me.C1TrueDBGrid1.Columns(c)
.DropDown = c
.FilterDropdown = True
End With
' turn on the dropdown button in the filterbar
Me.C1TrueDBGrid1.Splits(0).DisplayColumns(c).FilterButton = True
' turn it off for regulars cells in the grid
Me.C1TrueDBGrid1.Splits(0).DisplayColumns(c).Button = False
c.ColumnHeaders = False
Next



Now when you assign a DataSource to your grid, you will automatically get Excel Style filters on your True DBGrid.

Hope this helps someone.

Wednesday, March 5, 2008

Hello World

I had to do it. Finally a place on the web to reach out. Hello World. Here I come.