Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

primary keys

Status
Not open for further replies.

chronics

Programmer
Sep 10, 2006
35
GB
When i use the code below to add a datarelation to 2 tables named 'Category' and 'Product' and then try to find a row within the tables using the find method shown below I get the error 'Table doesnt have a Primary Key'.


DataRelation catAndProdDataRelation = new DataRelation("CategoryAndProductDataRelation", ds.Tables["Category"].Columns[0]
, ds.Tables["Product"].Columns[3], true);

ds.Relations.Add(catAndProdDataRelation);


DataRow cdr = ds.Tables["Category"].Rows.Find(id);

I have also tried adding the primary keys explicitly with
ds.Tables["Category"].Constraints.Add("PK_ColumnID", ds.Tables["Category"].Columns[0], true);
ds.Tables["Product"].Constraints.Add("PK_ProductID", ds.Tables["Product"].Columns[0], true);

I get the error constraint matches constraint 1 already in collection


I thought the datarelation created the primary keys and the constraint matches error seems to indicate that.
Any help much appreciated
 
You need to add the primary keys before adding the data releation.

My sample code follows:

Code:
            // Create the dataset
            DataSet ds = new DataSet();

            // Add the two tables
            DataTable categoryTable = ds.Tables.Add("Category");
            DataTable productTable = ds.Tables.Add("Product");

            // Set up the columns
            categoryTable.Columns.Add("id", typeof(int));
            categoryTable.Columns.Add("text", typeof(string));
            productTable.Columns.Add("id", typeof(int));
            productTable.Columns.Add("text", typeof(string));
            productTable.Columns.Add("dummy", typeof(string));
            productTable.Columns.Add("categoryId", typeof(int));

            // Add some data
            categoryTable.Rows.Add(1, "Category 1");
            categoryTable.Rows.Add(2, "Category 2");
            productTable.Rows.Add(1, "1st Protuct of Category 1", "", 1);
            productTable.Rows.Add(2, "2nd Protuct of Category 1", "", 1);
            productTable.Rows.Add(3, "1st Protuct of Category 2", "", 2);

            // Add primary Keys
            ds.Tables["Category"].Constraints.Add("PK_ColumnID", ds.Tables["Category"].Columns[0], true);
            ds.Tables["Product"].Constraints.Add("PK_ProductID", ds.Tables["Product"].Columns[0], true);

            // Add the relationship
            DataRelation catAndProdDataRelation = 
                new DataRelation("CategoryAndProductDataRelation", 
                ds.Tables["Category"].Columns[0], 
                ds.Tables["Product"].Columns[3], 
                true);
            ds.Relations.Add(catAndProdDataRelation);            

            // Run the find
            int id = 1;
            DataRow cdr = ds.Tables["Category"].Rows.Find(id);

            // Display the result
            textBox1.Text = cdr["text"].ToString();
 
thanks aptitude putting the primary key declaration before the relationship declaration solved the problem
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top