JG Vimalan's Blog

It is all about coding!

Cannot bind to the new display member. Parameter name: newDisplayMember

I got this error when I was trying to bind datasource in a combobox and set values in the ‘DisplayMember’ and ‘ValueMember’.

Here is the code snippet,

public class ClinicInfo
{
internal string ClinicID { get; set; }
internal string ClinicName { get; set; }

public override string ToString()
{
return ClinicName;
}
}

//Code for binding the data into ComboBox

IEnumerable<ClinicInfo> clinics = DBGateway.GetActiveClinics();

ClinicCmbBox.DataSource = clinics;
ClinicCmbBox.ValueMember = “ClinicID”;
ClinicCmbBox.DisplayMember = “ClinicName”;

Everything was looking good except the Access Modifiers (internal) for the properties in ClinicInfo class.

When I changed that to public as shown below, everything worked fine.

public string ClinicID { get; set; }
public string ClinicName { get; set; }

So, the properties you are setting in ‘ValueMember’ and ‘DisplayMember’ should be public.

September 27, 2011 Posted by | C#.NET, Windows Forms | 2 Comments

Custom actions in .NET installer

I got a task where, I need to perform custom actions right after installing the .NET application. It can be any action, say, write to log file, open the installed application, etc., In order to achieve this, follow the steps given below,

a. In my example I am using a .NET windows forms application. Add a class in your .NET application and inherit it from the System.Configuration.Install.Installer class. The Installer class exposes several useful events. I am performing a custom action in the committed event. Please find the complete code below,



[RunInstaller(true)]
    public class CustomInstaller:System.Configuration.Install.Installer
    {
        public CustomInstaller():base()
        {
            this.Committed += new InstallEventHandler(CustomInstaller_Committed);
        }

        void CustomInstaller_Committed(object sender, InstallEventArgs e)
        {
     //put your code here      
            //this code will run after installation is committed
        }

        public override void Install(System.Collections.IDictionary stateSaver)
        {
            base.Install(stateSaver);
        }

        public override void Commit(System.Collections.IDictionary savedState)
        {
            base.Commit(savedState);
        }

        public override void Uninstall(System.Collections.IDictionary savedState)
        {
            base.Uninstall(savedState);
        }
    }

b. Also, you need to specify the ‘Primary output’ in the custom actions section of your Installer project. To do that, right click your Installer project, View -> Customer Actions. You will be able to see folders like, Install, Commit, Rollback and Uninstall. Now, right click the Commit folder, select custom actions. A dialog will appear. Now, select Application folder and select Primary output. The output will be as shown below,

So, when you install your .net application, finally, your code will run.

November 12, 2010 Posted by | C#.NET, Windows Forms | 1 Comment

Handling hotkeys in Windows Forms application

There may be situations where you want to launch your windows application using an hotkey. I assume that, you have already set hot key for your windows application. So, in the form, all you need to do is, override the WndProc method as shown below,

namespace hkWin
{
    public partial class Main : Form
    {

 protected override void WndProc(ref Message m)
        {           
            if (m.Msg == 786)
            {
                //place your code here
            }
            base.WndProc(ref m);
        }

the number 786 will confirm whether the keys hit in keyboard is the hotkey for this application. This method will fireup even if the form is not in focus/minimized.

November 10, 2010 Posted by | C#.NET, Windows Forms | Leave a comment