Click here to Skip to main content
15,896,201 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
try
           {
               SyS_comboBox1.DataSource = ABLL.BindAC_Name();
               SyS_comboBox1.DisplayMember = "AC_NAME";
               SyS_comboBox1.ValueMember = "AC_NAME";
           }
           catch (Exception ee) { }

after binding i have to save the value of combobox so that the value shoul remain even after the app was re opened
Posted
Updated 16-Sep-14 21:07pm
v2
Comments
Sinisa Hajnal 17-Sep-14 2:22am    
Wherefrom do you read BindAC_Name()? Use that repository to save the value if you know the user or have some other way of identifying incoming connections. Otherwise, use cookies.
MadhuRavuri 17-Sep-14 3:03am    
BindAC_Name :it was a stored procedure containing the AC_Name and AC_NO form AH table and after binding AC_Name the selected value should remain in the combobox even after application is agin debugged
Sinisa Hajnal 17-Sep-14 3:18am    
OK, ignore the comment above, I thought you had to preserve it in web solution. My bad.
MadhuRavuri 17-Sep-14 3:09am    
iam using this in windows forms
BillWoodruff 17-Sep-14 2:49am    
And this is Windows Forms ? Web ? WPF ? What have you tried so far ? Show us some code.

use sql server or xml to save and load it on the time of loading
 
Share this answer
 
There are a variety of techniques that can be used to persist Application/Form settings/data across "sessions" (opening-closing-opening the Application) from "light-weight" to "heavy-weight:" I suggest you study this thread: [^] to get general ideas of the techniques.

Note, in case you were wondering: WinForm native Controls, like ComboBox, are not directly serializable.

Then, decide which technique is best for you to use: Application Settings (user only ? application wide ?) ? Serialize a Custom Class in Binary, XML, or JSON format ?, etc.

Whichever strategy you use, in your case you are going to need to check in your code for the existence of persisted settings, and also decide when you need to refresh the current settings ... and the ComboBox content ... by reading from the Database again.

If I only needed to persist one set of values related to some specific Control (like a ComboBox's Items Collection), and I believed those settings would not need to be refreshed form the Database often, I might use Application Settings [^].

Note that Application.Settings allows only one Type of object that will hold a Collection: the ArrayList. To use an ArrayList as an Application Setting,

Note that in the following example I've made the assumption that the ComboBoxItems Collection may be modified at run-time; so, in this case, you are not binding to the Database.

1. in the C# project title node in the Solution Explorer Window: double-click on Settings.settings: that opens the Settings Designer.

2. click on the dropdown of the Type field of a "blank" setting and select "Browse:" in the treeview of libraries select 'mscorlib: select System.Collection: select 'ArrayList

3. give your Property a Name in the 'Name field.

4. define whether your Property is user-defined or application-wide. Note that if you make it application-wide then you cannot assign to the Property directly with = ! It will have no 'setter.

Here's an example of how an ArrayList Applicaton Setting named 'ComboBoxXItems defined as 'user in scope might be used:
C#
private void Form1_Load(object sender, System.EventArgs e)
{
    if (Settings.Default.ComboBoxXItems == null)
    {
        // fill the ComboBox from the Database !
    }
    else
    {
        // clear theComboBox and refill it from the saved Setting
        comboBox1.Items.Clear();
        comboBox1.Items.AddRange(Settings.Default.ComboBoxXItems.ToArray());
    }
}

// for testing only: code to simulate adding a ComboBoxItem at run-time
// to make sure that the run-time added Items are saved in the Setting
private int count = 1;

private void button1_Click(object sender, System.EventArgs e)
{
    comboBox1.Items.Add("new item " + count.ToString());
    count++;
}

// the Form Closing Event is where the current ComboBox Items
// are saved to the Setting
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    // capture the current ComboBox Items
    ArrayList CBxItemsToSave = new ArrayList(this.comboBox1.Items);
      
    if (Settings.Default.ComboBoxXItems == null)
    {
        // if we initialized the ComboBox with Items properlyt
        // then there is an error here

        // for testing only
        Settings.Default.ComboBoxXItems = new ArrayList();
    }
    else
    {
        // clear the setting only if it is non-null !
        Settings.Default.ComboBoxXItems.Clear();
    }

    // refresh the Setting
    Settings.Default.ComboBoxXItems.AddRange(CBxItemsToSave);

    // save the Setting
    Settings.Default.Save();
}
Get started using one technique, or explore them all (best), and come back with specific questions, and code examples.
 
Share this answer
 
v2
Comments
MadhuRavuri 17-Sep-14 6:03am    
iam using this combobox in user control and how should i give it in form_closing method
MadhuRavuri 17-Sep-14 6:38am    
Items collection cannot be modified when the DataSource property is set is showing in button click

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900