Click here to Skip to main content
15,887,939 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Plz help me!

I am developing a VS 2010 C# windows form application such that I have a checkedlistbox with subject name when i choose course from combobox then checkboxlist shows all subject related to course name. also I have simple checkbox for (select all subject), i want to check all subject or check particular subject . Now when i checked subject then fee will be added and show on textbox . fee is given into the database of subject table

For example:
Subject Name= Fee
Checkedlistbox item 1 English= 1000
Checkedlistbox item 2 Hindi = 500
Checkedlistbox item 3 Marathi = 800
Checkedlistbox item 4 Science= 2000
Checkedlistbox item .......n

On a subject selected event, If for instance I checked items 1,2 and 4, I want the sum of these values (i.e 3500)
displayed in textbox1.
Many thanks.

What I have tried:

C#
private void chkselectsubject_CheckedChanged(object sender, EventArgs e)
        {            
            if (chkselectsubject.Checked)
            {               

                for (int i = 0; i < chklbselectsubject.Items.Count; i++)
                {
                    chklbselectsubject.SetItemChecked(i, true);
                   
                }              
            }
            else
            {
                for (int i = 0; i < chklbselectsubject.Items.Count; i++)
                {
                    chklbselectsubject.SetItemChecked(i, false);
                
                }
            }
Posted
Updated 2-Jul-17 22:55pm
v2
Comments
Prifti Constantine 3-Jul-17 3:58am    
Does each item of the CheckListBox hold a value of type integer or decimal inside of it?
Or does it Hold the value : "English:1000" as a string type?
Member 13289720 3-Jul-17 4:02am    
no sir, simple display subject name in cheklistbox and the subject fee is into the database of subject_master table. So, how i can do addition of each subject fee which i am selected from checklistbox
Prifti Constantine 3-Jul-17 4:23am    
Have you stored the values that you have in the database inside a dataset or datatable in order to use them or even query them?

1 solution

You could iterate though the List that is made to store inside the
C#
List<string> subjects = new List<string>();
            foreach (var item in chekBox.Items) {
                subjects.Add(item.ToString());
            }

           
   string sqlcommand = "Select * from tblSubjectRates where subjectName=@SubjectName";
            List<int> SubjectRates = new List<int>();
            using (SqlConnection con = new SqlConnection(connectionString)) {
                con.Open();
                using (SqlCommand cmd = new SqlCommand(sqlcommand, con)) {
                    foreach (var item in subjects) {
                        cmd.Parameters.AddWithValue("@SubjectName", item.ToString());
                        var x = cmd.ExecuteScalar();
                        SubjectRates.Add(Convert.ToInt32(x));
                    }                 
                    
                }
                int TotalSum = SubjectRates.Sum();
            }


I dont know if it will work... but give it a try.
I tried to generate it in my mind since i do not know your database schema in order to fully help you out. Just replace the keywords you think that will generate an error and i hope that this logic will work!
Best wishes and fingers crossed!
 
Share this answer
 
Comments
Member 13289720 3-Jul-17 6:45am    
thx sir,
but i really didn't understand how to add the subject fee
**Simple i display subject name in checklistbox and the subject fee is into the database of tblsubject table. So, how i can do addition of each subject fee which i am selected subject from checklistbox instead of following code
private void chkselectsubject_CheckedChanged(object sender, EventArgs e)
{
if (chkselectsubject.Checked)
{
for (int i = 0; i < chklbselectsubject.Items.Count; i++)
{
chklbselectsubject.SetItemChecked(i, true);
checkprice();
}
}
else
{
for (int i = 0; i < chklbselectsubject.Items.Count; i++)
{
chklbselectsubject.SetItemChecked(i, false);
checkprice();
}
}
}

private void checkprice()
{

SqlConnection con = new SqlConnection(cs.DBcon);
con.Open();
string strsql = "Select Sum(fee) from Subject_Master where Sub_Name='"+chklbselectsubject.SelectedItems+"'";
SqlCommand cmd= new SqlCommand(strsql, con);

object sum = cmd.ExecuteScalar();
txtTotalfee.Text = sum.ToString();
con.Close();


}

but additon not done .......Plz help
Prifti Constantine 3-Jul-17 6:56am    
Have you debuged to see what kind of value chklbselectsubject.SelectedItems returns? The way i see it, due to seeing your code for the first time, the error might possibly occur on the selection of the "chklbselectsubject". Also the variable sum doesnt need to be of type object since the sum will probably return an integer value type or mostly double since we are talking for a value type return from the database.
Member 13289720 3-Jul-17 7:28am    
when i using int sum=cmd.executeScalar()
i got an error cannot implicitly convert type 'object' to 'int' an explicit conversion exists.and also the subject not selected from the where condition
how can i do
Prifti Constantine 3-Jul-17 8:05am    
You could change the execute scalar part with the ExecuteReader();
using(sqlDataReader reader = cmd.ExecuteReader())
{
While(reader.Read())
{
int x = Convert.toInt32(reader["price"].toString());
}
}

or Cast the executeScalar() with (int) :
Sum = (int)cmd.executeScalar();
Member 13289720 3-Jul-17 10:28am    
yes its work, but...when i don't use where condition in query then all fee are calculate from tblsubject table i want only selected subject
how i can use query because [where Sub_Name='"+chklbselectsubject.SelectedItems+"'";] its not working

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