Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot set the SelectedValue in a ListControl with an empty ValueMember in winform using c#

Tags:

c#

winforms

I have a combobox that loads data from database but i got an error that you cannot set the selectedValue in a ListControl Although i have set the selectValue to a Primary key of a Table. But still getting error on runtime.. Here is the Code..

    private void FormAddStudent_Load(object sender, EventArgs e)
    {
        //For combobox Campuse
        cBoxCampus.DataSource = GetAllCampuses();

        cBoxCampus.DisplayMember = "campus_name";
        cBoxCampus.SelectedValue = "campus_id";
        //Foe ComboBox Department
        cBoxDepartment.DataSource = GetAllDepartment();

        cBoxDepartment.DisplayMember = "depname"; 
        cBoxDepartment.SelectedValue = "depid";
    }

and this is the code behind Insert Button

        private void btnInsert_Click(object sender, EventArgs e)
    {
        string CS = ConfigurationManager.ConnectionStrings["UMSdbConnectionString"].ConnectionString;
        using (SqlConnection con = new SqlConnection(CS))
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("SELECT ISNULL(MAX(std_id),0)+1 FROM Student", con);
            cmd.CommandType = CommandType.Text;
            tbID.Text = cmd.ExecuteScalar().ToString();

            {

                using (SqlCommand cmd1 = new SqlCommand("INSERT INTO Student (std_id,std_name,std_f_name,std_mob,std_gender,std_cnic,std_campus,std_dep,std_address,std_batch,std_batch_year)VALUES(@std_id,@std_name,@std_f_name,@std_mob,@std_gender,@std_cnic,@std_campus,@std_dep,@std_address,@std_batch,@std_batch_year)VALUES(@campus_id,@campus_name)", con))
                {

                    cmd1.CommandType = CommandType.Text;
                    cmd1.Parameters.AddWithValue("@std_id", tbID.Text);
                    cmd1.Parameters.AddWithValue("@std_name", tbName.Text);
                    cmd1.Parameters.AddWithValue("@std_f_name", tbFatherName.Text);
                    cmd1.Parameters.AddWithValue("@std_mob", tbMobNumber.Text);
                    cmd1.Parameters.AddWithValue("@std_gender", GetGender());
                    cmd1.Parameters.AddWithValue("@std_cnic", tbMobNumber.Text);
                    cmd1.Parameters.AddWithValue("@std_campus",(cBoxCampus.SelectedIndex == -1) ? 0: cBoxCampus.SelectedValue);
                    cmd1.Parameters.AddWithValue("@std_dep", (cBoxDepartment.SelectedIndex == -1) ? 0 : cBoxDepartment.SelectedValue);
                    cmd1.Parameters.AddWithValue("@std_address", tbAddress.Text);
                    cmd1.Parameters.AddWithValue("@std_batch", tbBatchNo.Text);
                    cmd1.Parameters.AddWithValue("@std_batch_year", tbBatchYear.Text);
                    cmd1.ExecuteNonQuery();
                    con.Close();
                    MessageBox.Show("Record Saved");

                }
            }


        }
    }
like image 715
Af'faq Avatar asked Oct 25 '16 12:10

Af'faq


1 Answers

Replace

cBoxCampus.SelectedValue = "campus_id";

With ListControl.ValueMember Property

cBoxCampus.ValueMember = "campus_id";

Do similar operation for cBoxDepartment

like image 170
Jaydip Jadhav Avatar answered Sep 24 '22 17:09

Jaydip Jadhav