updating a DataGrid

J

Jon Agiato

Hi, I am trying to use a data grid in a web application in which I have
three tiers. The DataGrid is not set up to a data source, or a data adapter,
so everytime I make a change I send the cell information to another object
which forms an SQL query and makes the query to the DB (actually to a
DBFacade object). In any case, please see the code I have listed below for
my update DataGrid event. With this code, the DB is updated using the old
data in the datagrid prior to the users changes, why is this and how can I
take the new data out of the grid after the user makes changes? Thanks very
much!

private void coursesDataGrid_UpdateCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
// get primary key courseID
TableCell courseIDCell = e.Item.Cells[1];
string courseID = ((TextBox)courseIDCell.Controls[0]).Text;

// get section
TableCell sectionCell = e.Item.Cells[2];
string section = ((TextBox)sectionCell.Controls[0]).Text;

// get semester
TableCell semesterCell = e.Item.Cells[3];
string semester = ((TextBox)semesterCell.Controls[0]).Text;

// get title
TableCell titleCell = e.Item.Cells[4];
string title = ((TextBox)titleCell.Controls[0]).Text;

// get school name
TableCell schoolNameCell = e.Item.Cells[5];
string schoolName = ((TextBox)schoolNameCell.Controls[0]).Text;

// update database here
coursesDataGrid.DataSource = courseManager.UpdateCourse(user, schoolName,
semester, courseID, title, section);

coursesDataGrid.EditItemIndex = -1;
coursesDataGrid.DataBind();
}

Jon Agiato
 
G

Guest

Jon,

Without seeing how UpdateCourse works, it is difficult to tell. Is that
data that you are passing into the function being stored in a datatable or is
it directly updating your database layer? If that is within a datatable, be
sure that you are accepting changes before going forward to your data layer.

If this isn't your solution, please post the code to the UpdateCourse
function.

Thanks,
Ian Suttle
http://www.IanSuttle.com
 
J

Jon Agiato

Hi Ian, and thanks for the reply.

The application is set up using the layers pattern. The UI layer which is
the code you see here takes the values from the controls, and passes it back
to a controller object, which is shown here as courseManager. The
controller object takes the data, formulates the appropriate SQL query, and
passes it back to a database facade object, whch then performs the action
query to update the row with the primary key passed with the new
information. The method called here on courseManager also makes a call to
the db facade object to retrieve a new copy of the table after the
operations are completed and returns a data table, which the UI layer then
assigns to the grid and rebinds so that the new grid is shown with the
updates. Viewstate is disabled for the grid.

Here's the code for the UpdateCourse method on courseManager:

public DataTable UpdateCourse(string username, string schoolName, string
semester, string courseID, string title, string section)
{
string queryString = "Update CTCourses Set CourseID = '" + courseID + "',
Section = '" + section + "', Semester = '" + semester + "', Title = '" +
title + "', SchoolName = '" + schoolName + "' Where Username = '" +
username + "' AND CourseID = '" + courseID + "';";
dbFacade.ActionQuery(queryString);

return this.RetrieveCourses(username);
}

The call to this.RetrieveCourses returns a new copy of the table as a
DataTable.

Thanks to Ian, and any who can help.

Best wishes,

Jon Agiato
Jon,

Without seeing how UpdateCourse works, it is difficult to tell. Is that
data that you are passing into the function being stored in a datatable or
is
it directly updating your database layer? If that is within a datatable,
be
sure that you are accepting changes before going forward to your data
layer.

If this isn't your solution, please post the code to the UpdateCourse
function.

Thanks,
Ian Suttle
http://www.IanSuttle.com

Jon Agiato said:
Hi, I am trying to use a data grid in a web application in which I have
three tiers. The DataGrid is not set up to a data source, or a data
adapter,
so everytime I make a change I send the cell information to another
object
which forms an SQL query and makes the query to the DB (actually to a
DBFacade object). In any case, please see the code I have listed below
for
my update DataGrid event. With this code, the DB is updated using the old
data in the datagrid prior to the users changes, why is this and how can
I
take the new data out of the grid after the user makes changes? Thanks
very
much!

private void coursesDataGrid_UpdateCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
// get primary key courseID
TableCell courseIDCell = e.Item.Cells[1];
string courseID = ((TextBox)courseIDCell.Controls[0]).Text;

// get section
TableCell sectionCell = e.Item.Cells[2];
string section = ((TextBox)sectionCell.Controls[0]).Text;

// get semester
TableCell semesterCell = e.Item.Cells[3];
string semester = ((TextBox)semesterCell.Controls[0]).Text;

// get title
TableCell titleCell = e.Item.Cells[4];
string title = ((TextBox)titleCell.Controls[0]).Text;

// get school name
TableCell schoolNameCell = e.Item.Cells[5];
string schoolName = ((TextBox)schoolNameCell.Controls[0]).Text;

// update database here
coursesDataGrid.DataSource = courseManager.UpdateCourse(user, schoolName,
semester, courseID, title, section);

coursesDataGrid.EditItemIndex = -1;
coursesDataGrid.DataBind();
}

Jon Agiato
 
G

Guest

From what you have posted, it looks good. Can you post the ActionQuery and
RetrieveCourses functions? We'll get down to the bottom of it somewhere in
here. Also, try putting debug info throughout and see where the
miscommunication is occurring.

Thanks,
Ian Suttle
http://www.IanSuttle.com

Jon Agiato said:
Hi Ian, and thanks for the reply.

The application is set up using the layers pattern. The UI layer which is
the code you see here takes the values from the controls, and passes it back
to a controller object, which is shown here as courseManager. The
controller object takes the data, formulates the appropriate SQL query, and
passes it back to a database facade object, whch then performs the action
query to update the row with the primary key passed with the new
information. The method called here on courseManager also makes a call to
the db facade object to retrieve a new copy of the table after the
operations are completed and returns a data table, which the UI layer then
assigns to the grid and rebinds so that the new grid is shown with the
updates. Viewstate is disabled for the grid.

Here's the code for the UpdateCourse method on courseManager:

public DataTable UpdateCourse(string username, string schoolName, string
semester, string courseID, string title, string section)
{
string queryString = "Update CTCourses Set CourseID = '" + courseID + "',
Section = '" + section + "', Semester = '" + semester + "', Title = '" +
title + "', SchoolName = '" + schoolName + "' Where Username = '" +
username + "' AND CourseID = '" + courseID + "';";
dbFacade.ActionQuery(queryString);

return this.RetrieveCourses(username);
}

The call to this.RetrieveCourses returns a new copy of the table as a
DataTable.

Thanks to Ian, and any who can help.

Best wishes,

Jon Agiato
Jon,

Without seeing how UpdateCourse works, it is difficult to tell. Is that
data that you are passing into the function being stored in a datatable or
is
it directly updating your database layer? If that is within a datatable,
be
sure that you are accepting changes before going forward to your data
layer.

If this isn't your solution, please post the code to the UpdateCourse
function.

Thanks,
Ian Suttle
http://www.IanSuttle.com

Jon Agiato said:
Hi, I am trying to use a data grid in a web application in which I have
three tiers. The DataGrid is not set up to a data source, or a data
adapter,
so everytime I make a change I send the cell information to another
object
which forms an SQL query and makes the query to the DB (actually to a
DBFacade object). In any case, please see the code I have listed below
for
my update DataGrid event. With this code, the DB is updated using the old
data in the datagrid prior to the users changes, why is this and how can
I
take the new data out of the grid after the user makes changes? Thanks
very
much!

private void coursesDataGrid_UpdateCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
// get primary key courseID
TableCell courseIDCell = e.Item.Cells[1];
string courseID = ((TextBox)courseIDCell.Controls[0]).Text;

// get section
TableCell sectionCell = e.Item.Cells[2];
string section = ((TextBox)sectionCell.Controls[0]).Text;

// get semester
TableCell semesterCell = e.Item.Cells[3];
string semester = ((TextBox)semesterCell.Controls[0]).Text;

// get title
TableCell titleCell = e.Item.Cells[4];
string title = ((TextBox)titleCell.Controls[0]).Text;

// get school name
TableCell schoolNameCell = e.Item.Cells[5];
string schoolName = ((TextBox)schoolNameCell.Controls[0]).Text;

// update database here
coursesDataGrid.DataSource = courseManager.UpdateCourse(user, schoolName,
semester, courseID, title, section);

coursesDataGrid.EditItemIndex = -1;
coursesDataGrid.DataBind();
}

Jon Agiato
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top