468,107 Members | 1,456 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,107 developers. It's quick & easy.

updating a DataGrid

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
Nov 18 '05 #1
3 1552
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" wrote:
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

Nov 18 '05 #2
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
"Ian Suttle" <Ia*******@discussions.microsoft.com> wrote in message
news:14**********************************@microsof t.com...
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" wrote:
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

Nov 18 '05 #3
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" wrote:
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
"Ian Suttle" <Ia*******@discussions.microsoft.com> wrote in message
news:14**********************************@microsof t.com...
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" wrote:
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


Nov 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by cwbp17 | last post: by
1 post views Thread by Patrick Delifer | last post: by
5 posts views Thread by junglist | last post: by
reply views Thread by Mike P | last post: by
2 posts views Thread by marcmc | last post: by
2 posts views Thread by =?Utf-8?B?VmFuZXNzYQ==?= | last post: by
1 post views Thread by Solo | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.