Hello everyone,
In my windows form application iam having a login form with 2 textboxes for username and password and checkbox (to keep the user logged in). When the user login for the first time if he check the checkbox then it should remains checked(iam having a option to uncheck in a menu item) even when the applcation is closed and then reopen it. How can code to remain the checkedstate of the checkbox always checked ?..
14 9390
You need to save your values someplace... ini file, text file, config file, registry...
you need to save the details for every person, then retrieve them the next time a certain person logs in..
hello,
I have tried to save the checkbox info to the config file. Iam getting a error "object reference is not set to an object". Below is my code -
private static string getConfigFilePath()
-
{
-
return Assembly.GetExecutingAssembly().Location + ".config";
-
}
-
private static XmlDocument loadConfigDocument()
-
{
-
XmlDocument docx = null;
-
try
-
{
-
docx = new XmlDocument();
-
docx.Load(getConfigFilePath());
-
return docx;
-
}
-
catch (System.IO.FileNotFoundException e)
-
{
-
throw new Exception("No configuration file found.", e);
-
}
-
}
-
private void rem_CheckedChanged(object sender, EventArgs e)
-
{
-
if (rem.Checked == true)
-
{
-
rem.CheckState = CheckState.Checked;
-
System.Xml.XmlDocument docx = new System.Xml.XmlDocument();
-
docx = loadConfigDocument();
-
System.Xml.XmlNode node;
-
node = docx.SelectSingleNode("//appsettings");
-
try
-
{
-
string key = "rem.checked";
-
string value = "true";
-
XmlElement elem = (XmlElement)node.SelectSingleNode(string.Format("//add[@key='{0}']", key));
-
if (elem != null)
-
{
-
elem.SetAttribute("value", value);
-
}
-
else
-
{
-
elem = docx.CreateElement("add");
-
elem.SetAttribute("key", key);
-
elem.SetAttribute("value", value);
-
node.AppendChild(elem);
-
}
-
docx.Save(getConfigFilePath());
-
}
-
catch (Exception e2)
-
{
-
MessageBox.Show(e2.Message);
-
}
-
any idea?..
Thank you..
Dinesh.
You could make us guess but... On what line does VS break with the null object error?
While that line is highlighted in VS, you can hover your mouse over each object/variable and the tooltip will give you its value. Which one is null?
hello tlhintoq, -
elem = (XmlElement)node.SelectSingleNode(string.Format("//add[@key='{0}']", key));
-
After this its directly goes to the catch section. i think because i will get null value in the initial state so it directly showing NullException.
@sarabonn
Your original question was how can you keep the checked state persistent between running instanances of your application.
How can code to remain the checkedstate of the checkbox always checked ?..
And two folks here told you to save the state of the checkbox when you close, and reload it when you open.
I'm going to GUESS that your 'node' object doesn't yet exist when you run this line. Put a breakpoint on this line. When your code stops here, hover over each word (XmlElement) ... node ... and so on... and I'll bet that node turns out to be null.
Either that or the return of your ".SelectSingleNode" method is null (it didn't find a match) thus you don't have an object to work with.
I have no idea what any of this
- elem = (XmlElement)node.SelectSingleNode(string.Format("//add[@key='{0}']", key));
has to do with your checkbox.checked being either true or false.
i think because i will get null value in the initial state so it directly showing NullException.
checkbox.Checked only has two possible answers: true or false. Null is not an option. It is either checked or it isn't.
If you are getting a null object it isn't the state of a checkbox. It might be the very existence of a checkbox. If your checkbox doesn't exist, then it can't have a state.
Actually i want to save the state of the checkbox. I think you mistaken that error line. That has nothing to do with checkbox. It will just check the config file whether Add key is there or not if it is there then it will add the value or else it add the key and value. In the key iam giving my "rem.checked" and in the value iam passing the value true..
I think you mistaken that error line. That has nothing to do with checkbox.
Then don't confuse the issue or those trying to help you with it by throwing in random bits and pieces that aren't related.
Actually i want to save the state of the checkbox. I think you mistaken that error line. That has nothing to do with checkbox. It will just check the config file whether Add key is there or not if it is there then it will add the value or else it add the key and value. In the key iam giving my "rem.checked" and in the value iam passing the value true..
There is no question or issue in this latest message. Are you still having an issue directly related to your original post of how do you make the checkbox state persistent?
hello tlhintoq,
What iam trying to do in my application is
1). I have a Login groupbox and checkbox "Keep me Logged in". So when the user click this frist time then the state of the checkbox should be set to true even when i close and reopen my application.
2).I have a menu when the user click then the state of the checkbox should be set to false i mean remove key also from the config file..
This is what iam trying to do..
Thank you.
I understand your desire. I understood it the first time you posted.
Two users said you need to save the boolean state in some way of your choosing. That can be a config file, XML, text file, registry setting, database record... its all up to you, your preferences and your needs.
I personally still save these things in the registry, but encrypt all the values so a user can't just copy/paste/edit the values to gain new permissions.
So let me ask you again... What problem in your code are you having with respect to setting the checkbox.checked property to either true or false?
hello tlhintoq,
I want to save the checkbox.checked to true. Iam getting the error after this following line. -
elem = (XmlElement)node.SelectSingleNode(string.Format("//add[@key='{0}']", key));
-
Iam checking whether the config file has the Add key if else iam adding the key and value to the config file. First time since the value of the elem will be null the loop moves to the catch section. This is the problem i have or can you please suggest me whether my approach is right and try someother ?..
Thank you very much.
Dinesh.
I don't know a thing about XML config files. Never used them. Someone else will have help with that.
Considering your current problem isn't about the checkbox, but rather about config files you might want to start a new thread for that.
But I can give you some advice that you may or may not be able to apply to this situation. Never assume anything. Never assume something exists, or that the user wouldn't think to enter in a different type of data, or that your network is up *now* just because it was when the application started... etc.
First time since the value of the elem will be null the loop moves to the catch section.
If I am understanding this right... Just to see if the value is null before trying to do something with it. ie: Wrap the action inside an If { } construct -
// My code may be off here since I don't do config files
-
// but you should be able to catch the logic of it and clean it up
-
if ( SelectSingleNode(string.Format("//add[@key='{0}']", key) != null)
-
{
-
}
-
else
-
{
-
}
-
Like I said, I tend to still do old-fashioned registry. - RegistryKey Regkey = "HKEY_CURRENT_USER\\Software\\MyApplication";
-
RegKey.SetValue("myCoolCheckbox", myCoolCheckbox.Checked);
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Fabri |
last post by:
How can I, on button click, to select ONLY the following 4 checkbox?
I would like to do this without a for loop through form.lenght because
this is only an example and I have to apply this script...
|
by: feanor |
last post by:
I need to select children checkboxes when selecting the parent one.
This is my function:
function SelectChildrens(checkbox_name){
form = document.forms;
Sname = checkbox_name.split("-");
for...
|
by: mike |
last post by:
Hi there:
I've read an excellent "how to"-article by Microsoft (no. 306227) - partly
cited cited at the end of this email).
I have implemented the code related to the part "How to Add a...
|
by: DotNetJunkies User |
last post by:
1.
i want to populate checkboxlist using javascript only at client side ....how can i do this..by populate word i mean that checkboxes should be checked or unchecked on some condition basis.......
|
by: KC |
last post by:
What's the difference between 'Checked' & 'CheckState' for the checkbox? Am
I wrong or do they do the same thing, which begs the question why?
---
Ken
|
by: marcmc |
last post by:
Hey All,
I want to find whether my first item within my CheckListBox is in a checked
or unchecked state. I tried the following but it did not work, Can anyone
help.
If...
|
by: rn5a |
last post by:
All the rows in a DataGrid, including the Header, are accompanied with
a CheckBox. I want that when the CheckBox in the Header is checked,
then all the CheckBoxes should automatically get checked....
|
by: cyberdawg999 |
last post by:
Greetings all in ASP land
I have overcome one obstacle that took me 2 weeks to overcome and I
did it!!!!!
I am so elated!! thank you to all who invested their time and energy towards helping me...
|
by: sarabonn |
last post by:
Hello everyone,
Iam firing a event in the chechbox_checkedchanged. It works fine
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |