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. Problem with for each statement

Problem with for each statement

Scheduled Pinned Locked Moved C#
data-structureshelp
14 Posts 7 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.
  • K kalaveer

    i am using foreach statement to read all the strings in a string array but while reading all the time it is showing System.Data.DataRow in the string identifier here is code which is giving the above specified problem: string[] fileidentifiers = ReturnFileIdentifiers(); foreach (string identifier in fileidentifiers) if (identifier == fileidentifier)

    M Offline
    M Offline
    Martin 0
    wrote on last edited by
    #5

    Hello, Try to use a for loop, with an indexer!

    for(int x=0;x < fileidentifiers.Length; x++)
    {

    as string;

    if(actIdentifier.Equals(fileidentifier)
    {

    }
    

    }

    Hope it helps! All the best, Martin

    K 1 Reply Last reply
    0
    • K kalaveer

      i am using foreach statement to read all the strings in a string array but while reading all the time it is showing System.Data.DataRow in the string identifier here is code which is giving the above specified problem: string[] fileidentifiers = ReturnFileIdentifiers(); foreach (string identifier in fileidentifiers) if (identifier == fileidentifier)

      S Offline
      S Offline
      Sandeep Akhare
      wrote on last edited by
      #6

      kalaveer wrote:

      System.Data.DataRow in the string identifier

      what is your return statement in the ReturnFileIdentifiers(); function or while addding to the array of string you are not properly adding them in ReturnFileIdentifiers(); function

      Thanks and Regards Sandeep If you want something you never had, do something you have never done!

      1 Reply Last reply
      0
      • M Martin 0

        Hello, Try to use a for loop, with an indexer!

        for(int x=0;x < fileidentifiers.Length; x++)
        {

        as string;

        if(actIdentifier.Equals(fileidentifier)
        {

        }
        

        }

        Hope it helps! All the best, Martin

        K Offline
        K Offline
        kalaveer
        wrote on last edited by
        #7

        Even this is not working by the way my ReturnFileIdentifiers code would be below string[] ReturnFileIdentifiers() { using (OleDbConnection connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\Warehouses_older\\Sample1_cat61.mdb")) { OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT MD_Metadata.FileIdentifier FROM MD_Metadata", connection); DataTable FileIDs = new DataTable(); adapter.Fill(FileIDs); string[] fileids = new string[FileIDs.Rows.Count]; for (int i = 0; i < FileIDs.Rows.Count; i++) fileids[i] = FileIDs.Rows[0].ToString(); return fileids; } }

        G M 2 Replies Last reply
        0
        • K kalaveer

          Even this is not working by the way my ReturnFileIdentifiers code would be below string[] ReturnFileIdentifiers() { using (OleDbConnection connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\Warehouses_older\\Sample1_cat61.mdb")) { OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT MD_Metadata.FileIdentifier FROM MD_Metadata", connection); DataTable FileIDs = new DataTable(); adapter.Fill(FileIDs); string[] fileids = new string[FileIDs.Rows.Count]; for (int i = 0; i < FileIDs.Rows.Count; i++) fileids[i] = FileIDs.Rows[0].ToString(); return fileids; } }

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #8

          kalaveer wrote:

          fileids[i] = FileIDs.Rows[0].ToString();

          The ToString method of DataRow is inherited from the Object class, and returns the name of the data type. What is is that you are trying to read from the data table?

          --- single minded; short sighted; long gone;

          K 1 Reply Last reply
          0
          • K kalaveer

            Even this is not working by the way my ReturnFileIdentifiers code would be below string[] ReturnFileIdentifiers() { using (OleDbConnection connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\Warehouses_older\\Sample1_cat61.mdb")) { OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT MD_Metadata.FileIdentifier FROM MD_Metadata", connection); DataTable FileIDs = new DataTable(); adapter.Fill(FileIDs); string[] fileids = new string[FileIDs.Rows.Count]; for (int i = 0; i < FileIDs.Rows.Count; i++) fileids[i] = FileIDs.Rows[0].ToString(); return fileids; } }

            M Offline
            M Offline
            Martin 0
            wrote on last edited by
            #9

            I think you whant to return the Item (FileIDs.Rows[0].Item) from the DataRow class. Don't you?

            1 Reply Last reply
            0
            • K kalaveer

              Yes it is returning all the strings properly.

              S Offline
              S Offline
              sujithkumarsl
              wrote on last edited by
              #10

              i tried with your code but its working. i am pasting the code here please update using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace testApp { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private string[] ReturnFileIdentifiers() { string[] sample = { "sujith", "syam", "tharun" }; return sample; } private void button1_Click(object sender, EventArgs e) { string fileidentifier = "sujith"; string[] fileidentifiers = ReturnFileIdentifiers(); foreach (string identifier in fileidentifiers) if (identifier == fileidentifier) { MessageBox.Show("hi" + fileidentifier); } } } }

              My small attempt...

              1 Reply Last reply
              0
              • G Guffa

                kalaveer wrote:

                fileids[i] = FileIDs.Rows[0].ToString();

                The ToString method of DataRow is inherited from the Object class, and returns the name of the data type. What is is that you are trying to read from the data table?

                --- single minded; short sighted; long gone;

                K Offline
                K Offline
                kalaveer
                wrote on last edited by
                #11

                But while debugging fileids contents are the string values of that perticual field contents which i queried using oledb command.

                C 1 Reply Last reply
                0
                • C Christian Graus

                  Then you're returning a collection of DataRows and you need to pull out the values that you need.

                  Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                  K Offline
                  K Offline
                  kalaveer
                  wrote on last edited by
                  #12

                  How to do so.

                  C 1 Reply Last reply
                  0
                  • K kalaveer

                    But while debugging fileids contents are the string values of that perticual field contents which i queried using oledb command.

                    C Offline
                    C Offline
                    Christian Graus
                    wrote on last edited by
                    #13

                    The debugger is showing you the contents of the rows.

                    Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                    1 Reply Last reply
                    0
                    • K kalaveer

                      How to do so.

                      C Offline
                      C Offline
                      Christian Graus
                      wrote on last edited by
                      #14

                      You have a collection of data rows. Do a foreach that recognises this fact. You can get items out of a data row by index or by name, using array notation.

                      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                      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