Posts Tagged 'C#'

Sharepoint Error: Save Conflict Your changes conflict with those made concurrently by another user

If you develop Sharepoint components that modify list items, you might know this error:

Save Conflict

Your changes conflict with those made concurrently by another user. If you want your changes to be applied, click Back in your Web browser, refresh the page, and resubmit your changes.

This happens whenever you try to save a SPListItem which has been modified since you opened it. Review your code to ensure that you don’t open the same list item multiple times, work with a single SPListItem object instead.

Sometimes however it is necessary to open the same list item in parallel, as example if you want to perform an operation with elevated privileges on the item. If you take this approach you need to ensure that if you save one of the SPListItem objects, you need to reload all other SPListItems that point to the same actual item. Otherwise you’ll get the error above whenever you perform a change on the outdated items.

You can use the following utility method to ease the reloading of items:

        /// <summary>
        /// Reload a list item from scratch
        /// </summary>
        /// <param name="item">The list item</param>
        /// <remarks>
        /// Useful to avoid save conflicts
        /// </remarks>
        /// <returns></returns>
        public static SPListItem ReloadListItem(SPListItem item)
            if (item == null)
                return null;

            return item.ParentList.GetItemByUniqueId(item.UniqueId);

Dealing with DBNull in DataReaders

When dealing with IDataReaders in .NET, one faces the problem of catching DBNull values and properly converting them to ‘native null’:

int? myNullableField = (int?) reader["myField"]; // will FAIL if DBNull is being returned..

This extension method eases this pain and allows to retrieve the values in a null-safe way:

    public static class IDataReaderExtensions
        public static Nullable<T> GetNullableValue<T> (this IDataReader reader, string fieldName) where T : struct
            object value = reader[fieldName];
            if (value == DBNull.Value)
                return null;

            return (T) value;

Using the above extension, you can now simply use the reader this way:

int? myNullableField = reader.GetNullableValue<int>("myField");

The Nullable generic type does not work with certain types such as string, therefore the constraint T : struct is really essential to get it working. I borrowed this constraint trick from

Happy coding..

Create .lib file from .dll

When working with 3rd party win dll’s you somtimes miss the according .lib file required to compile against it. There is a MS KB article showing how to generate a .lib file from a .dll, however the required steps are not described detailed enough I think. So here is my quick guide:

Open the Visual Studio Command Prompt, you find its shortcut in Start->Programs->Microsoft Visual Studio->Tools. Now run the dumpbin command to get a list of all exported functions of your dll:

dumpbin /exports C:\yourpath\yourlib.dll

This will print quite a bit of text to the console. However we are only interested in the functions:

ordinal hint RVA      name

1    0 00017770 jcopy_block_row
2    1 00017710 jcopy_sample_rows
3    2 000176C0 jdiv_round_up
4    3 000156D0 jinit_1pass_quantizer
5    4 00016D90 jinit_2pass_quantizer
6    5 00005750 jinit_c_coef_controller

Now copy all those function names (only the names!) and paste them into a new textfile. Name the nextfile yourlib.def and put the line “EXPORTS” at its top. My yourlib.def file looks like this:


Now from that definition file, we can finally create the .lib file. We use the “lib” tool for this, so run this command in your Visual Studio Command Prompt:

lib /def:C:\mypath\mylib.def /OUT:C:\mypath\mylib.lib

That’s it, happy coding 🙂

Colored ASP.NET GridView Cells

In Excel, there is a useful feature that lets you change the color of a cell depending on its value. As example, you could make a “total money” cell show up with a red background if its value is below zero. How can we do this with ASP.NET gridviews? There is no built-in functionality to make this work, you’ll need to write a few lines of code to get this done. Here is how I would solve this issue: Subscribe to the RowDataBound Event of your gridview, it is fired when the current row has been bound to data (filled with values):

GridView gv = new GridView();

gv.RowDataBound += new GridViewRowEventHandler(gv_RowDataBound);

In the according event handler, iterate through all cells and update their background-color based on their value:

// color last cell

void gv_RowDataBound(object sender, GridViewRowEventArgs e)


    if (e.Row.RowType == DataControlRowType.DataRow)


        if (e.Row.Cells.Count > 0)


            TableCell c = e.Row.Cells[e.Row.Cells.Count – 1];

            if (!string.IsNullOrEmpty(c.Text))




                    int value = int.Parse(c.Text);

                    c.BackColor =

                        value >= 100 ? Color.LimeGreen :

                        value >= 85 ? Color.Gold :


                    c.Text = string.Format(“{0}%”, value); // add percentage sign




                    c.Text = null;