DaraReader problem ...
-
Hi ALL ! I want to execute this by OledbDataReader when I want to take ... :
string CalcQuery = @"SELECT SUM(BuyAmount) From SubTblMetal WHERE IDMetal=" + int.Parse(txtId.ToString());
OleDbCommand cmdCacl;
cnnCalc.Open();
cmdCacl = new OleDbCommand(CalcQuery, cnnCalc);
OleDbDataReader dr;
dr = cmdCacl.ExecuteReader();
if (dr.HasRows==false)
{
txtBuyAmount.Text = "0";
throw new Exception();
}
else if (dr.Read())
{
txtBuyAmount.Text = dr[0].ToString();}
But when my DataReader has not any row ,I have an error for the value of txtBuAmount.Text please Help , how can get the value of Zero (0) , when dataRow has not any row(value). Thanks a lot !
-
Hi ALL ! I want to execute this by OledbDataReader when I want to take ... :
string CalcQuery = @"SELECT SUM(BuyAmount) From SubTblMetal WHERE IDMetal=" + int.Parse(txtId.ToString());
OleDbCommand cmdCacl;
cnnCalc.Open();
cmdCacl = new OleDbCommand(CalcQuery, cnnCalc);
OleDbDataReader dr;
dr = cmdCacl.ExecuteReader();
if (dr.HasRows==false)
{
txtBuyAmount.Text = "0";
throw new Exception();
}
else if (dr.Read())
{
txtBuyAmount.Text = dr[0].ToString();}
But when my DataReader has not any row ,I have an error for the value of txtBuAmount.Text please Help , how can get the value of Zero (0) , when dataRow has not any row(value). Thanks a lot !
First off, please use a parameterized query rather than concatenating the value into the statement. Next, don't trust HasRows. Also, why is that
throw
in there? As you are expecting at most one value, you might do better with ExecuteScalar and then test forDBNull.Value
(for when ther are no matching records).You'll never get very far if all you do is follow instructions.
-
Hi ALL ! I want to execute this by OledbDataReader when I want to take ... :
string CalcQuery = @"SELECT SUM(BuyAmount) From SubTblMetal WHERE IDMetal=" + int.Parse(txtId.ToString());
OleDbCommand cmdCacl;
cnnCalc.Open();
cmdCacl = new OleDbCommand(CalcQuery, cnnCalc);
OleDbDataReader dr;
dr = cmdCacl.ExecuteReader();
if (dr.HasRows==false)
{
txtBuyAmount.Text = "0";
throw new Exception();
}
else if (dr.Read())
{
txtBuyAmount.Text = dr[0].ToString();}
But when my DataReader has not any row ,I have an error for the value of txtBuAmount.Text please Help , how can get the value of Zero (0) , when dataRow has not any row(value). Thanks a lot !
-
First off, please use a parameterized query rather than concatenating the value into the statement. Next, don't trust HasRows. Also, why is that
throw
in there? As you are expecting at most one value, you might do better with ExecuteScalar and then test forDBNull.Value
(for when ther are no matching records).You'll never get very far if all you do is follow instructions.
-
First off, please use a parameterized query rather than concatenating the value into the statement. Next, don't trust HasRows. Also, why is that
throw
in there? As you are expecting at most one value, you might do better with ExecuteScalar and then test forDBNull.Value
(for when ther are no matching records).You'll never get very far if all you do is follow instructions.
I want To do This :
BuyAmount.Text = "0";
When there is No Row in dataReader thanks
-
I want To do This :
BuyAmount.Text = "0";
When there is No Row in dataReader thanks
I understand, but are there ever no rows? Or do you get a NULL (DBNull.Value) when there are no matching rows? Even if you do get no rows, it's better to handle that with an
else
on the otherif
. You don't need twoif
s; you don't need to testHasRows
.HasRows
is not a member of the IDataReader[^] interface so you can't count on it being in every database system you use (if you ever have to use multiple database system, like I do). Every time I've tried to use HasRows I got burnt.You'll never get very far if all you do is follow instructions.
-
I understand, but are there ever no rows? Or do you get a NULL (DBNull.Value) when there are no matching rows? Even if you do get no rows, it's better to handle that with an
else
on the otherif
. You don't need twoif
s; you don't need to testHasRows
.HasRows
is not a member of the IDataReader[^] interface so you can't count on it being in every database system you use (if you ever have to use multiple database system, like I do). Every time I've tried to use HasRows I got burnt.You'll never get very far if all you do is follow instructions.
Yes , thanks , I use If and else when dbIsNull . It works .