Search Results

Search found 9 results on 1 pages for 'datarelation'.

Page 1/1 | 1 

  • DataRelation Insert and ForeignKey

    - by Steve
    Guys, I have a winforms application with two DataGridViews displaying a master-detail relationship from my Person and Address tables. Person table has a PersonID field that is auto-incrementing primary key. Address has a PersonID field that is the FK. I fill my DataTables with DataAdapter and set Person.PersonID column's AutoIncrement=true and AutoIncrementStep=-1. I can insert records in the Person DataTable from the DataGridView. The PersonID column displays unique negative values for PersonID. I update the database by calling DataAdapter.Update(PersonTable) and the negative PersonIDs are converted to positive unique values automatically by SQL Server. Here's the rub. The Address DataGridView show the address table which has a DataRelation to Person by PersonID. Inserted Person records have the temporary negative PersonID. I can now insert records into Address via DataGridView and Address.PersonID is set to the negative value from the DataRelation mapping. I call Adapter.Update(AddressTable) and the negative PersonIDs go into the Address table breaking the relationship. How do you guys handle primary/foreign keys using DataTables and master-detail DataGridViews? Thanks! Steve EDIT: After more googling, I found that SqlDataAdapter.RowUpdated event gives me what I need. I create a new command to query the last id inserted by using @@IDENTITY. It works pretty well. The DataRelation updates the Address.PersonID field for me so it's required to Update the Person table first then update the Address table. All the new records insert properly with correct ids in place! Adapter = new SqlDataAdapter(cmd); Adapter.RowUpdated += (s, e) => { if (e.StatementType != StatementType.Insert) return; //set the id for the inserted record SqlCommand c = e.Command.Connection.CreateCommand(); c.CommandText = "select @@IDENTITY id"; e.Row[0] = Convert.ToInt32( c.ExecuteScalar() ); }; Adapter.Fill(this); SqlCommandBuilder sb = new SqlCommandBuilder(Adapter); sb.GetDeleteCommand(); sb.GetUpdateCommand(); sb.GetInsertCommand(); this.Columns[0].AutoIncrement = true; this.Columns[0].AutoIncrementSeed = -1; this.Columns[0].AutoIncrementStep = -1;

    Read the article

  • is it possible to add DataRelation to DataSet if child table contains rows that have no parent in pa

    - by matti
    If I fill the DataSet with DataAdapters that select all rows from Orders and Customers and call: private void CreateRelation() { // Get the DataColumn objects from two DataTable objects // in a DataSet. Code to get the DataSet not shown here. DataColumn parentColumn = DataSet1.Tables["Customers"].Columns["CustID"]; DataColumn childColumn = DataSet1.Tables["Orders"].Columns["CustID"]; // Create DataRelation. DataRelation relCustOrder; relCustOrder = new DataRelation("CustomersOrders", parentColumn, childColumn); // Add the relation to the DataSet. DataSet1.Relations.Add(relCustOrder); } (from http://msdn.microsoft.com/en-us/library/system.data.datarelation.aspx) there will be a runtime error if there is orders that do not have customers. This might happen when a buggy program has not deleted customer's orders when customer was deleted. What can I do except put Orders select string a additional where-condition: CUSTID IN (SELECT DISTINCT CUSTID FROM CUSTOMERS) OR: is it really that way (that all children have to have parents)? My code might have a bug also. The exception occurs when IN MY CODE I add the relation to filled DataSet. The exception is: An unhandled exception of type 'System.ArgumentException' occurred in System.Data.dll Additional information: This constraint cannot be enabled as not all values have corresponding parent values. Thanks & Best Regards - Matti

    Read the article

  • Master / Detail datagridview with relation from type string in C#

    - by Daniel
    Hello, I want to show a master / detail relationship using two datagridviews and DataRelation in C#. The relation between the master and the detail table is an ID from type string (and there is no chance to change the ID to type integer). It seems like the DataGridView is not able to update the detail view when changing the row in the master table. Does anybody know if it is possible to achieve a master / detail view using a string ID and if yes, how? Or do I have to use an external DataGrid from another company? Thanks for any help Daniel

    Read the article

  • Dataset holds a table called "Table", not the table I pass in?

    - by dotnetdev
    Hi, I have the code below: string SQL = "select * from " + TableName; using (DS = new DataSet()) using (SqlDataAdapter adapter = new SqlDataAdapter()) using (SqlConnection sqlconn = new SqlConnection(connectionStringBuilder.ToString())) using (SqlCommand objCommand = new SqlCommand(SQL, sqlconn)) { sqlconn.Open(); adapter.SelectCommand = objCommand; adapter.Fill(DS); } System.Windows.Forms.MessageBox.Show(DS.Tables[0].TableName); return DS; However, every time I run this code, the dataset (DS) is filled with one table called "Table". It does not represent the table name I pass in as the parameter TableName and this parameter does not get mutated so I don't know where the name Table comes from. I'd expect the table to be the same as the tableName parameter I pass in? Any idea why this is not so? EDIT: Important fact: This code needs to return a dataset because I use the dataRelation object in another method, which is dependent on this, and without using a dataset, that method throws an exception. The code for that method is: DataRelation PartToIntersection = new DataRelation("XYZ", this.LoadDataToTable(tableName).Tables[tableName].Columns[0], // Treating the PartStat table as the parent - .N this.LoadDataToTable("PartProducts").Tables["PartProducts"].Columns[0]); // 1 // PartsProducts (intersection) to ProductMaterial DataRelation ProductMaterialToIntersection = new DataRelation("", ds.Tables["ProductMaterial"].Columns[0], ds.Tables["PartsProducts"].Columns[1]); Thanks

    Read the article

  • InvalidArgument=Value of '3' is not valid for 'rowIndex'

    - by Devendra Dwivedi
    Hi all This is my code It is working for if 1 terminal that is having 3 services but it is not working for more than 3 services when I do then I have got following error message: InvalidArgument=Value of '3' is not valid for 'rowIndex' I have so tired to find this problem but couldn't get any solutions. Anybody please help me. MySqlCommand command = new MySqlCommand("VTerminalsLoad");//Procedure MySqlDataAdapter terminalAdapter = this.Database.ExecuteCommand(command); terminalAdapter.Fill(dataSet, "Terminals"); command = new MySqlCommand("VTServicesLoad");//Procedure command.Parameters.Add(new MySqlParameter("pVesselID", 1)); MySqlDataAdapter serviceAdapter = this.Database.ExecuteCommand(command);//Return Adaptor serviceAdapter.Fill(dataSet, "Services"); DataColumn[] parentColumns = { dataSet.Tables[0].Columns["SerialNo"], dataSet.Tables[0].Columns["VesselID"], dataSet.Tables[0].Columns["TerminalID"] }; DataColumn[] childColumns = { dataSet.Tables[1].Columns["SerialNo"], dataSet.Tables[1].Columns["VesselID"], dataSet.Tables[1].Columns["TerminalID"] }; DataRelation relationTS = new DataRelation("TerminalsServices", parentColumns, childColumns); dataSet.Relations.Add(relationTS); //Parent Table ListTerminal.DataSource = dataSet; //ListTerminal Parent datagridview ListTerminal.DataMember = "Terminals"; //Child Table ListServices.DataSource = dataSet;// ListServices Child datagridview ListServices.DataMember = "Terminals.TerminalsServices";

    Read the article

  • C# winforms: DataSet with multiple levels of related tables

    - by Jake
    Hi, I am trying to use DataSet and DataAdapter to "filter" and "navigate" DataRows in DataTables. THE SITUATION: I have multiple logical objects, e.g. Car, Door and Hinge. I am loading a Form which will display complete Car information including each Door and their respective Hinges. In this senario, The form should display info for 1 car, 4 doors and 2 hinges for each door. Is it possible to use a SINGLE DataSet to navigate this Data? i.e. 1 DataRow in car_table, 4 DataRow in door_table and 8 DataRow in hinge_table, and still be able to navigate correctly between the different object and their relations? AND, able to DataAdapter.Update() easily? I have read about DataRelation but don't really understand how to use it. Not sure if it is the correct direction for my problem. Appreciate any advise. Thanks!

    Read the article

  • Making a many-to-many relationship using DataRelations object

    - by dotnetdev
    Hi, I have about 200 tables which need to relate to another table in a many-to-many fashion. I have the tables (including the intersection table) ready IN SQL Server. How can I write code using the data relation object to make the relationship? The tables are: PartStatPartName PartsMaterialsIntersection << Materials The materials table needs to have a foreign key from the PartStatPartName table. I tried various approaches using the DataRelation class but the change did not sync to SQL Server, despite being connected and adding the relation, and then calling AcceptChanges() on the dataset. Any guidance much appreciated. I have seen some threads covering the same problem but need an example in code so I can follow the right method. Thanks

    Read the article

  • What is the Best way to databind an ASP.NET TreeView for table with many to many parent child relati

    - by Matt W
    I've got a table which has the usual ParentID, ChildID as it's first two columns in a self-referencing tree data structure. My issue is that when I pull this out and use the following code: DataSet set = DA.GetNewCategories(); set.Relations.Add( new DataRelation("parentChildCategories", set.Tables[0].Columns["CategoryParentID"], set.Tables[0].Columns["CategoryID"]) ); StringBuilder buildXml = new StringBuilder(); StringWriter writer = new StringWriter(buildXml); set.WriteXml(writer); TreeView2.DataSource = new HierarchicalDataSet(set, "CategoryID", "CategoryParentID"); TreeView2.DataBind(); I get the error: These columns don't currently have unique values I believe this is because my data has children with multiple parent nodes. This is fine for my application - I don't mind if one row of data is rendered in multiple nodes of my TreeView. Could someone shed light on this please? It doesn't seem unreasonable to have a DataSet render XML which has nodes appearing in multiple places, but I can't figure out how to do it. Thanks, Matt.

    Read the article

  • Why is a DataViewManager not sorting?

    - by Alarion
    First off, a quick rundown of what the code is doing. I have two tables: Companies and Clients. There is a one to many relationship between Companies and Clients (one company can have many clients). In some processing logic, I have both tables loaded into a DataSet - each are DataTables. I have added a DataRelation to set the relationship, and it works when I use GetChildRows on a record from the Companies table. However, I need to sort the records returned. After googling around some it looks like the DataViewManager is the way to go, and I have examined several basic examples. However, I cannot get my rows sorted. Am I missing something, or am I not using this how it is supposed to be used? Example code: Dim ldvmManager As New DataViewManager(mdsData) ldvmManager.DataViewSettings("Companies").Sort = "comName ASC" ldvmManager.DataViewSettings("Clients").ApplyDefaultSort = False ldvmManager.DataViewSettings("Clients").Sort = "entLastName ASC, entFirstName ASC" For Each mdrCurrentCompany in ldvmManager.DataViewSettings("Companies").Table.Rows Dim ldrClients() as DataRow = mdrCurrentCompany.GetChildRows("CompaniesClients") Next No matter what I do, the first Client returned has a last name that starts with a 'B' and the second has one that starts with a 'A'. From there, the order is all mixed up. If I use my entire data instead of the subset I was using to test with, the first Client returned has a last name that starts with 'J'. It seems like it is still using whatever default sort was being used before I tried to use the DataViewManager. Any ideas?

    Read the article

1