lundi 31 mars 2014

Constraint error in adding Multi rows to master/detail dataset


Vote count:

0




I have 2 dataGridViews bound to a master/detail relationship. When I try to add a second row to the master dataGridView I get the following error.


system.data.constraintexception column "Project Customer UBF Id" is constrained to be unique. Value '' is already present Data relation.


I can add multi rows to the child DataGridView, and if I remove the DataRelation between the tables I can add multi rows to the master.


The tables are set up with autoincrement primary keys in SQL server.


Any help in overcoming this error would be appreciated



private void getData()
{
try
{
conn = new SqlConnection(connstr);
conn.Open();

// Create a DataSet.
data = new DataSet();
data.Locale = System.Globalization.CultureInfo.InvariantCulture;

string sqlStr = "SELECT [Project Customer UBF].* FROM [Project Customer UBF]; "; //WHERE [Project Customer UBF].[Project Id] = " +_projectID;

// Add data from the Customers table to the DataSet.
masterDataAdapter = new
SqlDataAdapter(sqlStr, conn);

masterDataAdapter.Fill(data, "[Project Customer UBF]");

// Add data from the Orders table to the DataSet.
detailsDataAdapter = new
SqlDataAdapter("SELECT [Project Customer Discount].* FROM [Project Customer Discount]", conn);
detailsDataAdapter.Fill(data, "[Project Customer Discount]");

// Establish a relationship between the two tables.
DataRelation relation = new DataRelation("CustDist",
data.Tables["[Project Customer UBF]"].Columns["Project Customer UBF Id"],
data.Tables["[Project Customer Discount]"].Columns["Project Customer UBF Id"]);
data.Relations.Add(relation);

// Bind the master data connector to the Customers table.
masterBindingSource.DataSource = data;
masterBindingSource.DataMember = "[Project Customer UBF]";
masterBindingSource.Filter = "[Project Id] =" + _projectID;

// 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 = "CustDist";
conn.Close();
}
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.");
}
}

private void ProjectEdit_Load(object sender, EventArgs e)
{
dataGridView1.DataSource = masterBindingSource;
dataGridView2.DataSource = detailsBindingSource;
getData();
}


asked 2 mins ago






Aucun commentaire:

Enregistrer un commentaire