## Lookups in PowerPivot Calculated Columns – Using the RELATED Function

In my previous post we looked at how Calculated Columns work in PowerPivot. One limitation you may have noticed though was all of the calculations were done using values in that individual table. What if you wanted to lookup a value in second table, based on a value in the first table, and return a value from that second table. Yes Virginia, not only is there a Santa Clause but there’s also an answer for us in PowerPivot’s RELATED function.

In addition to the standard Excel functions, PowerPivot provides a set of its own functions for working with its data. These new functions are collectively known as Data Analysis eXpressions, or DAX for short. By now you’ve probably guessed that the first function from the DAX toolbox you’ll want to learn is the RELATED function.

Let’s start with the same Excel 2010 workbook we had at the end of the lesson Combining Data from Multiple Sources in PowerPivot. If you recall we had imported data from the AdventureWorksLT2008 database. To that we added data the CountryInfo table, which we’d typed in to an Excel spreadsheet.

At the time we used this to get the CountryAbbr column, and you may have wondered why we also included a DiscountRate column. It’s this lesson where this DiscountRate will come in to play.

If you recall from that post, we used PowerPivot’s Manage Relationships feature to create a link that ultimately connected the SalesOrderDetail table to the CountryInfo table. This groundwork enables us to lookup values very easily. Here is a simple example. Go to the Add Column column of SalesOrderDetail and enter this formula into the fx area:

=RELATED(‘CountryInfo’[CountryAbbr])

When PowerPivot gets done, the abbreviation for each country to be shipped to appears in this column. This can be used to “flatten out” some of your data. However it’s much more useful when used as part of a calculation. Delete the column we just added (right click on the column header and pick Delete Column from the menu).

Looking in the CountryInfo table we see the DiscountRate. A value of 0.04 means our US customers get a discount of 4% off their LineTotal. So in our SalesOrderDetail table we want to take the LineTotal and calculate a new value based on the rate, which is stored in the CountryInfo table. Simple enough using the PowerPivot RELATED function.

=[LineTotal] * (1 – RELATED(‘CountryInfo’[DiscountRate]))

This is fairly simple math, we take the DiscountRate from CountryInfo (for US, 0.04) and subtract from 1 giving us 0.96, or 96%. This is then multiplied by the LineTotal giving us our new discounted LineTotal amount, which I renamed to DiscountedLineTotal.

Hopefully these simple examples will give you a glimpse of the immense power RELATED offers. It will allow you to combine data from multiple tables to create in-depth analysis that previously required a specialist in Business Intelligence to create.

## TechMixer University – SSIS for Developers

In addition to help recruit speakers, I also had the privilege of speaking at TechMixer University 2009.

The slide deck and main demo can be found at my Code Gallery site:

https://code.msdn.microsoft.com/Release/ProjectReleases.aspx?ProjectName=SSISForDevs&ReleaseId=2883

The calling of SSIS from .Net demo can be found at:

http://code.msdn.microsoft.com/ssisfromnet

Thanks to everyone who attended TechMixer University. I look forward to seeing you next year!

In the previous entry we covered how to upload a file to SQL Server using the FILESTREAM, new to SQL Server 2008. In this post we will look at retrieving a file from SQL Server using FILESTREAM. If you missed yesterday’s installment, a simple front end was created, the full project can be found at the Code Gallery site http://code.msdn.microsoft.com/FileStreamFTS .

The interface is very simple:

The grid is a Data View Grid that shows the ID and Document information from the table we previously created. (If you want to see the code to populate the grid see the project.) The user picks a row, then clicks on the Get File button.

private void btnGetFile_Click(object sender, EventArgs e)

{

// Reset in case it was used previously

lblStatus.Text = "";

if (dgvFiles.CurrentRow != null)

{

// Grab the ID (Primary Key) for the current row

int ID = (int)dgvFiles.CurrentRow.Cells[0].Value;

// Now Save the file to the folder passed in the second

// paramter.

FileTransfer.GetFile2(ID, @"D:\Docs\Output\");

// And let user know it’s OK

lblStatus.Text = "File Retrieved";

}

}

The code is very simple, the heart of it is the FileTransfer.GetFile static method. Two values are passed in, the integer ID, which is the primary key from the database, and the path to save the file to. Here I simply hard coded a path, in a real life application you will want to give the user the ability to enter a path. Let’s take a look at the GetFile routine.

public static void GetFile(int ID, string outputPath)

{

// Setup database connection

SqlConnection sqlConnection = new SqlConnection(

"Integrated Security=true;server=(local)");

SqlCommand sqlCommand = new SqlCommand();

sqlCommand.Connection = sqlConnection;

try

{

sqlConnection.Open();

// Everything we do with FILESTREAM must always be in

// creating one.

SqlTransaction transaction

= sqlConnection.BeginTransaction("mainTranaction");

sqlCommand.Transaction = transaction;

// The SQL gives us 3 values. First the PathName() method of

// the Document field is called, we’ll need it to use the API

// Second we call a special function that will tell us what

// the context is for the current transaction, in this case

// the "mainTransaction" we started above. Finally it gives

// the name of the document, which the app will use when it

// creates the document but is not strictly required as

// part of the FILESTREAM.

sqlCommand.CommandText

= "SELECT Document.PathName()"

+ ", GET_FILESTREAM_TRANSACTION_CONTEXT() "

+ ", DocumentName "

+ "FROM FileStreamFTS.dbo.DocumentRepository "

+ "WHERE ID=@theID ";

"@theID", SqlDbType.Int).Value = ID;

{

throw new Exception("Unable to get BLOB data");

}

// OK we have some data, pull it out of the reader into locals

int length = context.Length;

// Now we need to use the API we declared at the top of this class

// in order to get a handle.

SafeFileHandle handle = OpenSqlFilestream(

path

, SQL_FILESTREAM_OPEN_NO_FLAGS

, context

, (UInt32)length, 0);

// Using the handle we just got, we can open up a stream from

// the database.

FileStream databaseStream = new FileStream(

// This file stream will be used to copy the data to disk

FileStream outputStream

= File.Create(outputPath + outputFilename);

// Setup a buffer to hold the streamed data

int blockSize = 1024 * 512;

byte[] buffer = new byte[blockSize];

// There are two ways we could get the data. The simplest way

// is to read the data, then immediately feed it to the output

// stream using it’s Write feature (shown below, commented out.

// The second way is to load the data into an array of bytes

// (here implemented using the generic LIST). This would let

// you manipulate the data in memory, then write it out (as

// shown here), reupload it to another data stream, or do

// something else entirely.

// If you want to go the simple way, just remove all the

// fileBytes lines and uncomment the outputStream line.

List<byte> fileBytes = new List<byte>();

{

//outputStream.Write(buffer, 0, buffer.Length);

foreach (byte b in buffer)

}

// Write out what is in the LIST to disk

foreach (byte b in fileBytes)

{

byte[] barr = new byte[1];

barr[0] = b;

outputStream.Write(barr, 0, 1);

}

// Close the stream from the databaseStream

databaseStream.Close();

// Write out the file

outputStream.Close();

// Finally we should commit the transaction.

sqlCommand.Transaction.Commit();

}

catch (System.Exception ex)

{

MessageBox.Show(ex.ToString());

}

finally

{

sqlConnection.Close();

}

return;

}

The routine kicks off by opening a connection, then establishing a transaction. Remember from the previous lesson that every time you work with a FILESTREAM it has to be in a transaction. Next we basically duplicate the SQL used in the previous lesson, returning the path name, transaction context, and document name. The only difference is we pass in the ID as a parameter. With that, just like with the previous example we call the OpenSqlFilestream API. Note a difference, in this example the second parameter is “DESIRED_ACCESS_READ” as opposed to the write access we indicated previosly.

Once we have the “handle” we can create a FileStream for reading from the database. In this example I loop through the file stream, loading the data into a LIST of bytes. Once in memory we are free to work with it as we need to. In this example I simply loop back through the generic List and write the data to the file stream we opened on the disk for writing. If all you are doing is writing, it would be somewhat more efficient to write the code like so:

{

outputStream.Write(buffer, 0, buffer.Length);

}

// Close the stream from the databaseStream

databaseStream.Close();

I simply eliminate the local byte array and write the buffer directly to the disk. Either way, the remainder is simple, just closing all the streams, commiting the transaction and closing the database connection.

This concludes the series on how to use FILESTREAM, in future posts we look into how to do Full Text Search with FILESTREAM stored objects.

## Accessing FILESTREAM Data From A Client .NET Application – Part 1 Uploading a File

The best way to work with documents in a database is via a .Net application. I created a simple Windows forms project to access the table I created in previous lessons. I named the application FileLoader, you can the entire project at the Code Gallery site http://code.msdn.microsoft.com/FileStreamFTS .

The interface is very simple:

As you can see there are two main functions, the upper half uploads a file to the SQL Server. The lower half displays the files already in the table, lets the user pick one and then click the GetFile button to save it locally. Today we’ll look at the Upload File functionality. Here is the code:

private void btnUploadFile_Click(object sender, EventArgs e)

{

// Reset in case it was used previously

lblStatus.Text = "";

// Make sure user entered something

if (txtFile.Text.Length == 0)

{

MessageBox.Show("Must supply a file name");

lblStatus.Text = "Must supply file name";

return;

}

// Make sure what user entered is valid

FileInfo fi = new FileInfo(txtFile.Text);

if (!fi.Exists)

{

MessageBox.Show("The file you entered does not exist.");

lblStatus.Text = "The file you entered does not exist.";

return;

}

// Upload the file to the database

// Refresh the datagrid to show the newly added file

// Let user know it was uploaded

lblStatus.Text = fi.Name + " Uploaded";

}

The real line of importance is the FileTransfer.UploadFile. This calls a static method in a class I named FileTransfer.cs. In order to use FILESTREAM there is an API call we have to make, so at the header area of the FileTransfer we have a lot of declarations. These are pretty much a straight copy from the MSDN help files.

//These contants are passed to the OpenSqlFilestream()

//API DesiredAccess parameter. They define the type

//of BLOB access that is needed by the application.

const UInt32 DESIRED_ACCESS_WRITE = 0×00000001;

//These contants are passed to the OpenSqlFilestream()

//API OpenOptions parameter. They allow you to specify

//how the application will access the FILESTREAM BLOB

//data. If you do not want this ability, you can pass in

//the value 0. In this code sample, the value 0 has

//been defined as SQL_FILESTREAM_OPEN_NO_FLAGS.

const UInt32 SQL_FILESTREAM_OPEN_NO_FLAGS = 0×00000000;

const UInt32 SQL_FILESTREAM_OPEN_FLAG_ASYNC = 0×00000001;

const UInt32 SQL_FILESTREAM_OPEN_FLAG_NO_BUFFERING = 0×00000002;

const UInt32 SQL_FILESTREAM_OPEN_FLAG_NO_WRITE_THROUGH = 0×00000004;

const UInt32 SQL_FILESTREAM_OPEN_FLAG_SEQUENTIAL_SCAN = 0×00000008;

const UInt32 SQL_FILESTREAM_OPEN_FLAG_RANDOM_ACCESS = 0×00000010;

//This structure defines the format of the final parameter to the

//OpenSqlFilestream() API.

//This statement imports the OpenSqlFilestream API so that it

//can be called in the Main() method below.

[DllImport("sqlncli10.dll", SetLastError = true, CharSet = CharSet.Unicode)]

static extern SafeFileHandle OpenSqlFilestream(

string Filestreamath,

uint DesiredAccess,

uint OpenOptions,

byte[] FilestreamTransactionContext,

uint FilestreamTransactionContextLength,

Int64 AllocationSize);

//This statement imports the Win32 API GetLastError().

//This is necessary to check whether OpenSqlFilestream

//succeeded in returning a valid / handle

[DllImport("kernel32.dll", SetLastError = true)]

static extern UInt32 GetLastError();

OK, with that out of the way, I’ve created a public, static method to upload the file. Here is the full routine:

{

// Establish db connection

SqlConnection sqlConnection = new SqlConnection(

"Integrated Security=true;server=(local)");

SqlTransaction transaction = null;

// Create a File Info object so you can easily get the

// name and extenstion. As an alternative you could

// choose to pass them in,  or use some other way

// to extract the extension and name.

FileInfo fi = new FileInfo(fileName);

try

{

// Open the file as a stream

FileStream sourceFile = new FileStream(fileName

// Create the row in the database

sqlConnection.Open();

SqlCommand cmd = new SqlCommand();

cmd.Connection = sqlConnection;

cmd.CommandText = "INSERT INTO "

+ "FileStreamFTS.dbo.DocumentRepository"

+ "(DocumentExtension, DocumentName) VALUES (‘"

+ fi.Extension + "’, ‘"

+ fi.Name + "’)";

cmd.ExecuteNonQuery();

// Now upload the file. It must be done inside a transaction.

transaction = sqlConnection.BeginTransaction("mainTranaction");

cmd.Transaction = transaction;

cmd.CommandText = "SELECT Document.PathName(), "

+ "GET_FILESTREAM_TRANSACTION_CONTEXT() "

+ "FROM FileStreamFTS.dbo.DocumentRepository "

+ "WHERE ID=(select max(id) from FileStreamFTS.dbo.DocumentRepository)";

{

throw new Exception("Could not get file stream context");

}

// Get the path

string path = (string)rdr[0];

// Get a file stream context

byte[] context = (byte[])rdr[1];

int length = context.Length;

rdr.Close();

// Now use the API to get a reference (handle) to the filestream

SafeFileHandle handle = OpenSqlFilestream(path

, DESIRED_ACCESS_WRITE

, SQL_FILESTREAM_OPEN_NO_FLAGS

, context, (UInt32)length, 0);

// Now create a true .Net filestream to the database

// using the handle we got in the step above

FileStream dbStream = new FileStream(handle, FileAccess.Write);

// Setup a buffer to hold the data we read from disk

int blocksize = 1024 * 512;

byte[] buffer = new byte[blocksize];

// Read from file and write to DB

{

dbStream.Write(buffer, 0, buffer.Length);

}

// Done reading, close all of our streams and commit the file

dbStream.Close();

sourceFile.Close();

transaction.Commit();

}

catch (Exception e)

{

if (transaction != null)

{

transaction.Rollback();

}

throw e;

}

finally

{

sqlConnection.Close();

}

}

First we open a connection to the SQL Server, then create a FileInfo object to make it simple to extract the file name and extension. Next a record is inserted into the database that will act as a place holder. It has the name of the file and the extension, but no file yet. I did go ahead and open a FileStream to the source file, located on the disk. We’ll need this later to upload the file.

Next you will see that I begin a transaction. Every time you work with a FILESTREAM it must always be in the context of a transaction. After that a SQL Data Reader is created that has three pieces of information. First, it calls the PathName() function for the Document field in our table. The PathName() will be needed later when we call the API. The second field is returned from the GET_FILESTREAM_TRANSACTION_CONTEXT function, and returns the transaction context for the transaction. Note this is not the name (in this example “mainTransaction”), but the context which is a special value. These two values are then copied into local variables which will be used in calling the OpenSqlFilestream API. In this example I also retrieve the DocumentName field, this is used by the code when it writes the file to the database, but is not strictly needed for the FILESTREAM.

Next you will see the call to the OpenSqlFilestream API, which returns a “handle”. This handle is then used to create a FileStream object. Using this newly created FileStream (here named dbStream) we can then upload the file. Now the main work begins. After setting up a buffer, we then simply read from the source file stream into the buffer, then write the exact same buffer to the database FileStream. The loop continues until there are no more bytes in the source.

At this point we are essentially done. We close the streams, commit the transaction, and in the finally block close the SQL database connection. The file should now be in the database. I do want to point out one thing. In the SQL to get the information to the row just uploaded, I use a subquery to get the max(id), essentially returning the last row just inserted. This is fine for this simple example, when the database has just one user. In your production systems where you are likely to have many users, however, you should use an alternate method to return the row you need. Otherwise two users could insert rows at the same time, and thus a conflict could occur with both of them getting back the same max(id). It will not happen often, but at some point it could happen and be very hard to debug.

This handled the uploading of files to the SQL Server via FILESTREAM, in the next installment we’ll look at how to retrieve the file we just uploaded.

## Generating a PDF file from a Reporting Services Report Viewer Control

In yesterday’s post, I demonstrated how to generate a SQL Server Reporting Services report without having to have SQL Server Reporting Services. The sample application used the Microsoft Report Viewer control to display the report. A common need in business is to generate documents, such as PDFs, that will later be archived. It turns out if you are using a report viewer control, this is easy to do programmatically.

First, you need to add one using statement to the top of your class, in addition to the others that were added yesterday.

using System.IO;

Next, we only need a few lines of code to generate the PDF.

Warning[] warnings;

string[] streamids;

string mimeType;

string encoding;

string extension;

byte[] bytes = reportViewer1.LocalReport.Render(

"PDF", null, out mimeType, out encoding, out extension,

out streamids, out warnings);

FileStream fs = new FileStream(@"D:\ReportOutput.pdf", FileMode.Create);

fs.Write(bytes, 0, bytes.Length);

fs.Close();

This code snippet came right from the MSDN Books on Line, and is pretty simple. I could have selected another format by changing the first value passed into the Render method, for example “EXCEL” would have rendered it as a Microsoft Excel document.

In the code samples I placed the above sample in it’s own button, but I could just have easily placed it under one of the other demo buttons.

This ability brings up some interesting possibilities. For example, the report viewer control does not have to be visible to the user in order for this to work. Thus you could create an application that every night generated a series of reports and saved them as PDFs to some central location, such as a web server or document control server. All the user (assuming one was around) would have to see is a progress bar, the reports themselves never get displayed.

## Using SQL Server Reporting Services in Client Mode

Recently I did a presentation at the March BSDA meeting. I showed how to use SQL Server Reporting Services without a SQL Server, or more specifically a SQL Server running Reporting Services. It got an enthusiastic response so I thought I’d add to it here by adding some reminder documentation, as much for myself as for all of you wonderful readers.

Using Reporting Services in Client, or Local mode is a 4 step process. First, you will need an XSD schema file to create the report on. Once you have the XSD you will be able to move to the second step, creating the report. Third you will need to place a Report Viewer control on your windows form, WPF form, or ASP.NET page. Finally you will need to write some code that generates an ADO.NET dataset, loads the report in the report viewer control, then binds it all together. Lets look at this step by step.

Normally when you create a report you connect to a database, then base it off of some object like a query, view, or stored procedure. The report is then uploaded to a Reporting Services server, which takes care of hosting it, displaying it, and generating the data for it. With client mode you have no server available, so we have to instead create a surrogate. That’s where our XSD file comes in.

Right click in Solution Explorer and “Add a new item”, and from the list of goodies select “XML Schema”. Name it something appropriate, letting the default extension be XSD. For this example I will be getting customer order data, so I’ve given it the name CustomerOrders.xsd. Visual Studio will think about it then add it to the project, and even helpfully open it for you. I don’t know about you, but hand typing XML Schema’s isn’t my idea of fun, so you should glace at it, go “that’s nice” then close it.

Now right click on the XSD file in Solution Explorer, and pick “Open with….”. In the dialog that appears, select “Dataset Editor”. When you do, Visual Studio presents a big scary warning message letting you know that you could lose contents, and that this will forever be a dataset XSD file. We have nothing in the file, so we’re cool with this, just click OK.

You will now be presented with a big surface area. In the middle it tells you to drag items from the server explorer or right click. If you have a table, view, or stored procedure you are free to drag it in, but most of the time you’ll want to base this off of a SQL query. Right click on the surface, and select Add…., Table Adapter from the menu. The first screen asks you for the database connection. This is the only time you’ll actually need a connection, in this example I am using the good old Northwind database. I pointed at Northwind and clicked next.

Next we are asked how we are going to access the data. Since we have a SQL Statement just pick the default of “Use SQL statements” and click Next.

Now take your SQL Statement and paste it in, and click Next.

OK, click Finish to wrap up the addition of the XSD. By default the adapter has a generic name, we should give ours something more meaningful. Click in the top bar, then enter a new name. Since my example report is for customer order data, I’ll name it CustomerOrders. I then went to the bottom bar and renamed the TableAdapter1 to CustomerOrderTableAdapter. You should now see something like:

Note that this will become your Data Source for the report. The data source will have the name of the XSD followed by the name of the source, in this case it will read CustomerOrders_CutomerOrders. OK, now it’s time to create the report.

Go back to Solution Explorer, right click and pick Add New Item. Navigate to the Reporting area and pick Report Wizard. Note the file extension should end in RDLC. If you have used Reporting Services before, you will know that reports typically end in RDL. However, client mode reports have just a slightly different syntax to them, thus the RDLC extension to differentiate the two. While you can modify an RDL to become an RDLC and vice versa, you have to do so by hacking the XML behind the report.

Note you can also choose just Report, but then you’ll have to setup everything manually. For this simple example though, we’ll just use the Report Wizard.

Give your report a meaningful name and click Add. The report wizard then shows you a welcoming screen if you’ve never run it before, just click Next.

Now we need to pick the data source. In this example, you want the CustomerOrders branch, so select it and click Next.

The next screen asks if we want a Tabular or Matrix report. Select the one for you, in my example I picked Tabular and clicked Next. The next screen asks how we want to display the data. For my example, I opted to group by the customers company name and contact name, then the order data went into the details area. Fill out as appropriate for your report and click Next.

The next screen asks how we want things laid out. This affects the look and feel of the report. For my example I just took the default and clicked next, however you are free to play with this to experiment with the different looks and feels your reports might have.

Likewise the next screen is also a look and feel one, asking what colorings we want to apply. Pick one that makes you happy and click next. You can always change it later, many times I pick the Generic one (which adds no colors) then fix it up afterward.

The final screen is the wrap up. Give your report a meaningful name and click Finish.

OK, you have a report, now you need a container. Open up the user interface you want to place the report viewer control on. In my example I went with a very simple Windows Forms application.

In my toolbox, I navigated to the Reporting section, where I only found one control, the MicrosoftReportViewer control. (Note I am using Visual Studio 2008 SP1, if you are on an earlier version your names may differ slightly). Grab it and drop it onto your design surface. I also added a Button control to the form to kick off the report display process.

Now it’s time for the last step, adding some code. In this example I’ve used a Windows Form. Opening it, the first thing we find in the form load area is::

this.reportViewer1.RefreshReport();

(Note I left my report viewer control named reportViewer1.) Delete it, we’ll have it refresh elsewhere.

Now we need to add some using statements to the top of our class.

//Add these to the standard list above

using System.Data.Sql;

using System.Data.SqlClient;

using Microsoft.Reporting.WinForms;

The first two will be used in accessing our Northwind database, you may need to use different libraries if you were going to another database. I’ve also included a referenced to the Reporting.WinForms library so we can manipulate the report programmatically.

Now let’s go to the code for the button click event. First, we need to reset the report viewer in case we’d been using it to host another report.

// Reset in case report viewer was holding another reportViewer1

reportViewer1.Reset();

Next We need to set the report viewer to local mode. This tells it we’ll be supplying the report name from a local file, and binding the report to a local ADO.NET datasource.

// Set the processing mode for the ReportViewer to Local

reportViewer1.ProcessingMode = ProcessingMode.Local;

Our third step is to create a local report variable, and set it’s reference to the report viewer’s local report. This will make it easier to work with. Then we’ll set the location of the report we want to use.

LocalReport localReport = reportViewer1.LocalReport;

localReport.ReportPath = @"D:\Presentations\SQL Server\SSRS RDLC\SSRS_RDLC\Report2.rdlc";

Now we need to create an ADO.Net dataset, and populate it. I implemented most of that functionality in a method called GetCustomerOrders, which I’ll append at the bottom of these instructions. It’s very straight forward code.

DataSet dataset = new DataSet("Northwind");

// Get the sales order data

GetCustomerOrders(ref dataset);

At this stage we have told it where our report is, and have created the dataset. Now we need to create a datasource for the report itself. We’ll use the ReportDataSource object. For the name, we’ll use the same name as the XSD schema, CustomerOrders_CustomerOrders. Then for the value we will give it the table from the dataset we created in code. It’s possible for a report to have multiple datasets, in the report we’d give each one it’s own name (based on the XSD) then here we’d bind the dataset table to the name we’d used in the report. Once done we will then add the new ReportDataSource to the local reports DataSources collection. Finally, we’ll referesh the report viewer to make it generate the report.

// Create a report data source for the sales order data

ReportDataSource dsCustomers = new ReportDataSource();

dsCustomers.Name = "Customers_Customers";

dsCustomers.Value = dataset.Tables["Customers"];

// Refresh the report

reportViewer1.RefreshReport();

You can download a copy of these instructions, along with the entire sample project including code and reports, at the Microsoft Code Gallery site http://code.msdn.microsoft.com/SqlServerRSClient . As promised, below is a copy of the GetCustomerOrders routine, for your reference.

private void GetCustomerOrders(ref DataSet dsNorthwind)

{

string sqlCustomerOrders = "SELECT c.[CustomerID]"

+ " ,c.[CompanyName]"

+ " ,c.[ContactName]"

+ " ,c.[ContactTitle]"

+ " ,c.[City]"

+ " ,c.[Region]"

+ " ,c.[PostalCode]"

+ " ,c.[Country]"

+ " ,c.[Phone]"

+ " ,c.[Fax]"

+ " ,o.[OrderID]"

+ " ,o.[CustomerID]"

+ " ,o.[EmployeeID]"

+ " ,o.[OrderDate]"

+ " ,o.[RequiredDate]"

+ " ,o.[ShippedDate]"

+ " ,o.[ShipVia]"

+ " ,o.[Freight]"

+ " ,o.[ShipName]"

+ " ,o.[ShipCity]"

+ " ,o.[ShipRegion]"

+ " ,o.[ShipPostalCode]"

+ " ,o.[ShipCountry]"

+ "  FROM [Northwind].[dbo].[Customers] c"

+ "  join [Northwind].[dbo].[Orders] o on c.CustomerID = o.CustomerID";

SqlConnection connection = new

SqlConnection("Data Source=(local); " +

"Initial Catalog=Northwind; " +

"Integrated Security=SSPI");

SqlCommand command =

new SqlCommand(sqlCustomerOrders, connection);

}

## Presenting Getting Started with SQL Server Compact Edition 3.5 at BUG.NET Meeting

Just wanted to let everyone know I’ll be doing a presentation this coming Tuesday night, August the 12th for the Birmingham .Net Users Group (BUG.NET). My topic, as you may have guessed from the title, will be using SQL Server Compact Edition.

While I will be using Visual Studio 2008, I will point out which pieces are 2005 compatible. I will also cover the use of both traditional coding techniques as well as how to use LinqToSQL to talk to the Compact Edition.

The meeting takes place at 6:30 pm at New Horizons Training Center in Homewood.

I also plan a new series of blog posts to start later this week on the subject, and will be creating a new Code Gallery site to hold my examples.

Also, don’t forget the regular BSDA meeting this coming Thursday night, the 14th. Also starting at 6:30 pm at New Horizons, Shannon Brooks-Hamilton, a software usability expert, will be there to talk about user interface design. Lots of good thought material on how we can make better UIs for our users.

## SQL Server 2005 Full Text Searching at the Huntsville Alabama Code Camp

My third and final presentation for the Alabama Code Camp 6 is “Introduction to SQL Server Full Text Searching”. Here are the materials I’ll be using during the demo.

First, here is a PDF of the PowerPoint slides:

Full Text Search Power Points

Next, most of the demos used SQL statements. This PDF file has all of the SQL plus some associated notes.

Full Text Search Demo Scripts

Finally, I did a WPF project that demonstrated how to call a full text search query from a WPF Windows application. Annoyingly enough WordPress (who hosts my blog) won’t let me upload ZIP files, so I renamed the extension to pdf. After you download the file to your drive, remove the .pdf and put the zip extension back on, then it should expand all the source for you correctly. (Yes, I know, I really need to get a host server for binaries, one of these days I’ll get around to it, but for today…)

Source for WPF Demo

## An Early Christmas from Redmond

They’re here! Today Microsoft released Visual Studio 2008 RTM. If you have an MSDN subscription you can download today and start producing all those new .Net 3.5 applications.

Also released today via the connect.microsoft.com site is CTP 5 of SQL Server 2008. It’s my understanding in this CTP most of the features are working, except for clustering.

I can see I won’t be getting any sleep tonight.

## Little Bobby Tables

I love this cartoon from xkcd, it really emphasizes why you need to screen your data inputs to protect against SQL Injection Attacks.

By the way, there’s a WebLog Awards going on right now, if you also enjoy xkcd give them a vote. http://2007.weblogawards.org/polls/best-comic-strip-1.php Hurry though, voting ends November 8th.

## Code Camp Samples

Tomorrow, Saturday October 6th I will be presenting “Getting Started with Full Text Searching”. Here are the materials I’ll be using during the demo.

First, here is a PDF of the PowerPoint slides:

Full Text Search Power Points

Next, most of the demos used SQL statements. This PDF file has all of the SQL plus some associated notes.

Full Text Search Demo Scripts

Finally, I did a WPF project that demonstrated how to call a full text search query from a WPF Windows application. Annoyingly enough WordPress (who hosts my blog) won’t let me upload ZIP files, so I renamed the extension to pdf. After you download the file to your drive, remove the .pdf and put the zip extension back on, then it should expand all the source for you correctly. (Yes, I know, I really need to get a host server for binaries, one of these days I’ll get around to it, but for tonight…)

Source for WPF Demo

## Arcane Fun Fridays

WHEW! All of this WPF / XAML sure has been a lot of fun. But I think it’s time to come up for air and see what else is happing out there in Dot Net land.

Alabama Code Camp is coming up in just a little over a week, Saturday October 6th to be exact. Still plenty of time to register and even just a bit of time if you want to get in on the Silverlight programming contest. First prize for that is a Zune! http://www.alabamacodecamp.com/home.html

devLink, the large conference for a cheap price comes up right afterward in Nashville, Friday and Saturday October 12th and 13th. http://www.devlink.net/ . You can tell I’ll be there, my name’s on the front page as a winner of a Barnes and Nobel gift card (look for the dude from AL !)

(By the way, anyone know of a good dog repellent? My nephew is coming to house sit and is bringing Marshmallow and Buttercup, his twin Dobermans along because I have a big back yard they can play in. Last time though they ate the garden hose, chewed the handle off my shovel, and bit through one of my lawnmower tires.)

There’s a new add-on for SQL Server Management Studio I’m eager to try out. It’s still in Beta but looks promising. It was blogged about at http://weblogs.sqlteam.com/mladenp/archive/2007/09/20/SSMS-Tools-Pack—an-add-in-for-SQL-Management-Studio.aspx or you can download it directly at http://www.ssmstoolspack.com/ .

If you are a fan of NUnit, you’ll appreciate the new xUnit. Read James’ announcement at http://jamesnewkirk.typepad.com/posts/2007/09/announcing-xuni.html .

In a recent Dot Net Rocks episode, Carl Franklin announced they would be taking over Shrinkster.com. Shrinkster has been down due to spam abuse, as soon as Carl gets everything setup we’ll be able to go back to using short links again!

Speaking of Dot Net Rocks, I especially enjoyed show 274, where the new features of VB.Net and C# for the 2008 release were discussed. Entertaining and lots of good tidbits. I think my favorite feature so far has got to be C#’s extension methods. http://www.dotnetrocks.com/default.aspx?showNum=274

During my long drive to the Tallahassee Code Camp last week, I put together a podcast theme session, and copied a bunch of related podcasts onto my cheapo SanDisk mp3 player. This time I went with a “Millenator” theme and got all the episodes of Dot Net Rocks that Mark Miller appeared on. Good stuff, lots of thoughtful material combined with some humor. Next time you go on a trip, copy a bunch of past episodes of your favorite podcast that are in the same theme and make that long drive go much quicker.

There have been several updates to the world’s greatest Visual Studio Add-In, CodeRush, over the last few weeks ( http://www.devexpress.com/Home/Announces/CodeRush25.xml ). Apparently Mark Miller and the boys have been busy! If you’re not on 2.5.4 go update yours today.

Speaking of Mark Miller, I love his intro slide for his VSLive session coming up in LasVegas. Take a look, pure genius. http://www.doitwith.net/2007/09/11/MyLastVSLiveSessionEver.aspx

A final note, between getting ready for Alabama Code Camp and going to devLink my blogging may get spotty for the next few weeks, bear with me and I’ll have full reports from both code camps and lots of fun new stuff to share.

The next control in the basic toolkit is the menu. Menus are much like a series of nested buttons in the way you deal with them. Let’s create a simple menu. I’ve added a DockPanel, so we could nest our menu at the top, a very common scenario.

<DockPanel>

</DockPanel>

The underscore acts as an indicator to underline in the menu, designating a “hot key”. When the user hits the ALT key, they are then able to combine with the hot key to activate the menu option. ALT+F opens the File menu, O will then trigger the open.

Responding to the users click is just like working with many other controls, simply add a Click=”” in the MenuItem. Above you will notice I did this for one item, the Exit menu choice under File. Here’s the code I created in C#:

void mnuFileExit_Click(object sender, RoutedEventArgs e)

{

this.Close();

}

Since all the other menus work the same way I won’t bother to wire them up, but you get the idea.

It’s also possible to insert a separator, a visual line the separates one menu option from the rest. In this example, I think Exit should be separated from the other options.

<!– …snip… –>

<Separator></Separator>

<!– …snip… –>

And now we have a nice clean line across the menu:

OK, I can hear you now, “Hey, this is WPF, what about all the pretty pictures?” No problem, the menus support the addition of icons to each item.

One thing I’ve found many developers don’t realize is that Visual Studio actually ships with a nice array of graphics for your use. If you are using Visual Studio 2005, navigate to the C:\Program Files\Microsoft Visual Studio 8\Common7\VS2005ImageLibrary folder. There you will find a zip file named VS2005ImageLibrary.zip.

I extracted the icons folders to my D drive, as you’ll notice in the next code snippet. Adjust the sample according to where you decide to put them. I did find that on Vista, I needed to move them out of my Program Files folder or they didn’t render correctly, a security permissions issue.

<Image Height=16 Width=16

Source=D:\Icons\WinXP\folderopen.ico />

<Image Height=16 Width=16

Source=D:\Icons\Win9x\DISK06.ico />

<Separator></Separator>

For the MenuItems I wanted to add icons to, I need to define the MenuItem.Icon tag, then inside place an Image. The Source property I set to files I’d mentioned earlier. Note also I explicitly set the Height and Width to 16×16, in order to make them fit nicely into the menu. However, you are free to make them any size you wish, the menu item height will adjust automatically to compensate.

Ah, a work of art even Leonardo da Vinci would be proud of. But our artistic menus are not limited to the the top of the Window. It’s also possible to attach a menu to nearly any control, in the form of a ContextMenu. ContextMenus appear when you Right Click with the mouse on the control.

<DockPanel>

<!–Menu omittied for brevity, same as previous–>

<Grid>

<Grid.RowDefinitions>

<RowDefinition></RowDefinition>

<RowDefinition></RowDefinition>

</Grid.RowDefinitions>

<Grid.ColumnDefinitions>

<ColumnDefinition></ColumnDefinition>

<ColumnDefinition></ColumnDefinition>

</Grid.ColumnDefinitions>

<TextBlock Grid.Row=0

Grid.Column=0

Grid.ColumnSpan=2>

Enter some text, right click for menu options.

</TextBlock>

<TextBox Grid.Row=1 Grid.Column=0>

<Separator></Separator>

<Image Height=16 Width=16

Source=D:\Icons\Win9x\DISK06.ico />

</TextBox>

</Grid>

</DockPanel>

I’m adding a Grid to the body of the DockPanel. In the Grid I’m putting a TextBlock with some instructions, then a TextBox. Try right clicking on the TextBox, and take a look at what you get:

To get this to work, I first had to create a ContextMenu tag specific for the control, in this case TextBox.ContextMenu. Within that tag I was then able to place my ContextMenu. Inside it I treated it just like a normal Menu control, the code in there I simply copied from the other menus. I could even have the Click events route to the same code in the code behind module if I wished.

It’s worth pointing out that a menu may appear anywhere on the Window. Here I will create a menu to the right of the TextBox, in the next column of the Grid.

<DockPanel>

<!–Omitted, see previous–>

<Grid>

<!–Omitted, see previous–>

<TextBox Grid.Row=1 Grid.Column=0>

<!–Omitted, see previous–>

</TextBox>

</Grid>

</DockPanel>

The menu code is the same as before, except I located in the Grid instead of at the top of the Window.

Virtually any application of decent size will need to employ a menu structure of some kind. Getting to know the basics of menu controls will get you ready for that first big WPF app.

## WPF Tab Control

Continuing the series on visual grouping controls in WPF, the Tab control is a common UI element that has been around for some time. It makes a convenient way to organize your window when there is more than could realistically fit and still be comprehensible.

In WPF, Tabs are very easy to implement. Create a new WPF Window, remove the default Grid tags, and add the following XAML:

<TabControl>

</TabControl>

Run the app, and you’ll see the two tabs on a window:

The TabItem, just like most controls, can hold a container control that can hold much more, thus making the tab really useful. In this example, let’s add a third tab item, with a grid. We’ll put in a few text boxes and a button.

<TabControl Name=tabMyTabs >

<Grid>

<Grid.RowDefinitions>

<RowDefinition></RowDefinition>

<RowDefinition></RowDefinition>

<RowDefinition></RowDefinition>

</Grid.RowDefinitions>

<Grid.ColumnDefinitions>

<ColumnDefinition Width=*></ColumnDefinition>

<ColumnDefinition Width=2*></ColumnDefinition>

</Grid.ColumnDefinitions>

<Label Grid.Column=0 Grid.Row=0

HorizontalAlignment=Right>

First Name:

</Label>

<Label Grid.Column=0 Grid.Row=1

HorizontalAlignment=Right>

Last Name:

</Label>

<TextBox Name=FirstName

Grid.Column=1

Grid.Row=0 />

<TextBox Name=LastName

Grid.Column=1

Grid.Row=1 />

<Button  Grid.Column=1

Grid.Row=2

Height=23 Width=75

HorizontalAlignment=Left

Name=CoolTabButton

Click=CoolTabButton_Click

VerticalAlignment=Top>

OK

</Button>

</Grid>

</TabItem>

</TabControl>

Using techniques already described in my post on Grids (http://arcanecode.wordpress.com/2007/08/28/grid-yourself-its-wpf/ , in case you missed it) I was able to create a tab with some useful content. Running it, you can see the results for yourself:

It’s also possible to determine the current tab via code. Let’s wire up a click event to that OK button you see. This requires we name the tab (I called it tabMyTabs), and the button, and add a Click=”CoolTabButton_Click” to the button declaration. Now all we have to do is add a little code:

private void CoolTabButton_Click(object sender, RoutedEventArgs e)

{

TabItem ti = tabMyTabs.SelectedItem as TabItem;

}

Running and clicking on the OK button will result in a message box with the words “Tab Index=Cool Tab”

Granted my design won’t win any awards, but it gives you the general idea of how to use a tab control in WPF.

Being an old VB coder, I fondly remember this control as the Option Button, but these days it’s been renamed to the RadioButton. They are used to present the user with a set of mutually exclusive options.

Adding them is simple, just use the <RadioButton> tag. For the button you wish to be the default (if any) you add the IsChecked=”true” flag.

WPF adds a huge improvement over it’s predecessor. No longer are you forced to use a container control to group your radio buttons. Instead, WPF adds a GroupName property. Take a look at this example:

<StackPanel>