C#
private void mComBale_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
string query1 = "SELECT baleno, SUM(mtrs) FROM tbChallan GROUP BY baleno'" + mComBale.SelectedItem.ToString() + "'";
cm = new SqlCommand(query1, con);
con.Open();
dr = cm.ExecuteReader();
while (dr.Read())
{
rsLblBaleTotal.Text = dr["baleno"].ToString();
}
dr.Close();
con.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}syntax is I write correctly. but when I select the item from combobox then I need sum of mtrs from baleno
any correction in code??
pls help

Rajkiran SwainRank #165 · 14.4K reputationPosted May 2, 2023, 11:56 AM EDT
string query1 = "SELECT baleno, SUM(mtrs) AS totalMtrs FROM tbChallan WHERE baleno='" + mComBale.SelectedItem.ToString() + "' GROUP BY baleno";There is an error in your SQL query. The GROUP BY clause is missing the column name. The corrected query should be like above query:
rsLblBaleTotal.Text = dr["totalMtrs"].ToString();Also, you need to assign the value of the calculated sum to the label control. So, the line inside the while loop should be:
Luv LRank #8,316 · 224 reputationPosted May 2, 2023, 12:35 PM EDT
Many Thanks.