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 create a Function return as DataTable with C# ?

how create a Function return as DataTable with C# ?

Scheduled Pinned Locked Moved C#
helpcsharpquestion
4 Posts 4 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.
  • X Offline
    X Offline
    xingselex
    wrote on last edited by
    #1

    my code is like bellow. but i don know the reason why it error? it message "Cannot assign to 'mnuItem' because it is a method group". please help me...... :( public DataTable mnuItem() { DataConnection dc = new DataConnection(); SqlConnection Cn = new SqlConnection(dc.ConnectStr); SqlDataAdapter Da = new SqlDataAdapter(); DataSet Ds = new DataSet (); DataTable Dt = new DataTable(); Cn.Open (); SqlCommand Cmd = new SqlCommand("select ObjectID, ObjectName from _Objects where ObjectType='MENU' ", Cn); Da.SelectCommand = Cmd; Da.Fill(Ds, "_Objects"); Dt = Ds.Tables["_Objects"]; Da.Dispose(); Cn.Close(); mnuItem = Dt; return mnuItem ; }

    0 M C 3 Replies Last reply
    0
    • X xingselex

      my code is like bellow. but i don know the reason why it error? it message "Cannot assign to 'mnuItem' because it is a method group". please help me...... :( public DataTable mnuItem() { DataConnection dc = new DataConnection(); SqlConnection Cn = new SqlConnection(dc.ConnectStr); SqlDataAdapter Da = new SqlDataAdapter(); DataSet Ds = new DataSet (); DataTable Dt = new DataTable(); Cn.Open (); SqlCommand Cmd = new SqlCommand("select ObjectID, ObjectName from _Objects where ObjectType='MENU' ", Cn); Da.SelectCommand = Cmd; Da.Fill(Ds, "_Objects"); Dt = Ds.Tables["_Objects"]; Da.Dispose(); Cn.Close(); mnuItem = Dt; return mnuItem ; }

      0 Offline
      0 Offline
      0x3c0
      wrote on last edited by
      #2

      You are assigning the function mnuItem the value of a DataTable. This would have worked in VB, but in C# you just need to use return Dt;. Incidentally, 'return mnuItem' would generate a StackOverflowException and a syntax error. Remove it

      1 Reply Last reply
      0
      • X xingselex

        my code is like bellow. but i don know the reason why it error? it message "Cannot assign to 'mnuItem' because it is a method group". please help me...... :( public DataTable mnuItem() { DataConnection dc = new DataConnection(); SqlConnection Cn = new SqlConnection(dc.ConnectStr); SqlDataAdapter Da = new SqlDataAdapter(); DataSet Ds = new DataSet (); DataTable Dt = new DataTable(); Cn.Open (); SqlCommand Cmd = new SqlCommand("select ObjectID, ObjectName from _Objects where ObjectType='MENU' ", Cn); Da.SelectCommand = Cmd; Da.Fill(Ds, "_Objects"); Dt = Ds.Tables["_Objects"]; Da.Dispose(); Cn.Close(); mnuItem = Dt; return mnuItem ; }

        M Offline
        M Offline
        MidwestLimey
        wrote on last edited by
        #3

        Additionally, the following line is pointless:

        xingselex wrote:

        DataTable Dt = new DataTable();

        Since you assign it later on to:

        xingselex wrote:

        Dt = Ds.Tables["_Objects"];

        This means you're creating an instance of a DataTable that is then discarded without use. Instead just declare it, or explicitly assign to null:

        DataTable Dt;

        or

        DataTable Dt = null;

        10110011001111101010101000001000001101001010001010100000100000101000001000111100010110001011001011

        1 Reply Last reply
        0
        • X xingselex

          my code is like bellow. but i don know the reason why it error? it message "Cannot assign to 'mnuItem' because it is a method group". please help me...... :( public DataTable mnuItem() { DataConnection dc = new DataConnection(); SqlConnection Cn = new SqlConnection(dc.ConnectStr); SqlDataAdapter Da = new SqlDataAdapter(); DataSet Ds = new DataSet (); DataTable Dt = new DataTable(); Cn.Open (); SqlCommand Cmd = new SqlCommand("select ObjectID, ObjectName from _Objects where ObjectType='MENU' ", Cn); Da.SelectCommand = Cmd; Da.Fill(Ds, "_Objects"); Dt = Ds.Tables["_Objects"]; Da.Dispose(); Cn.Close(); mnuItem = Dt; return mnuItem ; }

          C Offline
          C Offline
          cod3newbie
          wrote on last edited by
          #4

          I think u just remove the last 2 lines. Then after Cn.Close(); write return Dt;

          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