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. How to dump more than one row from table into the array !!!! please Help

How to dump more than one row from table into the array !!!! please Help

Scheduled Pinned Locked Moved C#
csharpdata-structureshelptutorial
3 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.
  • A Offline
    A Offline
    ANKUR22
    wrote on last edited by
    #1

    hello , my self ankur , i am very new to .net (C#) I had made an function in vb .net but same logic is not workin in C# please help. I had Written an Following Code . try { da = new SqlDataAdapter(Cmd); dt = new DataTable(); da.Fill(dt); int i=0,j=0,p=0,k=0,m=0; string[] arr = new string[((dt.Columns.Count) * 3)]; if (dt.Rows.Count <= 0) return arr; for (j = 0; j <= dt.Rows.Count - 1; j++) { if ((j == 0)) { for (i = 0; i <= dt.Columns.Count - 1; i++) { arr[i] =dt.Rows[0].ItemArray[i].ToString(); //(dt.Rows[0].Item(i)); p = i; } } if ((j == 1)) { k = p + 1; //For k = i To dt.Columns.Count - 1 for (i = 0; i <= dt.Columns.Count - 1; i++) { arr[k] =dt.Rows[1].ItemArray[i].ToString(); k = k + 1; } } if ((j == 2)) { m = k; //For k = i To dt.Columns.Count - 1 for (i = 0; i <= dt.Columns.Count - 1; i++) { arr[m] =dt.Rows[2].ItemArray[i].ToString(); m = m + 1; } //Next } }

    J B 2 Replies Last reply
    0
    • A ANKUR22

      hello , my self ankur , i am very new to .net (C#) I had made an function in vb .net but same logic is not workin in C# please help. I had Written an Following Code . try { da = new SqlDataAdapter(Cmd); dt = new DataTable(); da.Fill(dt); int i=0,j=0,p=0,k=0,m=0; string[] arr = new string[((dt.Columns.Count) * 3)]; if (dt.Rows.Count <= 0) return arr; for (j = 0; j <= dt.Rows.Count - 1; j++) { if ((j == 0)) { for (i = 0; i <= dt.Columns.Count - 1; i++) { arr[i] =dt.Rows[0].ItemArray[i].ToString(); //(dt.Rows[0].Item(i)); p = i; } } if ((j == 1)) { k = p + 1; //For k = i To dt.Columns.Count - 1 for (i = 0; i <= dt.Columns.Count - 1; i++) { arr[k] =dt.Rows[1].ItemArray[i].ToString(); k = k + 1; } } if ((j == 2)) { m = k; //For k = i To dt.Columns.Count - 1 for (i = 0; i <= dt.Columns.Count - 1; i++) { arr[m] =dt.Rows[2].ItemArray[i].ToString(); m = m + 1; } //Next } }

      J Offline
      J Offline
      Justin Perez
      wrote on last edited by
      #2

      What do you mean by "not working?"

      I get all the news I need from the weather report - Paul Simon (from "The Only Living Boy in New York")

      1 Reply Last reply
      0
      • A ANKUR22

        hello , my self ankur , i am very new to .net (C#) I had made an function in vb .net but same logic is not workin in C# please help. I had Written an Following Code . try { da = new SqlDataAdapter(Cmd); dt = new DataTable(); da.Fill(dt); int i=0,j=0,p=0,k=0,m=0; string[] arr = new string[((dt.Columns.Count) * 3)]; if (dt.Rows.Count <= 0) return arr; for (j = 0; j <= dt.Rows.Count - 1; j++) { if ((j == 0)) { for (i = 0; i <= dt.Columns.Count - 1; i++) { arr[i] =dt.Rows[0].ItemArray[i].ToString(); //(dt.Rows[0].Item(i)); p = i; } } if ((j == 1)) { k = p + 1; //For k = i To dt.Columns.Count - 1 for (i = 0; i <= dt.Columns.Count - 1; i++) { arr[k] =dt.Rows[1].ItemArray[i].ToString(); k = k + 1; } } if ((j == 2)) { m = k; //For k = i To dt.Columns.Count - 1 for (i = 0; i <= dt.Columns.Count - 1; i++) { arr[m] =dt.Rows[2].ItemArray[i].ToString(); m = m + 1; } //Next } }

        B Offline
        B Offline
        bit_cmdr
        wrote on last edited by
        #3

        I'm not really sure exactly what you're trying to accomplish here, but I think this code below might suit you better: string[] arr = new string[dt.Rows.Count * dt.Columns.Count]; int i = 0; int j = 0; foreach (DataRow dr in dt.Rows) {     j = 0;     foreach (DataColumn dc in dt.Columns)     {         arr[i] = dr.ItemArray[j].ToString();         j++;         i++;     } } The results of this would be an array structured as: [0] = row1 col1 [1] = row1 col2 [2] = row1 col3 [3] = row2 col1 ... Hope that helps, if it doesn't be a little more clear on what you're trying to accomplish and maybe we can come up with something better.

        - Arcond

        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