Help with WCF and Entity framework

Today I have implement an interesting situation … I have a WCF service that uses Entity Framework and executes a stored procedure that returns a Product object.
When I call the service from a client application it returns the Product, but I change the Product’s description manually in database and call the service again.
Unfortunately the returned Product is not the one with the new Description. I do not know how but the service returns the old product, not the one in the database with the changes…interesting … maybe the IIS 6.0 uses a cached instance of my DataAccess.dll or don’t know….please if you have some clues, maybe you should help me?

Hi,

why i can’t get databinding work with a ObjectResult<T> list which is bound to a TextBox control … so the situation is the following:

- I have a Customer and CustomerLicences entity object types

- I get the information from the database – I created a windows form with a BindingSource property (of type BindingSource). I set myForm.BindingSource.DataSource = ObjectResult<T>; where the list is the list with all my entity objects.

- I have 3 TextBox controls in my form and i call:

    textBoxName.DataBindings.Add( “Text”, this.BindingSource, “Name” );

    … and so on for all fields of my Customer entity object.

- I call:

using ( MyForm myForm = new MyForm() )

{

    myForm.BindingSource.DataSource = customers;

   // customers is from type  ObjectResult<TEntity>

 

    if ( myForm.ShowDialog() == DialogResult.OK )

    {

        myForm. BindingSource.EndEdit();

        myDataContext.SaveChanges();

    }

    else

    {

        myForm. BindingSource.CancelEdit();

    }

}

… with this call i edit the current Customer within the collection, but interesting only 1 property of my Customer’s object is changed and saved, nevertheless that i change all properties in my dialog’s bound textbox controls. 

So wtf is happened … why only the first property is saved??? Is this related with the 2-way binding or sort of?

DataGridView Extension 1.1 is released

A new version of the DataGridView extension is available … there is a major difference between the first version of it … Now it is almost just drag-n-drop of the extension in your application with DataGridView controls and you can start printing and exporting the data to Excel, Pdf and Html, searching text, making themes and many more …

You can view the DataGridView Extension home page here.

Also there is a Video for the Developers on how to integrate the Extension in existing applications.

Great component for the DataGridView control

Today I will write a post for a great component that extends the DataGridView control. It is called DataGridView Extension and it is free. The installation installs it in %PROGRAMFILES%/CompletIT/Bin/V1.0 folder three dlls – “DataGridViewExtension.dll”, “DGVEExcelExporting.dll” and “DGVEHtmlExporting.dll”. You add a reference to the dlls and in the Toolbox of the Visual Studio select choose items, select the “DataGridViewExtension.dll” in the bin/1.o folder, drag the “DataGridViewExtensionComponent” into your form or control that contains DataGridView controls. After that on each DataGridView control there will be additional property – “IsManagedByExtension” – set this property to true. Run your application. You will see that all additional functionalities like theme managment, export data to excel and html from the DataGridView control, searching for text in the DataGridView control, columns managment and so on will be automaticaly integrated and added.

 The homepage for the DataGridView Extension is http://www.completit.com/Products/DGVE/Overview.aspx

If you have any problems in integrating it, please feel free to contact me.

How to ensure that a cell is visible in a DataGridView control

To ensure that a cell is visible just simply set the CurrentCell property of the DataGridView control. The DataGridView control automatic scroll if it is not visible.

How to set ColumnWidth of a range in pixels in Excel’s worksheet with C#…

Opa … today I have searched for a function that converts my pixel’s width of a column in a DataGridView control to a ColumnWidth of an excel’s range object …. but unfortunately I have not found such and I have to write myself such. Here it is the code:

  // Get the column width
  private float GetColumnWidth(Worksheet ws, int colIndex)
  {
      return float.Parse(((Range)ws.Cells[1, colIndex]).EntireColumn.ColumnWidth.ToString());
  }
 

  // Get the column width in pixels
  private float GetColumnWidthInPixels(Worksheet ws, int colIndex)
  {
      return float.Parse(((Range)ws.Cells[1, colIndex]).EntireColumn.Width.ToString());
  }
  

  // Calculate the column width characters to point ratio
  private float GetColumnWidthCharactersForPointRatio(Worksheet ws)
  {
      float colWidth = GetColumnWidth(ws, 1);
      float widthInPixels = GetColumnWidthInPixels(ws, 1);
     

      return widthInPixels / colWidth;
  }

The last function is the key for all excel column width specific operations – it gives the ratio between the normal ColumnWidth (in characters) and the pixel’s width. From now on you just have to set your own width in the following way:
((Range)ws.Cells[1, colIndex]).EntireColumn.ColumnWidth = YOUR_OWN_CUSTOM_WIDTH_IN_PIXELS / RATIO;

If you have any suggestions for easier way go on and post it :)

DefaultCellStyle.BackColor = Color.Empty problem in the DataGridView control

Today I had a very strange problem in my DataGridView control. I have set for DefaultCellStyle.BackColor property of the DataGridView control a value Color.Empty , after that I have tried to set different colors, but no one of them is applied …. Thanks to “justbrowsing” comment we have found that this Microsoft ‘behavior’ is valid for Windows XP Proffesional, not for Server 2003 St. Ed.

Using the keyword ‘yield’ in C#

Hi folks, today I’ve discovered that the basic C# language specification is escaping me. I keep discovering new features of the language itself. I have found the keyword ‘yield’ in C#. It takes one of the forms: ‘yield return expression‘ or ‘yield break’. Now lets explain the use of the keyword in a brief example:

public class DaysOfTheWeek : IEnumerable
{
        string[] m_Days = { "Sun", "Mon", "Tue", "Wed", "Thr", "Fri", "Sat" };

        public IEnumerator GetEnumerator()
        {
                yield return m_Days[0];
                yield return m_Days[2];

                yield return m_Days[4];
                yield break;

        }
}

class TestDaysOfTheWeek
{
        static void Main()

        {
              // Create an instance of the collection class
                DaysOfTheWeek week = new DaysOfTheWeek();

                // Iterate with foreach
                foreach (string day in week)

                {
                        System.
Console.Write(day + " ");
                }
                // Iterate with the IEnumerator
                IEnumeratormyEnumerator = week.GetEnumerator();
                while(myEnumerator.MoveNext() == true)
                {
                        System.
Console.Write(myEnumerator.Current + " ");
                }
        }

}
Now … when we have a class that has a collection of items and if we want to provide an access to that collection. There are many ways providing that but the simplest is to make an iterator.  An iterator is a method, get accessor or an operator that enables us to support foreach statements in an enumeration or a class. Let’s explain what is happened behind the scene. First the compiler ‘detect’ that our class DaysOfWeek implements the interface IEnumerable so it has a function called GetEnumerator() which returns an object of type implementing the interface IEnumerator or it is an iterator. If in the function there is a ‘yield’ keyword the compiler decides that this function is an iterator and generates automatically a class that implements the IEnumerable interface and generates its Current, MoveNext and Dispose methods.  Now on each successive iteration of the foreach loop (or the direct call to myEnumerator.MoveNext), the next iterator code body resumes after the previous yield statement and continues to the next until the end of the iterator body is reached, or a yield break statement is encountered. So the picture is something like that:

1. IEnumerator myEnumerator = week.GetEnumerator(); – gets an instance of the class generated by the compiler witch provides iteration.

2. myEnumerator.MoveNext() – Finds the first ‘yield return’ statement. If the compiler finds ‘yield break’ or gets out of scope MoveNext() returns false and the while breaks

3. myEnumerator.Current – gets the expression returned by the ‘yield return’ statement.

4. myEnumerator.MoveNext() – Finds the next ‘yield return’ statement bellow the first one. If the compiler finds ‘yield break’ or gets out of scope MoveNext() returns false and the while breaks

…..
The result of the execution of the program is the output: “Sun Tue Thr”.  This is how you can do a specific ways to iterate through your composed data structure. You could provide one iterator which returns elements in ascending order, and one which returns elements in descending order or even in reversed order. An iterator that takes the elements in reversed order is just like this:

public IEnumerator GetEnumerator()
{
    for (int i = m_Days.Length - 1; i >= 0; i---)

    {
        yield return m_Days[i];
    }
}

So it is quite pretty to implement an iterator and to use it, don’t you think? :)

Posted in Basics. 7 Comments »

Memory managment or just another advertising trick :)

Hi folks,

today is a very bad day, cause it is Monday :) But for Micro$oft this is a day of CLR bugs (CLR = Common Language Runtime). Now let’s illustrate a sample program and the problem it causes. I have wrote a program to see what is the Memory Management in .NET and how does it handles with Out Of Memory problems … and guess what – it does not handles so well :) Here is the code:

private void button1_Click(object sender, EventArgs e)
{
   List<byte[]> bytes = new List<byte[]>();
   while (true)
   {
      byte[] barray = new byte[10000];

      bytes.Add(barray);
   }
}

It is very obvious that I wanted to see when the OutOfMemoryException is thrown, but unfortunately I get this:

FatalExecutionEngineError was detectedMessage: The runtime has encountered a fatal error. The address of the error was at 0x79fce5c2, on thread 0×928. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack. 

Can anyone answer what the f.ck is this? Maybe this is really a bug in their good generic List class? What do you think ;) ?

Styles inheritance in DataGridView control

The new DataGridView control for tabular data manipulation provides a large capabilities for its appearance management. The basic class for all visual styles in the DataGridView is the DataGridViewCellStyle. This class implements some properties for changing the font, the background color of the cell, the color of the font, alignments, padding, wrapping modes and so on. Now lets explain the priorities of all cell styles and the styles itself:

1. DataGridView.DefaultCellStyle – This is the default cell style of all cells in the DataGridView control (including row and column header cells). If no other style is defined this style is applied to all cells.

On the second level the DataGridView control is divided on three logical parts. Column Header Cells, Row Header Cells and the sub-table excluding this cells. Lets see the styles for each logical part:

1.1 DataGridView.ColumnHeadersDefaultCellStyle – This is the default cell style for all column header cells including the top left header cells (the top left corner cell of the table). If this style is not defined a DataGridView.DefaultCellStyle is applied for all column header cells.

1.2 DataGridView.RowHeadersDefaultCellStyle – This is the default cell style for all row header cells . If this style is not defined a DataGridView.DefaultCellStyle is applied for all row header cells.

For Column Header and Row Header Cells if the enable header visual styles property of the DataGridView control is set the DataGridView.ColumnHeadersDefaultCellStyle and the DataGridView.RowHeadersDefaultCellStyle are overridden by the current theme.

1.3 DataGridViewColumn.DefaultCellStyle – This is the style used by all cells in the sub-table’s column. For every column in the Columns collection of the DataGridView such style can be defined. If it is not defined a DataGridView.DefaultCellStyle is applied for all cells of the sub-table’s column.

1.3.1 DataGridView.RowsDefaultCellStyle – This is the style used by all cells in the sub-table. If this style is not defined for every column of the sub-table is used its DataGridViewColumn.DefaultCellStyle.

1.3.1.1 DataGridView.AlternatingRowsDefaultCellStyle – This style is used by all cells in the alternating rows in the sub-table, creating a ledger like effect. If this style is not defined a DataGridView.RowsDefaultCellStyle is applied for all alternating row’s cells.

1.3.1.1.1 DataGridViewRow.DefaultCellStyle – This style is used by all cells in a single row of the subtable. Now one row in the subtable can be alternating or regular. If this property is not defined for the single row there are two cases:

- A DataGridView.AlternatingRowsDefaultCellStyle is applied if the row is alternating;

- A DataGridView.RowsDefaultCellStyle is applied if the row is regular;

Every cell of the DataGridView control has its own DataGridViewCell.Style property. If it is set it override every other style on the higher level mentioned above. If it is not set the exact style is DataGridViewCell.InheritedStyle which is examined from all higher level styles.

So if you want to change the style of some cells in the DataGridView control it is very important to understand the styles inheritance and see that you can manage styles on a single cell level or higher (global) level.

Follow

Get every new post delivered to your Inbox.