iAlpine.net
A journey of exploration and discovery

Architecting Applications the Microsoft Way

October 17, 2010 12:58 by mihko

 


C# Import/Export data from files using FileHelpers Library

September 27, 2010 19:08 by mihko

Many times in my application projects, I have the need to import/export data from CVS files.

Before finding out about FileHelpers library, I was always doing import/export either through SSIS or by parsing data in C#.

FileHelpers library can completely solve this problem, no need to parse data anymore. Any kind of import and export you need to do in your application, using fixed length or delimiter records in files, strings or streams.

With the library you can easily strong type your flat file, mapping it to each record and then read or write your file as a strong typed .NET array. The library also includes import and export from different storages such as Excel, Access, SqlServer, etc.

The procedure of using import/export is straight forward:

1. Define the class
2. Add a reference
3. Read from the file and cast it to an array
4. Perform any actions you wish on the array


You can dowload the library and get more details about it at sourceforge.net

 


C# - Unable to read the stream from files using Resources in Windows Forms project?

October 16, 2009 15:49 by mihko


A few days ago I needed to access and read the stream from files that I included in my Resources of Windows Forms project. On Microsoft support website http://support.microsoft.com/kb/319292 is a nice article about "How to embed and access resources by using Visual C#". Make sure you read all of it.

A process is straight forward using Assembly namespace, the trick that I encountered in my project was that in my debugger I kept on receiving a null value when reading any type of files to stream using:

// Reading stream from Aero Silver-Busy.ani Cursor
Stream my = this.GetType().Assembly.GetManifestResourceStream("demoProject.Resources.Aero Silver-Busy.ani");

Later after rereading the article I discovered that in Resources you need to click on a file and in Properties of a file set Build Action property to Embedded Resource. That is it and make sure you are refering to the right namespace and filename!


Windows Forms Microsoft ToolTip Binding to SQL to show data & memory performance

April 25, 2008 12:27 by mihko


I found out on my laptop where I have installed locally SQL 2005 and VS 2008 that when implementing ToolTip in Windows Forms Application that the ToolTip component can be quite memory demanding. I have encountered this problem in an application where I have a datagridview bound to data in SQL. When adding a ToolTip feature and databound some information from SQL to ToolTip component the application started consuming a lot more CPU power and it was performing visibly slower.

The only solution I know until know is to try to avoid information bound to ToolTip. In my application I displayed the same information on click event in MessageBox.  


Saving Null Values to DateTime field in SQL with Windows Forms TextBox Field

October 3, 2007 17:19 by mihko

Saving Null values into DateTime Fileds in SQL Database is not as straight forward as it seems in C# and Windows Forms.  If you want to save a Null value into Database you have to write a custom code otherwise you encounter various interesting errors.

To solve a problem in one of my projects I used the following code:

Step 1:
Because I wanted that Data is saved when DataGridView Row Validating Event fires I wrote the folowing code in DataGridView_RowValidating event.


// Save Data to Tables on RowChange
private void vozilaDataGridView_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
{

// If field TxtDatumNakupa is blank
if (TxtDatumNakupa.Text == "")
{
try
{
// Define Nullable DateTime myDate
DateTime? myDate = new DateTime();

// Set it to Null
myDate = null;

// Update the filed in DataSet with null value and toolStripStatusLblVoziloID.Text is the ID where Update occurs in DB
vrednost_VozilaTableAdapter.UpdateQueryDatumNakupaUpdate(null, Convert.ToInt32(toolStripStatusLblVoziloID.Text));

}

catch (FormatException)
{
MessageBox.Show("Error writing to DB!");
}

}
}

Step 2:
Since the TextBox that is bound to DateTime also needs to be validated for correct input I used a TextBox Validating Event to check for correct DateTime format

private void TxtDatumNakupa_Validating(object sender, CancelEventArgs e)
{


// Define date
DateTime date;

// If its not same format as date and txtDatumNakupa.text is also not empty
if (!DateTime.TryParse(TxtDatumNakupa.Text, out date) && TxtDatumNakupa.Text != "")
{
MessageBox.Show("Date in such format does not exist!", "Row Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
e.Cancel =
true;
}
 
}

Step 3:
Because TextBox is DataBound and it does not let click out of a TextBox if it is empty, I found that in Design View where DataBinding is done to TextBox you have to add DataSourceUpdateMode.OnPropertyChanged, "" so it lets you pass by with an empty field.

this.TxtDatumPogodbe.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.vrednost_VozilaBindingSource, "Datum_Pogodbe", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged, ""));

That is it! When I tested the example I was able to normaly save Null value into SQL.
* Note that Nullable DateTime myDate does not need to be declared here I left it inside in case you want to save also a DateTime below and modify example a bit.






  

Sponsors


www.knetit.net

Kiva - loans that change lives


  

Job Board for Web Designers and Web Developers

The On Demand Global Workforce - oDesk

affiliate_link