Automaticaly create an ID for a datarow for a DataSet

Last post 06-02-2008 1:37 PM by PeterWellington. 6 replies.

Sort Posts:

  • Automaticaly create an ID for a datarow for a DataSet

    05-31-2008, 2:13 PM
    • Member
      50 point Member
    • Jelmer850i
    • Member since 03-17-2005, 3:07 AM
    • Posts 735

    I've got a dataset. and try to add a new row to it.
    I do it on this way:

    DataSet objData = new DataSet();
    objData.ReadXml(Server.MapPath("xml file"));

    DataRow dr = objData.Tables[tabel].NewRow();

    dr[0] = id.text;
    dr[1] = description.text;

    objData.Tables[tablename].Rows.Add(dr);

    But row should be a automatically id.
    Right now i need to add it by myself.

    What is the best way to create a ID for it ?
    Normally its just 1, 2 ,3 , 4, 5 ........
    So just nummeric numbers.. if i could get a max(id) of the table i could create 1 by myself ?

  • Re: Automaticaly create an ID for a datarow for a DataSet

    06-01-2008, 4:42 AM

    You can do that but the easiest way is to set the AutoIncrement property to true for your key DataColumn (you may also want to use the AutoIncrementSeed property).

    PW

    http://www.autosql.net

  • Re: Automaticaly create an ID for a datarow for a DataSet

    06-01-2008, 6:24 AM
    • Member
      50 point Member
    • Jelmer850i
    • Member since 03-17-2005, 3:07 AM
    • Posts 735

    I like to add something to my XML file.
    I do it on this way;

    string tabel = "item";
    
    DataSet objData = new DataSet();
    objData.ReadXml(Server.MapPath("item.xml"));
    
                    DataRow dr = objData.Tables[tabel].NewRow();                
                    dr[1] = "test";
                    dr[2] = "another test";
                    objData.Tables[tabel].Rows.Add(dr);

    So i leave dr[0] (the id) empty, but its empty in my xml file..
    So what should i add here to create a new id?

    can i run a SQL query on a XML file ? so i can get the max(id) of it ?

  • Re: Automaticaly create an ID for a datarow for a DataSet

    06-01-2008, 10:36 AM
    • Member
      50 point Member
    • Jelmer850i
    • Member since 03-17-2005, 3:07 AM
    • Posts 735

     

    I made a function for it...


            protected int getMaxID(DataSet objData,string tabel)
            {      
                int max = 0;           
                foreach (DataRow dr in objData.Tables[tabel].Rows)  //table name
                {
                    if (Int32.Parse(dr.ItemArray[2].ToString()) > max)
                    {
                        max = Int32.Parse(dr.ItemArray[2].ToString());
                    }               
                }
                return max;
            }

  • Re: Automaticaly create an ID for a datarow for a DataSet

    06-01-2008, 9:33 PM

    You shouldn't have to do all of that.  Check that the xml file you're using to load the data set has AutoIncrement set to true for the column we're talking about.  If not, change it and set the AutoIncrementSeed property to one, then save to xml again.  When you add news rows, the column values should automatically increment for you, you don't have to set the value at all.

    PW

    http://www.autosql.net

  • Re: Automaticaly create an ID for a datarow for a DataSet

    06-02-2008, 4:10 AM
    • Member
      50 point Member
    • Jelmer850i
    • Member since 03-17-2005, 3:07 AM
    • Posts 735

     

    How? right now i use this function, it works great.

            protected int getMaxID(DataSet objData,string tabel)
            {      
                int max = 0;           
                foreach (DataRow dr in objData.Tables[tabel].Rows)  //table name
                {
                    if (Int32.Parse(dr.ItemArray[2].ToString()) > max)
                    {
                        max = Int32.Parse(dr.ItemArray[2].ToString());
                    }               
                }
                return max;
            }

     

    This is my xml file:

    <?xml version="1.0" standalone="yes"?>
    <items>
      <item id="3">
        <description>rr</description>
        <fileName>test.jpg</fileName>
      </item>
    </items>

  • Re: Automaticaly create an ID for a datarow for a DataSet

    06-02-2008, 1:37 PM
    Answer

    Once you load the xml file into a dataset, do this:

    DataColumn dc = yourDataSet.Tables["tablename"].Columns["columnname"];

    dc.AutoIncrement = true;

    dc.AutoIncrementSeed = maxId + 1;

    Then when you add new records, the id field will automatically be set for you.

Page 1 of 1 (7 items)