I want to get the total of the books in the database using sum but if there is no book in the database, it gives error. I want it to write 0 instead of error. My codes are below.
OleDbCommand cmd = new OleDbCommand("select sum(adet) from kitaplar25", conn);
cmd.Parameters.AddWithValue("@adet", label14.Text);
label14.Text = cmd.ExecuteScalar().ToString();

Rajkiran SwainRank #165 · 14.4K reputationPosted Apr 7, 2023, 11:48 AM EDT
OleDbCommand cmd = new OleDbCommand("SELECT SUM(adet) FROM kitaplar25", conn); object result = cmd.ExecuteScalar(); if (result != null) { label14.Text = result.ToString(); } else { label14.Text = "0"; }Tuhin PaulRank #7 · 71.9K reputationPosted Apr 7, 2023, 1:24 PM EDT
To handle the case where there are no books in the database and avoid the error, you can use the
COALESCEfunction in your SQL query to replace theNULLvalue with 0.Tuhin PaulRank #7 · 71.9K reputationPosted Apr 7, 2023, 1:25 PM EDT
OleDbCommand cmd = new OleDbCommand("SELECT COALESCE(SUM(adet), 0) AS total FROM kitaplar25", conn); label14.Text = cmd.ExecuteScalar().ToString();we use the
COALESCEfunction to return the sum of books (SUM(adet)) or 0 if the result isNULL. We also use theASkeyword to alias the result astotal. Finally, we assign the result to thelabel14.Textproperty.Mehmet FatihRank #1,474 · 1.1K reputationPosted Apr 8, 2023, 4:23 AM EDT
Thanks you everyone for your efforts. I have solved the problem changing something in Rajkiran Swain's answer like that if (result != DBNull.Value).
Rajkiran SwainRank #165 · 14.4K reputationPosted Apr 8, 2023, 12:01 PM EDT
If it will be helpful, please accept the answer.
Jignesh KumarRank #91 · 22.9K reputationPosted Apr 7, 2023, 12:04 PM EDT
Hi,
Please check below and small change is query for isNull() function will solve your issue
OleDbCommand cmd = new OleDbCommand("select isNull(sum(isNull(adet,0)),0)from kitaplar25", conn); cmd.Parameters.AddWithValue("@adet", label14.Text); label14.Text = cmd.ExecuteScalar().ToString();Rajkiran SwainRank #165 · 14.4K reputationPosted Apr 7, 2023, 2:26 PM EDT
OleDbCommand cmd = new OleDbCommand("SELECT ISNULL(SUM(adet), 0) FROM kitaplar25", conn); label14.Text = cmd.ExecuteScalar().ToString();Try this one too.
Mehmet FatihRank #1,474 · 1.1K reputationPosted Apr 7, 2023, 4:34 PM EDT
I am sorry but none of them gives 0 result.
Amit MohantyRank #112 · 20K reputationPosted Apr 7, 2023, 11:56 PM EDT
OleDbCommand cmd = new OleDbCommand("SELECT ISNULL(SUM(adet), 0) FROM kitaplar25", conn); label14.Text = cmd.ExecuteScalar().ToString();Muhammad Imran AnsariRank #326 · 7.6K reputationPosted Apr 8, 2023, 12:31 AM EDT
With an alternative way, you can use the ternary operator to provide a default value for null values. Here's an example:
OleDbCommand cmd = new OleDbCommand("SELECT SUM(adet) FROM kitaplar25", conn); object result = cmd.ExecuteScalar(); int sum = result == null ? 0 : Convert.ToInt32(result); // Do something with the sumMehmet FatihRank #1,474 · 1.1K reputationPosted Apr 8, 2023, 3:32 AM EDT
You all are trying to find solution to my problem but I am very sorry to infirm that all of them give error. I am usinf access database. Maybe it depends on it. The closest answer is Rajkiran Swain's answer. The only problem with Rajkiran Swain's solution is that it doesn't write zeros.