Registry Errors
-
i am trying to write dg tablestyle grid column widths to the registry: opening and creating keys is just fine, i can open regedit and see the keys. it is when i try to assign a key value to the width of a column that i get the errors. here is how i save the key:
Param.SetValue("IHDetailIDWidth",this.dgIssueHistory.TableStyles["IssueHistory"].GridColumnStyles["DetailID"].Width);
and here is how i am trying to retrieve the key:int ID = Param.GetValue("IHDetailIDWidth");
when i don't run the second line of code, i get no errors, and it is writting to the registry just fine. when i try to run the code to retrieve the values is when i get this error: Object reference not set to an instance of an object. it highlights the section of code where i set the value though. any ideas on what i'm doing wrong? -
i am trying to write dg tablestyle grid column widths to the registry: opening and creating keys is just fine, i can open regedit and see the keys. it is when i try to assign a key value to the width of a column that i get the errors. here is how i save the key:
Param.SetValue("IHDetailIDWidth",this.dgIssueHistory.TableStyles["IssueHistory"].GridColumnStyles["DetailID"].Width);
and here is how i am trying to retrieve the key:int ID = Param.GetValue("IHDetailIDWidth");
when i don't run the second line of code, i get no errors, and it is writting to the registry just fine. when i try to run the code to retrieve the values is when i get this error: Object reference not set to an instance of an object. it highlights the section of code where i set the value though. any ideas on what i'm doing wrong?mikemilano wrote: when i try to run the code to retrieve the values is when i get this error: Object reference not set to an instance of an object. Are you creating your
Param
object before you call theGetValues()
method? For example:MyAppSettings Param = new MyAppSettings(); ... Param.GetValue("IHDetailIDWidth");
Also, check in your
GetValue
methods code to make sure all objects referenced have been created. Roger Stewart "I Owe, I Owe, it's off to work I go..." -
mikemilano wrote: when i try to run the code to retrieve the values is when i get this error: Object reference not set to an instance of an object. Are you creating your
Param
object before you call theGetValues()
method? For example:MyAppSettings Param = new MyAppSettings(); ... Param.GetValue("IHDetailIDWidth");
Also, check in your
GetValue
methods code to make sure all objects referenced have been created. Roger Stewart "I Owe, I Owe, it's off to work I go..."i am creating a Param object before i call GetValues() here's a little more of the code with the 2 methods i'm using. I can comment out either one of the 'problem' lines, and the methods work fine, but it is when both are not commented that i get the object error.
private void CreateStyles(DataGrid dg) { DataGridTableStyle style = new DataGridTableStyle(); style.MappingName = "IssueHistory"; DataGridTextBoxColumn DetailID = new DataGridTextBoxColumn(); DetailID.MappingName = "DetailID"; DetailID.WidthChanged += new EventHandler(this.SaveIssueGrid); RegistryKey softwareKey = Registry.LocalMachine.OpenSubKey("Software"); RegistryKey SC = softwareKey.OpenSubKey("SC"); if( SC != null) { RegistryKey Param = SC.OpenSubKey("Param"); if(Param != null) { DetailID.Width = (int)Param.GetValue("IHDetailIDWidth",50); // <-- problem line } } // code here to add style to dg } void SaveIssueGrid(object sender, EventArgs e) { RegistryKey softwareKey = Registry.LocalMachine.OpenSubKey("Software",true); RegistryKey SC = softwareKey.CreateSubKey("SC"); RegistryKey Param = SC.CreateSubKey("Param"); Param.SetValue("IHDetailIDWidth",this.dgIssueHistory.TableStyles["IssueHistory"].GridColumnStyles["DetailID"].Width); // <- problem line }
sorry for the big code post, but i've been working on this for hours with the same results. -
i am creating a Param object before i call GetValues() here's a little more of the code with the 2 methods i'm using. I can comment out either one of the 'problem' lines, and the methods work fine, but it is when both are not commented that i get the object error.
private void CreateStyles(DataGrid dg) { DataGridTableStyle style = new DataGridTableStyle(); style.MappingName = "IssueHistory"; DataGridTextBoxColumn DetailID = new DataGridTextBoxColumn(); DetailID.MappingName = "DetailID"; DetailID.WidthChanged += new EventHandler(this.SaveIssueGrid); RegistryKey softwareKey = Registry.LocalMachine.OpenSubKey("Software"); RegistryKey SC = softwareKey.OpenSubKey("SC"); if( SC != null) { RegistryKey Param = SC.OpenSubKey("Param"); if(Param != null) { DetailID.Width = (int)Param.GetValue("IHDetailIDWidth",50); // <-- problem line } } // code here to add style to dg } void SaveIssueGrid(object sender, EventArgs e) { RegistryKey softwareKey = Registry.LocalMachine.OpenSubKey("Software",true); RegistryKey SC = softwareKey.CreateSubKey("SC"); RegistryKey Param = SC.CreateSubKey("Param"); Param.SetValue("IHDetailIDWidth",this.dgIssueHistory.TableStyles["IssueHistory"].GridColumnStyles["DetailID"].Width); // <- problem line }
sorry for the big code post, but i've been working on this for hours with the same results.Sorry for the late reply. Since you know the keys/subkeys that you want to open, try and open/create them all at once. This gets rid of the multiple CreateSubKey/OpenSubKey statements which may be part of the problem. I was able to get a small example working, that I hope will help you. Create a new C# Windows Application and place 2 buttons (btnRead and btnWrite), one label (label1) on the form. Add the Click events for the two buttons using the Designer Property Grid. Replace the generated Click events with the following code:
private void btnRead_Click(object sender, System.EventArgs e) { RegistryKey softwareKey = Registry.LocalMachine.OpenSubKey(@"Software\SC\Param"); if ( softwareKey != null ) { int x = (int)softwareKey.GetValue("IHDetailIDWidth", 50); // <-- problem line label1.Text = x.ToString(); } } private void btnWrite_Click(object sender, System.EventArgs e) { RegistryKey softwareKey = Registry.LocalMachine.OpenSubKey(@"Software\SC\Param", true); softwareKey.SetValue("IHDetailIDWidth",this.Width); // <- problem line }
Roger Stewart "I Owe, I Owe, it's off to work I go..."
-
Sorry for the late reply. Since you know the keys/subkeys that you want to open, try and open/create them all at once. This gets rid of the multiple CreateSubKey/OpenSubKey statements which may be part of the problem. I was able to get a small example working, that I hope will help you. Create a new C# Windows Application and place 2 buttons (btnRead and btnWrite), one label (label1) on the form. Add the Click events for the two buttons using the Designer Property Grid. Replace the generated Click events with the following code:
private void btnRead_Click(object sender, System.EventArgs e) { RegistryKey softwareKey = Registry.LocalMachine.OpenSubKey(@"Software\SC\Param"); if ( softwareKey != null ) { int x = (int)softwareKey.GetValue("IHDetailIDWidth", 50); // <-- problem line label1.Text = x.ToString(); } } private void btnWrite_Click(object sender, System.EventArgs e) { RegistryKey softwareKey = Registry.LocalMachine.OpenSubKey(@"Software\SC\Param", true); softwareKey.SetValue("IHDetailIDWidth",this.Width); // <- problem line }
Roger Stewart "I Owe, I Owe, it's off to work I go..."
thanks for the help .. this has made me realize my problem is in the event handler and not specific to the registry. that is a great shortcut for just opening one key. i wasn't aware you could do it that way. i do appreciate the help, .. i'll start a new thread on the new subject.