2
I’ve got a problem with mine query, when there are values in the table the sum normally happens as programmed:
string nomeProd = item.SubItems[0].Text;
double Quantidade = Convert.ToDouble(item.SubItems[2].Text);
cmdQry.CommandText = "SELECT SUM(quantidadeMat) FROM tbl_EstoqueMat WHERE reservadoMat=False AND nomeMat=@nome";
cmdQry.Parameters.Clear();
cmdQry.Parameters.Add(new OleDbParameter("@nome", nomeProd));
try
{
//SOMA AQUI, OCORRE NORMALMENTE QUANDO HÁ DADOS NA TABELA
double soma = Convert.ToDouble(cmdQry.ExecuteScalar());
When no data is in the table, or not available for summation, the following error occurs:
The value is returned null. How to deal with this type of problem?
I use Oledb, that is, Access and C#.
Okay, good answer, that’s what I was looking for. Thanks for the tips. And one more thing: why is double usage inappropriate? The values in my bank are double. Does this cause any problem? And why opt for decimal? I see many using. Grateful
– Thales
Yes, cause, read the link that I posted. I don’t even know if I should accept null in the snare. And this https://answall.com/a/56299/101 or this https://answall.com/q/15261/101.
– Maniero
There is no null in the column, my query filters only specified data, and it may occur that none is available, or some are available. Thank you so much for the answers and tips, helped me a lot!
– Thales
Set "not available"
– Maniero
Basically, it’s a stock system, and in the case of the code, I’m dealing with the stock of raw materials. When a production order is opened, the system calculates the required amount of raw material and offers the user option to select which one to use. When one is selected it is reserved and cannot be used in another production due to the handling processes at the factory. This code you helped me with is to check the quantity of the materials in stock. If there is no availability there will be no production yet.
– Thales
You have described your software, but not what is "not available". Because if this is 0, then the sum should occur normally and your problem is another.
– Maniero
Not being available means that the matter is marked as "Reserved", that is, the code will ignore the matter, which will have quantity, either "500" or "5", but the code should not add.
– Thales
I get it, then nothing comes into the sum, then it’s null anyway.
– Maniero
One more thing, now the variable "result" is coming back empty and not null. How should I deal?
– Thales
What does "empty mean"?
– Maniero
It says that the value is "{ }". By performing if the way you put it, it simply jumps to "Else"
– Thales
I didn’t put
else
some. You need to see this right, because either you did something you shouldn’t have done or something different is happening than it should be. Then I will say that the question does not have all the data it should. This data should not happen in correct code.– Maniero
Yeah, you didn’t put the
else
, but the problem is he doesn’t read theresultado
null. It does not execute theif
that you put.– Thales
There is something missing from your question. It only shows a null error.
– Maniero
Yes, but there’s another problem now. Yours
if
does not work. Theresultado
is not returned null, and the part where you placevar soma = ToDecimal(resultado);
does not work, it presents the same exception, "Object cannot be converted from Dbnull into other types".– Thales
Try switching
null
forDBNull.Value
.– Maniero
Thank you so much! It’s 100% now.
– Thales