You must Sign In to post a response.
  • Category: ASP.NET

    How to add new rows and How to insert records in existing dataset

    Hi,

    How to add new rows and How to insert records in existing dataset.

    In this below example, I already fill the datas only first 2 rows(Id and Name) using dataset.
    Ex:
    Id Name
    1 xxx
    2 yyy
    3 zzz

    Now I want insert new row(Age) in the same dataset.
    Output Ex:
    Id Name Age(NewRow)
    1 xxx 10
    2 yyy 12
    3 zzz 13

    Regards
    Karthik
  • #756354
    Hi,

    First off all I want to tell you something that is not a Row, that is column. In your existing dataset you need to add one more column. Now, what help you are expecting from our end.

    If you are expecting how to add column to dataset then refer below sample.


    DataSet ds=new DataSet();

    ds.Tables[0].Columns.Add("NewCol");


    Try something like above to achieve your goal...

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #756356
    Hi,

    I have added new column as Age(NewRow) in my dataset.

    I want to insert values like,
    Id Name Age(NewRow)
    1 xxx 10
    2 yyy 12
    3 zzz 13

  • #756357
    Refer the below code for Inserting the new records in existing dataset

    DataRow nRow = DatasetName.DataTableName.NewRow();
    nRow.Id = 4;
    nRow.Name = "AAA";
    nRow.Age = 14;
    ExistingTable.Rows.Add(nRow);

    -----------------------
    DataTable dtTable = new DataTable("Emp");
    dtTable.Columns.Add("ID");
    dtTable.Columns.Add("Name");
    dtTable.Columns.Add("Age");
    dtTable.Rows.Add(1,"AAA", 1);
    dtTable.Rows.Add(2,"BBB", 2);
    DataSet dsSet = new DataSet();
    dsSet.Tables.Add(dtTable1);

  • #756359
    Hi,

    If you want to update the column in dataset then you can iterate each and every row and update the record.

    Refer below sample

    for(int i=0;i<ds.Tables[0].Rows.Count; i++)
    {
    ds.Tables[0].Rows[i]["Age"]=1+ds.Tables[0].Rows[i]["Id"].ToString();
    }
    ds.Tables[0].AcceptChanges();


    Try something like above,

    Hope this could be helpful to you...

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/


  • Sign In to post your comments