?
Solved

dataset manipulation

Posted on 2008-02-11
11
Medium Priority
?
833 Views
Last Modified: 2012-08-13
i have one datatable in dataset .datatable has columns with data.i dont have any primary key column.i want to add one column in this datatable name as ID.which should get increament based on rows insertions in datatable.it shoud increment by one.and it should have unique value.like 1 ,2  3, so on.i want to do all this on dataset level only.kindly tell me how to do?
0
Comment
Question by:ajay1feb
  • 4
  • 4
  • 3
11 Comments
 
LVL 22

Expert Comment

by:p_davis
ID: 20866499
DataColumn[] pkCol = {dataSet.Tables["dataTableName"].Columns["columnName"]};

dataSet.Tables["dataTableName"].PrimaryKey = pkCol;
dataSet.Tables["dataTableName"].Columns["columnName"].AutoIncrement = true;
0
 
LVL 2

Expert Comment

by:patrickriva
ID: 20866523
This could do.....
DataSet ds = new DataSet();
			ds.Tables.Add(new DataTable());
 
			ds.Tables[0].Columns.Add(new DataColumn("ID", typeof (int)));
			for ( int i = 0; i < ds.Tables[0].Rows.Count; i++ )
				ds.Tables[0].Rows[i]["ID"] = i;

Open in new window

0
 

Author Comment

by:ajay1feb
ID: 20872716
dera patrickriva,

   solution is nice but one problem is here.if i have 10 rows in rowset then it is giving id like 0 to 9.its not starting from 1.its should start from 1 and it should end to 10 if rowset has 10 rows.waiting for ur reply.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 2

Expert Comment

by:patrickriva
ID: 20872953
just change the following line with a + 1

                             
ds.Tables[0].Rows[i]["ID"] = i + 1;

Open in new window

0
 

Author Comment

by:ajay1feb
ID: 20873289
dear patrickriva,

now   i want to make this ID column as primary key.
0
 
LVL 2

Expert Comment

by:patrickriva
ID: 20873330
here ...
ds.Tables[0].PrimaryKey = new DataColumn[] { ds.Tables[0].Columns["ID"] };

Open in new window

0
 

Author Comment

by:ajay1feb
ID: 20873376
it is not working.following message is displaying.
These columns don't currently have unique values.
now is like following code.now it is not showing any exception.
 strItems = "SELECT GRM.group,GRM.productline,IM.productType,IM.CatalogNumber,IM.Description,PR.RecommededUnits,CLP.CurrentListPrice from GroupLineMapping GRM,ItemMaster IM,  ProductLineConfiguration PR, ConsignmentListPrice CLP WHERE GRM.productline=PR.productline AND PR.CatalogNumber=IM.CatalogNumber AND  IM.CatalogNumber=CLP.CatalogNumber AND GRM.group='" + comboGroup.SelectedItem.Text + "'" + "AND PR.productline='" + comboPoductLine.SelectedItem.Text + "'";

        DataSet dsItems = new DataSet();
        dsItems = AccessDB.DBDataSet(strItems);
         
        dsItems.Tables.Add(new DataTable());
       
        dsItems.Tables[0].Columns.Add(new DataColumn("ItemID", typeof(int)));
        //dsItems.Tables[0].PrimaryKey = new DataColumn[] { dsItems.Tables[0].Columns["ItemID"] };
     

        for (int i = 0; i < dsItems.Tables[0].Rows.Count; i++)
            dsItems.Tables[0].Rows[i]["ItemID"] = i+1;
            GVQuote.DataSource = dsItems;
            dsItems.Tables[0].PrimaryKey = new DataColumn[] { dsItems.Tables[0].Columns["ItemID"] };





0
 
LVL 2

Accepted Solution

by:
patrickriva earned 2000 total points
ID: 20873460
Looking at your indentation I think you need to put the last two lines outside the for loop....
0
 
LVL 22

Expert Comment

by:p_davis
ID: 20874329
ajay1feb -- did you even try my solution suggestion. it uses the datasets native properties.-- also there is a autoincrement seed to start out with the id number that you want.

also i already told you how to add ad the column as the primary key
0
 

Author Comment

by:ajay1feb
ID: 20874344
dear davis,

   i tried yours suggestion but it didn't work in my scenario.but patrickriva's suggestion worked.
Regards,
Ajay
0
 
LVL 22

Expert Comment

by:p_davis
ID: 20874356
if you have time i am interested to know what, exactly, didn't work.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
SQL Database Recovery Software repairs the MDF & NDF Files, corrupted due to hardware related issues or software related errors. Provides preview of recovered database objects and allows saving in either MSSQL, CSV, HTML or XLS format. Ensures recov…
Hi, this video explains a free download that you can incorporate into your Access databases, or use stand-alone for contact management. Contacts -- Names, Addresses, Phone Numbers, eMail Addresses, Websites, Lists, Projects, Notes, Attachments…

593 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question