jlrray
asked on
Creating a Master - Detail / Parent - Child Form with C# / SSE 2005
I've been struggling piecing together tutorial from tutorial and ultimately I would just like to create a master-detail form in C#. In MS Access, it used to be easy to create a subform and have the detail correspond to the master data. I have a main table that is filled with user information tblUser and other tables with corresponding data such as tblTrainingClasses & tblCertifications. How can I link the tblTraining classes & tblCertifications tables utilizing a datagridview , to the main tblUsers table? I have created all the objects on a form already. I also would like the datagridview to be editable.
ASKER
Let me try and decipher that and see if I can get it to work with my app.
ASKER
I got the master-detail form to work, however, I am unable to update an existing row in my child table. I used the designer to add 3 textbox's from tblPersonnel and 1 datagridview from tblCertRecords. I do not know how to do this all manually, so i've been using the designer for most of it. The error message I get is this:
Update requires a valid UpdateCommand when passed DataRow collection with modified rows.
Here's my existing code:
private void Form1_Load(object sender, EventArgs e)
{
this.tblPersonnelTableAdap ter.Fill(t his.cacheP ROPEDDataS et.tblPers onnel);
this.tblCertRecordsTableAd apter.Fill (this.cach ePROPEDDat aSet.tblCe rtRecords) ;
}
private void tblPersonnelBindingNavigat orSaveItem _Click(obj ect sender, EventArgs e)
{
this.Validate();
this.tblPersonnelBindingSo urce.EndEd it();
this.tblPersonnelTableAdap ter.Update (this.cach ePROPEDDat aSet.tblPe rsonnel);
try
{
// Delete records in the child table
this.tblCertRecordsTableAd apter.Upda te(this.ca chePROPEDD ataSet.tbl CertRecord s.Select(" ", "", DataViewRowState.Deleted)) ;
//Handle ADD, UPDATE, and DELETE in parent table
this.tblPersonnelTableAdap ter.Update (this.cach ePROPEDDat aSet.tblPe rsonnel.Se lect("", "",
DataViewRowState.Added |
DataViewRowState.ModifiedC urrent |
DataViewRowState.Deleted)) ;
//Handle ADD & UPDATE in child table
this.tblCertRecordsTableAd apter.Upda te(this.ca chePROPEDD ataSet.tbl CertRecord s.Select(" ","",
DataViewRowState.Added |
DataViewRowState.ModifiedC urrent));
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message );
throw;
}
}
Can someone please help me update my records?
Update requires a valid UpdateCommand when passed DataRow collection with modified rows.
Here's my existing code:
private void Form1_Load(object sender, EventArgs e)
{
this.tblPersonnelTableAdap
this.tblCertRecordsTableAd
}
private void tblPersonnelBindingNavigat
{
this.Validate();
this.tblPersonnelBindingSo
this.tblPersonnelTableAdap
try
{
// Delete records in the child table
this.tblCertRecordsTableAd
//Handle ADD, UPDATE, and DELETE in parent table
this.tblPersonnelTableAdap
DataViewRowState.Added |
DataViewRowState.ModifiedC
DataViewRowState.Deleted))
//Handle ADD & UPDATE in child table
this.tblCertRecordsTableAd
DataViewRowState.Added |
DataViewRowState.ModifiedC
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message
throw;
}
}
Can someone please help me update my records?
Tackling the errors one by one usually result in a good outcome.
In the designer can you create an UpdateCommand method using the properties of the datagridview?
If so move the process handling into that area.
In the designer can you create an UpdateCommand method using the properties of the datagridview?
If so move the process handling into that area.
ASKER
I'm not sure what you mean. I've pretty much isolated the error to the last section //Handle ADD & UPDATE in the child table. I am able to add / delete any item in the child or parent table. I just cant update and existing item. I've stepped through the process and basically it catches at the last line.
ASKER
Here is my progress so far. I am finally able to update my detail table. Yay. Although this part was successful, it opened up another can of worms. The good thing is, I'm starting to see how this whole process works. Here's what I did so far.
My original error message I received said:
Update requires a valid UpdateCommand when passed DataRow collection with modified rows.
I did a google search and came upon this post in the MSDN forums.
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=261817&SiteID=1
create a method for update.
Goto Data->show data sources, Right click on the table you are having probs with and edit dataset with designer. Right click on the table adapter and choose properties, expand Data->updateCommand. Choose new, expand updatecommand '+'. Go to command text and add table. Select the fields you reuire you will end up wilth a query similar:
UPDATE TBLCustomer
SET name =, address =, phone =
change to
UPDATE TBLCustomer
SET name =?, address =?, phone =?
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- --
So it saved! But here's the problem. Now, when I update a record, when I close the application and restart there are 5 additional records + the 1 existing record of exactly the same data . My immediate guess is that it corresponds to 1 record for each of the existing fields or columns that are in that table. I had tried adding the '?' for the SET locations. I even tried adding @fieldname to them which gave me the same result.
When I looked at the UpdateCommand properties of the original TableAdapter, there was a lot more code in there which, although I'm not sure what it means, it appears that it took into consideration, nulls and other parameters. I'm going to attempt to copy much of the format from the original tableadapter properties and put them into this new tableadapter.
Just to follow: Here's my code so far:
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'cachePROPEDDataSet.tblCer tification s' table. You can move, or remove it, as needed.
this.tblCertificationsTabl eAdapter.F ill(this.c achePROPED DataSet.tb lCertifica tions);
//Reverse the order. Make sure tblPersonnel comes before tblCert
// TODO: This line of code loads data into the 'cachePROPEDDataSet.tblPer sonnel' table. You can move, or remove it, as needed.
this.tblPersonnelTableAdap ter.Fill(t his.cacheP ROPEDDataS et.tblPers onnel);
// TODO: This line of code loads data into the 'cachePROPEDDataSet.tblCer tRecords' table. You can move, or remove it, as needed.
this.tblCertRecordsTableAd apter.Fill (this.cach ePROPEDDat aSet.tblCe rtRecords) ;
}
private void tblPersonnelBindingNavigat orSaveItem _Click(obj ect sender, EventArgs e)
{
if (this.Validate())
{
this.tblPersonnelBindingSo urce.EndEd it();
//this.tblPersonnelTableAd apter.Upda te(this.ca chePROPEDD ataSet.tbl Personnel) ;
this.tblCertRecordsBinding Source.End Edit();
//this.tblCertRecordsTable Adapter.Up date(this. cachePROPE DDataSet.t blCertReco rds);
try
{
// DELETE records in the child table
this.tblCertRecordsTableAd apter.Upda te(this.ca chePROPEDD ataSet.tbl CertRecord s.Select(" ", "",
DataViewRowState.Deleted)) ;
//Handle ADD, UPDATE, and DELETE in parent table
this.tblPersonnelTableAdap ter.Update (this.cach ePROPEDDat aSet.tblPe rsonnel.Se lect("", "",
DataViewRowState.Added |
DataViewRowState.ModifiedC urrent |
DataViewRowState.Deleted)) ;
//Handle ADD & UPDATE in child table
this.tblCertRecordsTableAd apter.Upda te(this.ca chePROPEDD ataSet.tbl CertRecord s.Select(" ", "",
DataViewRowState.Added |
DataViewRowState.ModifiedC urrent));
//this.Validate();
//this.tblCertificationsBi ndingSourc e.EndEdit( );
//this.tblCertificationsTa bleAdapter .Update(th is.cachePR OPEDDataSe t.tblCerti fications) ;
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message );
//throw;
}
}
}
Joe
My original error message I received said:
Update requires a valid UpdateCommand when passed DataRow collection with modified rows.
I did a google search and came upon this post in the MSDN forums.
--------------------------
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=261817&SiteID=1
create a method for update.
Goto Data->show data sources, Right click on the table you are having probs with and edit dataset with designer. Right click on the table adapter and choose properties, expand Data->updateCommand. Choose new, expand updatecommand '+'. Go to command text and add table. Select the fields you reuire you will end up wilth a query similar:
UPDATE TBLCustomer
SET name =, address =, phone =
change to
UPDATE TBLCustomer
SET name =?, address =?, phone =?
--------------------------
So it saved! But here's the problem. Now, when I update a record, when I close the application and restart there are 5 additional records + the 1 existing record of exactly the same data . My immediate guess is that it corresponds to 1 record for each of the existing fields or columns that are in that table. I had tried adding the '?' for the SET locations. I even tried adding @fieldname to them which gave me the same result.
When I looked at the UpdateCommand properties of the original TableAdapter, there was a lot more code in there which, although I'm not sure what it means, it appears that it took into consideration, nulls and other parameters. I'm going to attempt to copy much of the format from the original tableadapter properties and put them into this new tableadapter.
Just to follow: Here's my code so far:
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'cachePROPEDDataSet.tblCer
this.tblCertificationsTabl
//Reverse the order. Make sure tblPersonnel comes before tblCert
// TODO: This line of code loads data into the 'cachePROPEDDataSet.tblPer
this.tblPersonnelTableAdap
// TODO: This line of code loads data into the 'cachePROPEDDataSet.tblCer
this.tblCertRecordsTableAd
}
private void tblPersonnelBindingNavigat
{
if (this.Validate())
{
this.tblPersonnelBindingSo
//this.tblPersonnelTableAd
this.tblCertRecordsBinding
//this.tblCertRecordsTable
try
{
// DELETE records in the child table
this.tblCertRecordsTableAd
DataViewRowState.Deleted))
//Handle ADD, UPDATE, and DELETE in parent table
this.tblPersonnelTableAdap
DataViewRowState.Added |
DataViewRowState.ModifiedC
DataViewRowState.Deleted))
//Handle ADD & UPDATE in child table
this.tblCertRecordsTableAd
DataViewRowState.Added |
DataViewRowState.ModifiedC
//this.Validate();
//this.tblCertificationsBi
//this.tblCertificationsTa
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message
//throw;
}
}
}
Joe
ASKER
Ok. I figured it out... Except I dont know exactly what went wrong. I can only speculate at this time that it was the data itself. The table I had attempted to update had many null fields. I still need to figure out why that triggers errors.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
DataSet ds = MyDatabase.GetCategoriesAn
//Bind the grid
dataGridView1.DataSource = ds.Tables["Products"];
//Remove the auto-generated CategoryID column
dataGridView1.Columns.Remo
//Create the detail Column for CategorieID
DataGridViewComboBoxColumn
detailCol.HeaderText = "Category";
//set the DisplayStyle to None, if you only want to have a lookup field
dgcb.DisplayStyle = DataGridViewComboBoxDispla
dgcb.ReadOnly = true;
//This column is bound to the Products.CategoryID field
detailCol.DataPropertyName
//The detail column is filled from the Categories table
detailCol.DefaultCellStyle
detailCol.DisplayMember = "CategoryName";
detailCol.ValueMember = "CategorieID";
//Finally add the column to the grid
dataGridView1.Columns.Add(
HTH
Tommie