Category: C#

EF Framework core for ling2sql users

Linq2Sql had a few extremely practical tools such as sqlmetal enabling you to generate code form an existing database with a single command, replace a single file and be done with it. EF Core seems to align with the trends lately to make everything more cumbersome, but you can stil use a workflow much like it. Creating a data DLL 1) Create a new empty net core dll 2) Install nuget packages for EF Core: […]

Using backgroundworkers with progress reporting and cancelation

With the release of VS11 around the corner and the new async features in there everyone will have a wide toolset for writing non blocking responsive applications. If you have not yet played with the beta versions or regular threads (see for example Threads and the threadpool then the backgroundworker is the most basic component to help you get started. Minimal example with parameter passing and error handling This examples shows how to: Run a […]

Coloring individual cells or rows in a DX Gridcontrol based on a cell value

Coloring individual cells This sample hooks into the RowCellStyle event of the Gridview, and evaluates the value of the cell. Depending on the outcome, a background color is assigned to the cell. private void gridViewQueue_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e) { var oView = (GridView)sender; if (e.Column.FieldName == “task_stamp”) { Color oBgcolor; var dtValue = DateTime.Parse(oView.GetRowCellValue(e.RowHandle, “task_stamp”).ToString()); if (dtValue < DateTime.Now) { oBgcolor = Color.LightGreen; } else { oBgcolor = Color.LightYellow; } if (dtValue.Hour == 20) { […]

Distinct, join and outer Join with linq to sql

While stored procedures will beat linq most of the time if you want top notch performance, there is a distinct advantage to LINQ when it comes to readability of your code, and keeping things a little more central within the application. These samples make use of the NorthWind database, and a dbml file called NorthWindContext.dbml which has been created with the sqlmetal utility that comes with Visual Studio. in these samples they are loaded with […]

Importing data from an Excel sheet

C# Example using System; using System.Collections.Generic; using System.Data; using System.Data.OleDb; using System.Linq; using System.Text; namespace Excel2Table { public class ExcelReader { /// <summary> /// Reads an excel file with OLEDB, and returns a datatable object from the content. The sheet to be imported /// needs to be named “Import” in the excel file. /// </summary> /// <param name=”sFilehandle”>Complete path to the excel file, including the filename and extension</param> /// <returns>A datatable containing the imported file</returns> […]

Threads and the Threadpool

This small demonstration code snippet will show you how to: Create threads to run code in the background of your application Demonstrate how to pass one or more parameters to a background thread Demonstrate how to wait for all background activity to finish before exiting the main program. using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; namespace ConsoleApplicationThreadTest { /// <summary> /// This is a simple demonstration of starting background tasks for processing and passing […]

Using LINQ with MySQL

This code demonstrates access to OSCOMMERCE database objects which are hosted on a mysql server, through the enitity framework and LINQ. Additionaly, the connectionstring is overriden at runtime to seperate developent and production environments. Install the MySql connector for visual studio Add a ADO.NET Entity data object to the solution Use the Wizard to create the object context (adding the tables product and products_descriptions for this example If you use VS2012: Remove the .tt files […]

Resursively listing a directory tree using a stack object

The class offered below shows an implementation of an efficient way to scan large filesystem trees and returning the content. By not using recursion but controlling the worklist with a stack element a large part of the load on the GC is reduced. Using recursion the collection of created objects by the recusrive calls can’t be unwind until the entire process is completed, while by using a stack this control is left with the programmer. […]

Getting and setting the clipboard content

These samples show handling the clipboard in the most basic way. First a test is done to see if there currently is text on the clipboard. If a text is found, the programm proceeds to read and display the text, and then replace it with a new text. By modifieing the parameters you can use the same technique to handle other formats such as images or audio and video fragments. VB.NET Implementation Imports System.Windows.forms Module […]

Making a form transparant

This code sample displays a method to seemingly draw anywhere freely on the screen. The key to this technique is the TransparanceKey offered by the .net framework, which makes this a fairly straightforward task. To make the background of a form transparant, the *only* thing we need to do is this; oDrawform.AllowTransparency = True oDrawform.TransparencyKey = oDrawform.BackColor If we add code to that to hide the control boxes, title bar and maximize the form we […]

Next Page »