Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > DataGridView Update Problem with BL Layer
 

DataGridView Update Problem with BL Layer

Hi All,

This is a follow on from my troubles with implementing a 3-tier model with a DataGridView. I've got a grid that displays records from a Personnel table. The Data Access Layer (DAL) consists of a DataSet declared by connecting to an Access database. I have a Business Logic layer (BLL) that currently just exposes a getPersonnel() and an Update( PersonnelRow ) function; they just call the corresponding functions from the autogenerated DAL class.

Here's the BL code:

Code Snippet

using System;

using System.Collections.Generic;

using System.Text;

using System.ComponentModel;

using mTiers.DAL.SadAdminDataSetTableAdapters;

namespace mTiers.BLL

{

[DataObject]

class personnelBL

{

private static PersonnelTableAdapter _personnelAdapter = null;

private static PersonnelTableAdapter Adapter

{

get

{

if (_personnelAdapter == null)

_personnelAdapter = new PersonnelTableAdapter();

return _personnelAdapter;

}

}

public mTiers.DAL.SadAdminDataSet.PersonnelDataTable PersonnelData

{

get

{

return Adapter.GetData();

}

}

[DataObjectMethod(DataObjectMethodType.Select, true)]

public mTiers.DAL.SadAdminDataSet.PersonnelDataTable GetPersonnel()

{

return PersonnelData;

}

[DataObjectMethod(DataObjectMethodType.Update, true)]

public void UpdatePersonnel(mTiers.DAL.SadAdminDataSet.PersonnelRow updatedRow)

{

Adapter.Update(updatedRow);

}

}

}

And here's the form code:

Code Snippet

public partial class frmMain : Form

{

public frmMain()

{

InitializeComponent();

}

private BLL.personnelBL _bl = null;

private void frmMain_Load(object sender, EventArgs e)

{

_bl = new mTiers.BLL.personnelBL();

personnelDataBindingSource.DataSource = _bl.GetPersonnel();

}

private void btOK_Click(object sender, EventArgs e)

{

DAL.SadAdminDataSet.PersonnelDataTable personnelTable = personnelDataBindingSource.DataSource as

DAL.SadAdminDataSet.PersonnelDataTable;

if (personnelTable != null)

{

_bl.UpdatePersonnel(personnelTable);

}

this.Close();

}

}

Any ideas why the values from updated cells are not written back to the database or even the dataset? I assume it's because I need to do a commit somewhere but I've tried that in a number of places without sucess.

Thanks Andy

Andy Bath  Monday, January 28, 2008 1:25 PM

It seems to be a problem with the Access database connection. I've rewritten the code to connect to aSQL Server db and all is fine. Since this is my target database (was just using Access as I had it at home) and time is short, I won't be able to dig any deeper into this problem.

Thanks for your help,

Andy

Andy Bath  Wednesday, January 30, 2008 3:34 PM
My guess is because you bind DataGridView to result of method. and there is no way

save values back to BL. Try to expose a property or somethig like this

Code Snippet

object _personnel = null;

private void frmMain_Load(object sender, EventArgs e)

{

_bl = new mTiers.BLL.personnelBL();

_personnel = _bl.GetPersonnel();

personnelDataBindingSource.DataSource = _personnel;

}

//see if _personnel is updated....

hope this helps

Galin Iliev  Monday, January 28, 2008 2:10 PM

Thanks Galin, but that made no difference. I was a little wrong in my original posting; the changes are getting through to my BLL.Update() function (I can see the modified row within the dataset's personnel table), it's just the update function on the Adapter is doing nothing.

I'm now calling row.AcceptChanges() and row.EndEdit() within my BL update function but this also seems to have no effect. My latest update function looks like this:

Code Snippet

if (row.RowState == System.Data.DataRowState.Modified)

{

try

{

row.AcceptChanges();

row.EndEdit();

Adapter.Update(row);

}

catch (Exception e)

{

System.Console.WriteLine("Update : " + e.Message);

}

}

Thanks Andy

Andy Bath  Monday, January 28, 2008 4:28 PM

Hi Andy,

Make sure you do not call AcceptChanges() method as it marks all rows with status Unchanged and hey wont be got by Update() method of data adapter

you can try to call DataSet.GetChanges()to get only modified rrecords in separate DataSet instance
Galin Iliev  Monday, January 28, 2008 4:32 PM

Thanks Galin,

That's a couple of good points; the code now looks like this:

Code Snippet

[DataObjectMethod(DataObjectMethodType.Update, false)]

public void UpdatePersonnel(mTiers.DAL.SadAdminDataSet.PersonnelDataTable updatedTable )

{

if (updatedTable.GetChanges() != null && updatedTable.GetChanges().Rows.Count > 0)

{

foreach (DAL.SadAdminDataSet.PersonnelRow row in updatedTable.GetChanges().Rows)

{

try

{

row.EndEdit();

Adapter.Update(row);

}

catch (Exception e)

{

System.Console.WriteLine("Update : " + e.Message);

}

}

}

}

but the updates are still not getting through to my Access DB. Is there a way of detecting if the SQL is being generated properly/at all?

Thanks Andy

Andy Bath  Monday, January 28, 2008 5:02 PM

why don;t use use just a simple adapter.Update() call?

Code Snippet

[DataObjectMethod(DataObjectMethodType.Update, false)]

public void UpdatePersonnel(mTiers.DAL.SadAdminDataSet.PersonnelDataTable updatedTable )

{

DataTable chnages = updatedTable.GetChanges();

if (chnages)

{

try

{

Adapter.Update(chnages);

}

catch (Exception e)

{

System.Console.WriteLine("Update : " + e.Message);

}

}

}

Galin Iliev  Monday, January 28, 2008 8:58 PM
Did you check the value of RowState property? My guess it that it's Unchanged and then the Adapter.Update method won't update the database
MarceloRos  Tuesday, January 29, 2008 4:19 PM
Sorry, I'm not sure my comment was right after you deleted the call to AcceptChanges method. I had a similar problem but I used to get en error in the Update line until I added the New ...CommandBuilder(adapter) after creating the adapter so the UPDATE command is automatically generated. See the "Automatically Generating Commands" in the help for the explanation.

Marcelo
MarceloRos  Tuesday, January 29, 2008 5:00 PM

It seems to be a problem with the Access database connection. I've rewritten the code to connect to aSQL Server db and all is fine. Since this is my target database (was just using Access as I had it at home) and time is short, I won't be able to dig any deeper into this problem.

Thanks for your help,

Andy

Andy Bath  Wednesday, January 30, 2008 3:34 PM

You can use google to search for other answers

Custom Search

More Threads

• How to Wrap column header text in DataGrid ?
• Implements IBindingListView Problem
• New object datasource designer
• Modifying BidingSource
• using datagridview in .net 1.1
• Password disappearing in connection string?
• Installing prerequisites (windows application vs2005)
• MVC databinding
• OpenFileDialog
• DataGridView selecting extra rows when code placed in SelectionChanged.