Click here to Skip to main content
11,636,300 members (78,220 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: .NET3.5 C#
In my application i have 3 forms . In each form i have two button Next and Back button. The Next button i used to go to next form and back button is used to go to previous form. In next button click event i store data of control in memory varibale and also store data in sql and after that it goes to next form. when i goes to second form and after that i clicked on back button of second form it goes to previous form but data is not display in combobox which i selcted which is come from database.but rest of the control display data. Is there any way to store combox selected data which is come from database in memory varibale. below i mention my code. In combobox i retrive and dsiplay data from database using DisplayMember property of combobox .

 private void btnnext_Click(object sender, EventArgs e)
                using (SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]))
                    using (SqlCommand cmd = new SqlCommand())
                        cmd.Connection = conn;
                        cmd.CommandType = System.Data.CommandType.StoredProcedure;
                        cmd.Parameters.AddWithValue("@Transtype", "SaveEmployeeSelfDetail");
                        cmd.Parameters.AddWithValue("@Employee_Code", employeecode);
                        cmd.Parameters.AddWithValue("@Salutation", salutaion);
                        cmd.Parameters.AddWithValue("@FirstName", firstname);
                        cmd.Parameters.AddWithValue("@MiddleName", middlename);
                        cmd.Parameters.AddWithValue("@LastName", lastname);
                        cmd.Parameters.AddWithValue("@Gender", gender);
                        cmd.Parameters.AddWithValue("@DateOfBirth", dateofbirth);
                        cmd.Parameters.AddWithValue("@MaritalStatus", martialstatus);
                        cmd.Parameters.AddWithValue("@FatherName", fathername);
                        cmd.Parameters.AddWithValue("@DateOfJoin", dateofjoin);
                        cmd.Parameters.AddWithValue("@EmpOrg1", firstorglevel);
                        cmd.Parameters.AddWithValue("@EmpOrg2", secondorglevel);
                        cmd.Parameters.AddWithValue("@EmpOrg3", thiredorglevel);
                        cmd.Parameters.AddWithValue("@EmpOrg4", fourthorglevel);
                        cmd.Parameters.AddWithValue("@EmpOrg5", fifthorglevel);
                        cmd.Parameters.AddWithValue("@EmpOrg6", sixorgl);
                        cmd.Parameters.AddWithValue("@EmpOrg7", sevenorglevel);
                        cmd.Parameters.AddWithValue("@EmpOrg8", eightorglevel);
                        // cmd.Parameters.AddWithValue("@ToDate", Convert.ToDateTime (dateTimePicker2.Text.Trim()));
                        cmd.CommandText = "Hiring_CreateNewEmployee";
                        SqlDataAdapter dAdapter = new SqlDataAdapter();
                        dAdapter.SelectCommand = cmd;
               // MessageBox.Show("Record Saved Successfully", "Self Details", MessageBoxButtons.OK, MessageBoxIcon.Information);


            catch (Exception ex)
                lblerror.Text = ex.Message;

// store data in memory variable
            Data.salutaion = salutaion;
            Data.fathername = txtfathername.Text;
            Data.empid = employeecode;
            Data.firstorglevel = firstorglevel;
            Data.secondorglevel = secondorglevel;
            Data.thiredorglevel = thiredorglevel;
            Data.fourthorglevel = fourthorglevel;
            Data.fifthorglevel = fifthorglevel;
            Data.sixorgl = sixorgl;
            Data.sevenorglevel = sevenorglevel;
            Data.eightorglevel = eightorglevel;
           // Data.singleemployeearea = sixthorglevel;
            //Data.singleemployeearea = comboBox1.SelectedItem.ToString();
            Data.firstname = firstname;
            Data.lastname = lastname;
            Data.middlename = middlename;
           // Data.designation = sevenorglevel;
            Data.martialstatus = martialstatus;
            Data.gender = gender;
            Data.dateofbirth = dateofbirth;
            Data.dateofjoining = dateofjoin;
            //Data.employeetype = eightorglevel;
            Data.dd = datetimejoin.Value;

// calling function on click on back button of 2nd form which display data in control
public void DisplyEmployeeSelfDetails()

               txtemployeecode.Text =      Data.empid;
               cmbFirstOrgLevel.SelectedItem  = Data.firstorglevel;
               cmbSecondOrgLevel.SelectedItem  = Data.secondorglevel;
               cmbThiredOrgLevel.SelectedItem = Data.thiredorglevel;
               cmbFourthOrgLevel.SelectedItem = Data.fourthorglevel;
               cmbFifthOrgLevel.SelectedItem = Data.fifthorglevel;
               cmbsixorglevels.SelectedItem = Data.sixorgl;
               cmbsevenorglevel.SelectedItem = Data.sevenorglevel;
               cmbeightorglevel.SelectedItem = Data.eightorglevel;
               //comboBox1.SelectedItem = Data.singleemployeearea;
               cmbsalutation.SelectedItem = Data.salutaion;
               txtfathername.Text         = Data.fathername;
               txtfirstname.Text          = Data.firstname;
               txtlastname.Text = Data.lastname;
               txtmiddlename.Text = Data.middlename;
               combomaritalstatus.SelectedItem = Data.martialstatus;
               combogender.SelectedItem = Data.gender;
               datetimejoin.Value = Convert.ToDateTime(Data.dateofjoining);
               datetimebirth.Value = Convert.ToDateTime(Data.dateofbirth);

Posted 17-Sep-12 23:20pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

how do you bind cmbFourthOrgLevel?
and if Data.fourthorglevel returns value-member then
it should be written as below,
cmbFourthOrgLevel.SelectedValue = Data.fourthorglevel;
Happy Coding!
Smile | :)
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

when Combobox getting disable it loose its selected value. i am working with asp. net so in web application also this thing happen. what we do, we store that selected value in hidden control when page gets load and at the time of display again assign
to the combobox.
So i dont know weather there any hidden control in windows application. if it is not there then take one control like textbox and try..........
I hope it will work.

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

  Print Answers RSS
0 327
1 OriginalGriff 273
2 Sergey Alexandrovich Kryukov 255
3 Suvendu Shekhar Giri 225
4 stibee 195
0 OriginalGriff 8,941
1 Sergey Alexandrovich Kryukov 8,459
2 Mika Wendelius 6,849
3 F-ES Sitecore 2,364
4 Suvendu Shekhar Giri 2,320

Advertise | Privacy | Mobile
Web02 | 2.8.150728.1 | Last Updated 18 Sep 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100