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. showing multiple rows in a table after an SQL query

showing multiple rows in a table after an SQL query

Scheduled Pinned Locked Moved C#
databasexmlhelpquestion
5 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.
  • E Offline
    E Offline
    Emmet_Brown
    wrote on last edited by
    #1

    Hi everyone First I'm trying to return multiple rows from a table via XML:

        string strSQLCommand = "SELECT \* FROM Magazalar WHERE Sehir = '" + a + "'";
    
        SqlCommand command = new SqlCommand(strSQLCommand, conn);
    
        TableRowCollection returnvalue = (TableRowCollection)command.ExecuteScalar();
    

    a is cannot be null and the query may return a single row or multiple rows. then, I want to show this returned row collection in a table: MagazaShowTable.Rows = returnvalue; The compiler says that Table.Rows is a Read-Only, and sth cannot be assigned to it, and I couldn't find any suitable option rather than .Rows ... Can anybody help?

    S 1 Reply Last reply
    0
    • E Emmet_Brown

      Hi everyone First I'm trying to return multiple rows from a table via XML:

          string strSQLCommand = "SELECT \* FROM Magazalar WHERE Sehir = '" + a + "'";
      
          SqlCommand command = new SqlCommand(strSQLCommand, conn);
      
          TableRowCollection returnvalue = (TableRowCollection)command.ExecuteScalar();
      

      a is cannot be null and the query may return a single row or multiple rows. then, I want to show this returned row collection in a table: MagazaShowTable.Rows = returnvalue; The compiler says that Table.Rows is a Read-Only, and sth cannot be assigned to it, and I couldn't find any suitable option rather than .Rows ... Can anybody help?

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

      Hi, first of all be aware that command.ExecuteScalar() returns the first column from the first row, not multiple rows. Then as the compiler already says, Rows is a ReadOnly-Property. You could add the rows by using MagazaShowTable.Rows.Add

      It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

      E 1 Reply Last reply
      0
      • S SeMartens

        Hi, first of all be aware that command.ExecuteScalar() returns the first column from the first row, not multiple rows. Then as the compiler already says, Rows is a ReadOnly-Property. You could add the rows by using MagazaShowTable.Rows.Add

        It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

        E Offline
        E Offline
        Emmet_Brown
        wrote on last edited by
        #3

        Thanks for answering... So what is the suitable function for calling multiple rows? command.ExecuteReader() ? I'm converting the returned value to TableRowCollection, is it right? and how I can I show this row collection in my table?

        S 1 Reply Last reply
        0
        • E Emmet_Brown

          Thanks for answering... So what is the suitable function for calling multiple rows? command.ExecuteReader() ? I'm converting the returned value to TableRowCollection, is it right? and how I can I show this row collection in my table?

          S Offline
          S Offline
          SeMartens
          wrote on last edited by
          #4

          ExecuteReader is what you need. The returned value is a SqlDataReader. With this you can iterate through the rows. Simpler approach is to use a dataset with a dataadapter. This will read the rows within a table anyway. But first you can use ExecuteReader, iterate through the rows and add the rows with values to your datatable.

          It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

          E 1 Reply Last reply
          0
          • S SeMartens

            ExecuteReader is what you need. The returned value is a SqlDataReader. With this you can iterate through the rows. Simpler approach is to use a dataset with a dataadapter. This will read the rows within a table anyway. But first you can use ExecuteReader, iterate through the rows and add the rows with values to your datatable.

            It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

            E Offline
            E Offline
            Emmet_Brown
            wrote on last edited by
            #5

            I'll give it a try thank you! :D

            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