You could iterate though the List that is made to store inside the
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!