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. Can I get the DataTable properties from an arbitrary DataRow?

Can I get the DataTable properties from an arbitrary DataRow?

Scheduled Pinned Locked Moved C#
dockertutorialquestion
10 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.
  • J Offline
    J Offline
    JoeRip
    wrote on last edited by
    #1

    I have a method which takes a DataRow as an out argument. The method fills out the DataRow with data, and returns it. So,

    public CreateTheFancyTable()
    {
    DataTable dt = new DataTable();
    DataColumn dc1 = new DataColumn("First_Column", System.Int32);
    DataColumn dc2 = new DataColumn("Second_Column", System.String);
    dc1.AllowDBNull = false;
    dc2.AlllowDBNull = false;
    DataRow dr = dt.NewRow();

    bool fSucceeded = FillOutMyRow(dr);
    }

    public bool FillOutMyRow(DataRow dr)
    {
    // fetch some data from an outside source here

     // fill out the row here
    
     dr\["First\_Column"\] = 1;
     dr\["Second\_Column"\] = "";
    

    }

    Please don't focus on the bugs in my quick sample here, the real code is a lot more complex than this. Now, the last line is going to fail, because dt.Columns[dc2].AllowDBNull is false. How, in the code of FillOutMyRow(), can I find out the properties of the columns in the row that was passed to me? I only know how to get the column properties of a table, and I don't have the table. Can I refer directly to the column properties of a row (and if so, how?)? Or do I need to get the parent container of the row (the table), and if so, how? Thanks, Joe

    L J 2 Replies Last reply
    0
    • J JoeRip

      I have a method which takes a DataRow as an out argument. The method fills out the DataRow with data, and returns it. So,

      public CreateTheFancyTable()
      {
      DataTable dt = new DataTable();
      DataColumn dc1 = new DataColumn("First_Column", System.Int32);
      DataColumn dc2 = new DataColumn("Second_Column", System.String);
      dc1.AllowDBNull = false;
      dc2.AlllowDBNull = false;
      DataRow dr = dt.NewRow();

      bool fSucceeded = FillOutMyRow(dr);
      }

      public bool FillOutMyRow(DataRow dr)
      {
      // fetch some data from an outside source here

       // fill out the row here
      
       dr\["First\_Column"\] = 1;
       dr\["Second\_Column"\] = "";
      

      }

      Please don't focus on the bugs in my quick sample here, the real code is a lot more complex than this. Now, the last line is going to fail, because dt.Columns[dc2].AllowDBNull is false. How, in the code of FillOutMyRow(), can I find out the properties of the columns in the row that was passed to me? I only know how to get the column properties of a table, and I don't have the table. Can I refer directly to the column properties of a row (and if so, how?)? Or do I need to get the parent container of the row (the table), and if so, how? Thanks, Joe

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      DataRow has a Table property.

      xacc.ide - now with TabsToSpaces support
      IronScheme - 1.0 alpha 4a out now (29 May 2008)

      J 1 Reply Last reply
      0
      • J JoeRip

        I have a method which takes a DataRow as an out argument. The method fills out the DataRow with data, and returns it. So,

        public CreateTheFancyTable()
        {
        DataTable dt = new DataTable();
        DataColumn dc1 = new DataColumn("First_Column", System.Int32);
        DataColumn dc2 = new DataColumn("Second_Column", System.String);
        dc1.AllowDBNull = false;
        dc2.AlllowDBNull = false;
        DataRow dr = dt.NewRow();

        bool fSucceeded = FillOutMyRow(dr);
        }

        public bool FillOutMyRow(DataRow dr)
        {
        // fetch some data from an outside source here

         // fill out the row here
        
         dr\["First\_Column"\] = 1;
         dr\["Second\_Column"\] = "";
        

        }

        Please don't focus on the bugs in my quick sample here, the real code is a lot more complex than this. Now, the last line is going to fail, because dt.Columns[dc2].AllowDBNull is false. How, in the code of FillOutMyRow(), can I find out the properties of the columns in the row that was passed to me? I only know how to get the column properties of a table, and I don't have the table. Can I refer directly to the column properties of a row (and if so, how?)? Or do I need to get the parent container of the row (the table), and if so, how? Thanks, Joe

        J Offline
        J Offline
        J4amieC
        wrote on last edited by
        #3

        JoeRip wrote:

        Now, the last line is going to fail, because dt.Columns[dc2].AllowDBNull is false.

        :confused: No it wont, you're setting the value to an empty string not DbNull.Value. Anyway, you should be able to go back to the table/columns from the datarow. dr.Table.Columns so you could do if(dr.Table.Columns["Second_Column"].AllowDBNull) { ... }

        J 1 Reply Last reply
        0
        • L leppie

          DataRow has a Table property.

          xacc.ide - now with TabsToSpaces support
          IronScheme - 1.0 alpha 4a out now (29 May 2008)

          J Offline
          J Offline
          JoeRip
          wrote on last edited by
          #4

          Thanks for the help. Of course, I figured that out about 5 seconds after clicking "Post" :-) I always assume the more complex answer first (I was starting to research "parent containers" when I noticed Table sitting there...)

          1 Reply Last reply
          0
          • J J4amieC

            JoeRip wrote:

            Now, the last line is going to fail, because dt.Columns[dc2].AllowDBNull is false.

            :confused: No it wont, you're setting the value to an empty string not DbNull.Value. Anyway, you should be able to go back to the table/columns from the datarow. dr.Table.Columns so you could do if(dr.Table.Columns["Second_Column"].AllowDBNull) { ... }

            J Offline
            J Offline
            JoeRip
            wrote on last edited by
            #5

            Meanwhile, I'm stuck on the fact that I can't test the passed in DataRow to see if it's null or not.. given

            public bool FillOutMyRow(out DataRow dr)
            {
            if (dr == null)
            {
            return false;
            }
            // fill out the data
            dr["first_column"] = 1;
            return true;
            }

            The compiler is complaining: "use of unassigned parameter dr" What's the problem here?

            L J 2 Replies Last reply
            0
            • J JoeRip

              Meanwhile, I'm stuck on the fact that I can't test the passed in DataRow to see if it's null or not.. given

              public bool FillOutMyRow(out DataRow dr)
              {
              if (dr == null)
              {
              return false;
              }
              // fill out the data
              dr["first_column"] = 1;
              return true;
              }

              The compiler is complaining: "use of unassigned parameter dr" What's the problem here?

              L Offline
              L Offline
              leppie
              wrote on last edited by
              #6

              Use ref, not out.

              xacc.ide - now with TabsToSpaces support
              IronScheme - 1.0 alpha 4a out now (29 May 2008)

              J 1 Reply Last reply
              0
              • L leppie

                Use ref, not out.

                xacc.ide - now with TabsToSpaces support
                IronScheme - 1.0 alpha 4a out now (29 May 2008)

                J Offline
                J Offline
                J4amieC
                wrote on last edited by
                #7

                Do you actually need ref? DataRow being a class isnt it passed in by reference in any case?

                1 Reply Last reply
                0
                • J JoeRip

                  Meanwhile, I'm stuck on the fact that I can't test the passed in DataRow to see if it's null or not.. given

                  public bool FillOutMyRow(out DataRow dr)
                  {
                  if (dr == null)
                  {
                  return false;
                  }
                  // fill out the data
                  dr["first_column"] = 1;
                  return true;
                  }

                  The compiler is complaining: "use of unassigned parameter dr" What's the problem here?

                  J Offline
                  J Offline
                  J4amieC
                  wrote on last edited by
                  #8

                  Why have you changed the param to an out? That wasn't there in your original post!

                  J 1 Reply Last reply
                  0
                  • J J4amieC

                    Why have you changed the param to an out? That wasn't there in your original post!

                    J Offline
                    J Offline
                    JoeRip
                    wrote on last edited by
                    #9

                    That's true, it wasn't. My original post was a quick and dirty bit done from memory, and therefore incomplete. Are you saying that ref or out is not required here at all? I can pass a DataRow and modify it to my heart's content? I did test out "ref" when "out" failed, and that does work. Still not sure why "out" doesn't work in the "if (dr == null) case, but it's not clear to me what "unassigned" means here when the caller has already assigned a value to the variable...

                    J 1 Reply Last reply
                    0
                    • J JoeRip

                      That's true, it wasn't. My original post was a quick and dirty bit done from memory, and therefore incomplete. Are you saying that ref or out is not required here at all? I can pass a DataRow and modify it to my heart's content? I did test out "ref" when "out" failed, and that does work. Still not sure why "out" doesn't work in the "if (dr == null) case, but it's not clear to me what "unassigned" means here when the caller has already assigned a value to the variable...

                      J Offline
                      J Offline
                      J4amieC
                      wrote on last edited by
                      #10

                      the ref keyword forces the argument to be passed by reference, it has no effect on reference types such as DataRow, but consider the following example with a value type:

                      static void Main()
                      {
                      int i = 1;
                      NoRef(i);
                      Console.WriteLine(i); // prints '1'

                      WithRef(ref i);
                      Console.WriteLine(i); // prints '2'
                      }

                      static void NoRef(int i)
                      {
                      i = i +1;
                      }

                      static void WithRef(ref int i)
                      {
                      i = i + 1;
                      }

                      as for the out keyword, this means that the method itself has to set the value of the param before using it. Hence why you cant check it for null before initializing it to some value.

                      static void Main()
                      {
                      int i = 0;
                      SetOut(out i);
                      Console.WriteLine(i); // prints '1'
                      }

                      static vouid SetOut(out int i)
                      {
                      i = 1;
                      }

                      So to cut a long story short, just remove the out/ref keyword altogether from your method. You can still change the values of your DataRow within your method.

                      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