Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. DaraReader problem ...

DaraReader problem ...

Scheduled Pinned Locked Moved C#
help
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nassimnastaran
    wrote on last edited by
    #1

    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 !

    P D 2 Replies Last reply
    0
    • N nassimnastaran

      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 !

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      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 for DBNull.Value (for when ther are no matching records).

      You'll never get very far if all you do is follow instructions.

      D N 2 Replies Last reply
      0
      • N nassimnastaran

        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 !

        D Offline
        D Offline
        DamithSL
        wrote on last edited by
        #3

        try with

        cmdCacl = new OleDbCommand(CalcQuery, cnnCalc);
        txtBuyAmount.Text = cmdCacl.ExecuteScalar() as string;

        If You Never Try You’ll Never Know

        1 Reply Last reply
        0
        • P PIEBALDconsult

          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 for DBNull.Value (for when ther are no matching records).

          You'll never get very far if all you do is follow instructions.

          D Offline
          D Offline
          DamithSL
          wrote on last edited by
          #4

          Very nicely explained :thumbsup:

          If You Never Try You’ll Never Know

          1 Reply Last reply
          0
          • P PIEBALDconsult

            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 for DBNull.Value (for when ther are no matching records).

            You'll never get very far if all you do is follow instructions.

            N Offline
            N Offline
            nassimnastaran
            wrote on last edited by
            #5

            I want To do This :

            BuyAmount.Text = "0";

            When there is No Row in dataReader thanks

            P 1 Reply Last reply
            0
            • N nassimnastaran

              I want To do This :

              BuyAmount.Text = "0";

              When there is No Row in dataReader thanks

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #6

              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 other if. You don't need two ifs; you don't need to test HasRows. 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.

              N 1 Reply Last reply
              0
              • P PIEBALDconsult

                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 other if. You don't need two ifs; you don't need to test HasRows. 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.

                N Offline
                N Offline
                nassimnastaran
                wrote on last edited by
                #7

                Yes , thanks , I use If and else when dbIsNull . It works .

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups