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. Database & SysAdmin
  3. Database
  4. how to find the return value of reader in mysql

how to find the return value of reader in mysql

Scheduled Pinned Locked Moved Database
mysqltutorial
6 Posts 2 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.
  • M Offline
    M Offline
    Member 10263519
    wrote on last edited by
    #1

    hi, am selecting data from table . and executed reader with rdr=cmd.executereader() based on the result i need to write the logic. if( returns null) { } else { }

    S 1 Reply Last reply
    0
    • M Member 10263519

      hi, am selecting data from table . and executed reader with rdr=cmd.executereader() based on the result i need to write the logic. if( returns null) { } else { }

      S Offline
      S Offline
      Simon_Whale
      wrote on last edited by
      #2

      simple

      if(rdr.HasRows == true)
      {
      //do stuff
      }
      else
      {
      //do no rows returned stuff
      }

      here is an example for you. http://stackoverflow.com/questions/12609959/how-to-check-if-sqldatareader-has-no-rows[^]

      Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON

      M 1 Reply Last reply
      0
      • S Simon_Whale

        simple

        if(rdr.HasRows == true)
        {
        //do stuff
        }
        else
        {
        //do no rows returned stuff
        }

        here is an example for you. http://stackoverflow.com/questions/12609959/how-to-check-if-sqldatareader-has-no-rows[^]

        Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON

        M Offline
        M Offline
        Member 10263519
        wrote on last edited by
        #3

        eventhough it's not working properly. here is the code

        string query1 = "SELECT tag_id,category_id,product_id,product_name,gate_id,remarks FROM transaction where tag_id Like '" + sEPC + "' AND out_time=NULL";
        MySqlCommand cmd1 = new MySqlCommand(query1, connection);
        MySqlDataReader rdr1 = cmd1.ExecuteReader();
        //int c = rdr1.RecordsAffected;
        // MessageBox.Show("count" + c);
        //if (rdr1 !=null && rdr1.Read() && rdr1.HasRows)
        if (rdr1.HasRows==true)
        {
        //ListViewItem item1 = new ListViewItem();
        item.SubItems[OUTTime.Index].Text = s2;
        //item.SubItems[5].Text = s2;
        //ListView1_EPC.Items.Add(item);
        //ListView1_EPC.Items[0].SubItems[OUTTime.Index].Text = s2;
        rdr1.Close();
        String s1 = "update transaction set out_time='" + s2 + "' where tag_id='" + sEPC + "' ";
        MySqlCommand cmd2 = new MySqlCommand(s1, connection);
        cmd2.ExecuteNonQuery();

                                        }
                                        else
                                        {
                                            rdr1.Close();
                                            item.SubItems\[INTime.Index\].Text = s2;
                                            // ListView1\_EPC.Items.Add(item);
                                            String a0 = ListView1\_EPC.Items\[0\].Text;
                                            String a1 = item.SubItems\[1\].Text;
                                            String a2 = item.SubItems\[2\].Text;
                                            String a3 = item.SubItems\[3\].Text;
                                            String a4 = item.SubItems\[4\].Text;
                                            String a5 = item.SubItems\[5\].Text;
        
                                            String str = "insert into transaction(tag\_id,category\_id,product\_id,product\_name,gate\_id,in\_time,remarks)values('" + a0+ "','" + a1 + "','" + a2 + "','" + a3 + "','" + a4 + "','" +s2 + "' ,'"+a5+"')";
        
        S 1 Reply Last reply
        0
        • M Member 10263519

          eventhough it's not working properly. here is the code

          string query1 = "SELECT tag_id,category_id,product_id,product_name,gate_id,remarks FROM transaction where tag_id Like '" + sEPC + "' AND out_time=NULL";
          MySqlCommand cmd1 = new MySqlCommand(query1, connection);
          MySqlDataReader rdr1 = cmd1.ExecuteReader();
          //int c = rdr1.RecordsAffected;
          // MessageBox.Show("count" + c);
          //if (rdr1 !=null && rdr1.Read() && rdr1.HasRows)
          if (rdr1.HasRows==true)
          {
          //ListViewItem item1 = new ListViewItem();
          item.SubItems[OUTTime.Index].Text = s2;
          //item.SubItems[5].Text = s2;
          //ListView1_EPC.Items.Add(item);
          //ListView1_EPC.Items[0].SubItems[OUTTime.Index].Text = s2;
          rdr1.Close();
          String s1 = "update transaction set out_time='" + s2 + "' where tag_id='" + sEPC + "' ";
          MySqlCommand cmd2 = new MySqlCommand(s1, connection);
          cmd2.ExecuteNonQuery();

                                          }
                                          else
                                          {
                                              rdr1.Close();
                                              item.SubItems\[INTime.Index\].Text = s2;
                                              // ListView1\_EPC.Items.Add(item);
                                              String a0 = ListView1\_EPC.Items\[0\].Text;
                                              String a1 = item.SubItems\[1\].Text;
                                              String a2 = item.SubItems\[2\].Text;
                                              String a3 = item.SubItems\[3\].Text;
                                              String a4 = item.SubItems\[4\].Text;
                                              String a5 = item.SubItems\[5\].Text;
          
                                              String str = "insert into transaction(tag\_id,category\_id,product\_id,product\_name,gate\_id,in\_time,remarks)values('" + a0+ "','" + a1 + "','" + a2 + "','" + a3 + "','" + a4 + "','" +s2 + "' ,'"+a5+"')";
          
          S Offline
          S Offline
          Simon_Whale
          wrote on last edited by
          #4

          Define not working properly? also to read from the reader you have to do this

          if(rdr1.HasRows == true)
          {
          while (rdr1.Read())
          {
          //have made an assumption here that you have a ID field in the data
          //so that you can see how to get data back from the reader.
          int id = Convert.ToInt(rdr1["ID"]);
          }
          }

          Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON

          M 2 Replies Last reply
          0
          • S Simon_Whale

            Define not working properly? also to read from the reader you have to do this

            if(rdr1.HasRows == true)
            {
            while (rdr1.Read())
            {
            //have made an assumption here that you have a ID field in the data
            //so that you can see how to get data back from the reader.
            int id = Convert.ToInt(rdr1["ID"]);
            }
            }

            Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON

            M Offline
            M Offline
            Member 10263519
            wrote on last edited by
            #5

            no i don't want to read the data of reader. datatype of out_time and in_time in mysql table is datetime. my problem is: 1.select the data from transaction where tagid=? and outtime=null 2. if reader is having rows then write outtime into transaction table. else insert row into transaction table with only intime, no outtime here is the code :

            string query1 = "SELECT tag_id,category_id,product_id,product_name,gate_id,remarks FROM transaction where tag_id Like '" + sEPC + "' AND out_time=NULL";
            MySqlCommand cmd1 = new MySqlCommand(query1, connection);
            MySqlDataReader rdr1 = cmd1.ExecuteReader();

                                            if (rdr1.HasRows==true)
                                            {
                                              
                                            item.SubItems\[OUTTime.Index\].Text = s2;
                                         rdr1.Close();
                        String s1 = "update transaction set out\_time='" + s2 + "'  where tag\_id='" + sEPC + "' ";
                                                MySqlCommand cmd2 = new MySqlCommand(s1, connection);
                                                cmd2.ExecuteNonQuery();
            
            
                                            }
                                            else 
                                              
                                            {
                                                rdr1.Close();
                                                item.SubItems\[INTime.Index\].Text = s2;
                                                
                                                String a0 = ListView1\_EPC.Items\[0\].Text;
                                                String a1 = item.SubItems\[1\].Text;
                                                String a2 = item.SubItems\[2\].Text;
                                                String a3 = item.SubItems\[3\].Text;
                                                String a4 = item.SubItems\[4\].Text;
                                                String a5 = item.SubItems\[5\].Text;
            
                                                String str = "insert into transaction(tag\_id,category\_id,product\_id,product\_name,gate\_id,in\_time,remarks)values('" + a0+ "','" + a1 + "','" + a2 + "','" + a3 + "','" + a4 + "','" +s2 + "' ,'"+a5+"')";
                                                MySqlCommand cmd2 = new MySqlCommand(str, connection);
                                                cmd2.ExecuteNonQuery();
            
            1 Reply Last reply
            0
            • S Simon_Whale

              Define not working properly? also to read from the reader you have to do this

              if(rdr1.HasRows == true)
              {
              while (rdr1.Read())
              {
              //have made an assumption here that you have a ID field in the data
              //so that you can see how to get data back from the reader.
              int id = Convert.ToInt(rdr1["ID"]);
              }
              }

              Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON

              M Offline
              M Offline
              Member 10263519
              wrote on last edited by
              #6

              ok i got it. thanks for the help.. But now the problem is , i can have multiple records with same tag_id, when i update all the records with same tag_id is updating. i need to update only the latest record. how can i solve this.......

              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