Results 1 to 9 of 9

Thread: [RESOLVED] Master/Detail Relation problems

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    683

    Resolved [RESOLVED] Master/Detail Relation problems

    I am trying to figure out bindingsources with master detail relationships. I have a database with a table of personnel. I have another database on the same server with claims by those personnel. I create a dataset and add both tables and then add a datarelation. I have tried it two different ways (one is commented out) and both give me the same error: This constraint cannot be enabled as not all values have corresponding parent values.

    The form has 2 dataviews on it. Here is the code:

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsApplication2
    {
        public partial class frm1 : Form
        {
            //private DataGridView masterDataGridView = new DataGridView();
            private BindingSource masterBindingSource = new BindingSource();
            //private DataGridView detailsDataGridView = new DataGridView();
            private BindingSource detailsBindingSource = new BindingSource();
    
            public frm1()
            {
                InitializeComponent();
            }
    
            private void frm1_Load(object sender, EventArgs e)
            {
                // Bind the DataGridView controls to the BindingSource
                // components and load the data from the database.
                masterDataGridView.DataSource = masterBindingSource;
                detailsDataGridView.DataSource = detailsBindingSource;
                GetData();
    
                // Resize the master DataGridView columns to fit the newly loaded data.
                masterDataGridView.AutoResizeColumns();
    
                // Configure the details DataGridView so that its columns automatically
                // adjust their widths when the data changes.
                detailsDataGridView.AutoSizeColumnsMode =
                    DataGridViewAutoSizeColumnsMode.AllCells;
            }
            
            private void GetData()
            {
                try
                {
                    // Connection strings. 
                   
                    String connectionString =
                        "Data Source=SCE-SCE-DSVR02D;Initial Catalog=TrainingManagementDB;Integrated Security=True";
                    String connectionString2 =
                        "Data Source=SCE-SCE-DSVR02D;Initial Catalog=DocCtrl;Integrated Security=True";
            
                    SqlConnection connection = new SqlConnection(connectionString);
                    SqlConnection connection2 = new SqlConnection(connectionString2);
                    // Create a DataSet.
                    DataSet data = new DataSet();
                    data.Locale = System.Globalization.CultureInfo.InvariantCulture;
    
                    // Add data from the Pers table to the DataSet.
                    SqlDataAdapter masterDataAdapter = new
                        SqlDataAdapter("SELECT Last_Name, Service_Number, First_Name, Initials, Rank, Status, COS_In, MOC, Squadron, Gender, Element FROM PersData ORDER BY Last_Name ", connection);
                    masterDataAdapter.Fill(data, "PersData");
    
                    // Add data from the Claims table to the DataSet.
                    SqlDataAdapter detailsDataAdapter = new
                        SqlDataAdapter("select * from Claims", connection2);
                    detailsDataAdapter.Fill(data, "Claims");
    
                    // Establish a relationship between the two tables.
                    DataRelation relation = new DataRelation("CustomersOrders",
                        data.Tables["PersData"].Columns["Service_Number"],
                        data.Tables["Claims"].Columns["SN"]);
                    data.Relations.Add(relation);
    
    
                    //DataColumn primarykey = data.Tables["PersData"].Columns["Service_Number"];
                    //DataColumn foreignkey = data.Tables["Claims"].Columns["SN"];
                    //DataRelation relation = data.Relations.Add(primarykey, foreignkey);
    
                    // Bind the master data connector to the Persdata table.
                    masterBindingSource.DataSource = data;
                    masterBindingSource.DataMember = "PersData";
    
                    // Bind the details data connector to the master data connector,
                    // using the DataRelation name to filter the information in the 
                    // details table based on the current row in the master table. 
                    detailsBindingSource.DataSource = masterBindingSource;
                    detailsBindingSource.DataMember = "CustomersOrders";
                }
                catch (SqlException)
                {
                    MessageBox.Show("To run this example, replace the value of the " +
                        "connectionString variable with a connection string that is " +
                        "valid for your system.");
                }
            }
    
        }
    }

  2. #2
    Fanatic Member BillBoeBaggins's Avatar
    Join Date
    Jan 2003
    Location
    in your database, dropping your tables.
    Posts
    628

    Re: Master/Detail Relation problems

    My first thought is either A)your data types are not compatible or b)you have nulls in one side of your link either SN or Service_Number contains a null.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    683

    Re: Master/Detail Relation problems

    Thats what I thought too, I checked and they are both 11 nvarchar (no nulls).

  4. #4
    Fanatic Member BillBoeBaggins's Avatar
    Join Date
    Jan 2003
    Location
    in your database, dropping your tables.
    Posts
    628

    Re: Master/Detail Relation problems


  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    683

    Re: Master/Detail Relation problems

    I just copied the Claims table into the other database and ran this query and got no returns

    Select SN from Claims where (NOT EXISTS(Select Service_Number from Persdata))

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Master/Detail Relation problems

    The obvious answer is that the child table contains records with no corresponding parent record in the other table. How would you go about finding such a record? Wouldn't you just loop through the child DataTable and then look for a corresponding record in the parent DataTable? Just don't add the DataRelation and you can fill the tables and then look.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    683

    Re: Master/Detail Relation problems

    Thats what I thought and why I wrote the select above. It returned no records so, I don;t think there is any extra records. Any other ideas?

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    683

    Re: Master/Detail Relation problems

    Fixed it. there was bad records but I needed to revise my sql to this:

    select * from Claims where not exists (Select * from Persdata where Persdata.Service_Number = Claims.SN)

  9. #9
    Fanatic Member BillBoeBaggins's Avatar
    Join Date
    Jan 2003
    Location
    in your database, dropping your tables.
    Posts
    628

    Re: [RESOLVED] Master/Detail Relation problems

    Cool. I like happy endings.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width