473,659 Members | 3,082 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.W ebControls.Data GridCommandEven tArgs e)
{
// get primary key courseID
TableCell courseIDCell = e.Item.Cells[1];
string courseID = ((TextBox)cours eIDCell.Control s[0]).Text;

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

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

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

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

// update database here
coursesDataGrid .DataSource = courseManager.U pdateCourse(use r, schoolName,
semester, courseID, title, section);

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

Jon Agiato
Nov 18 '05 #1
3 1792
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.W ebControls.Data GridCommandEven tArgs e)
{
// get primary key courseID
TableCell courseIDCell = e.Item.Cells[1];
string courseID = ((TextBox)cours eIDCell.Control s[0]).Text;

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

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

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

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

// update database here
coursesDataGrid .DataSource = courseManager.U pdateCourse(use r, 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(st ring 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.Action Query(queryStri ng);

return this.RetrieveCo urses(username) ;
}

The call to this.RetrieveCo urses 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*******@disc ussions.microso ft.com> wrote in message
news:14******** *************** ***********@mic rosoft.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.W ebControls.Data GridCommandEven tArgs e)
{
// get primary key courseID
TableCell courseIDCell = e.Item.Cells[1];
string courseID = ((TextBox)cours eIDCell.Control s[0]).Text;

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

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

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

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

// update database here
coursesDataGrid .DataSource = courseManager.U pdateCourse(use r, 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
miscommunicatio n 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(st ring 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.Action Query(queryStri ng);

return this.RetrieveCo urses(username) ;
}

The call to this.RetrieveCo urses 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*******@disc ussions.microso ft.com> wrote in message
news:14******** *************** ***********@mic rosoft.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.W ebControls.Data GridCommandEven tArgs e)
{
// get primary key courseID
TableCell courseIDCell = e.Item.Cells[1];
string courseID = ((TextBox)cours eIDCell.Control s[0]).Text;

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

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

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

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

// update database here
coursesDataGrid .DataSource = courseManager.U pdateCourse(use r, schoolName,
semester, courseID, title, section);

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

Jon Agiato


Nov 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1298
by: cwbp17 | last post by:
Have two tables that have a FK relationship on ID column. Have one datagrid that displays all of the columns of both tables. What's the best approach on updating a row from the datagrid back to the database? I've used the following code for the DataGird1_UpdateCommand Option1
1
1234
by: Patrick Delifer | last post by:
HI, I am trying to update a DataGrid where I want to rebind just the row that was updated. Here's the situation: 1) Updating is not a problem, it works fine. 2) I am not using ItemCommand (edit, delete, etc) 3) the button that issues the update/rebind is outside the datagrid, on the web form. Till now I have been issuing a full DataBind for the entire DG, that I read
5
2026
by: junglist | last post by:
Hi guys, I've been trying to implement an editable datagrid and i have been succesful up to the point where i can update my datagrid row by row. However what used to happen was that once i updated one row, all of them were updated so i immediatelly figured out that i have to include the id of every entry in the update statement. This is where the problem is raised. My database is an Access database. The table i am updating contains a Date...
0
1631
by: cwbp17 | last post by:
I'm having trouble updating individual datagrid cells. Have two tables car_master (columns include Car_ID, YEAR,VEHICLE) and car_detail (columns include Car_ID,PRICE,MILEAGE,and BODY);both tables have a FK relationship on CAR_ID so the oracledataadapter1 select statement(CommandText) is: select car_master.car_id, car_master.year,car_master.vehicle,car_detail.car_id AS EXPR1,
10
5656
by: jaYPee | last post by:
does anyone experienced slowness when updating a dataset using AcceptChanges? when calling this code it takes many seconds to update the database SqlDataAdapter1.Update(DsStudentCourse1) DsStudentCourse1.AcceptChanges() i'm also wondering because w/ out AcceptChanges the data is still save into the database and it is now faster.
0
1053
by: Mike P | last post by:
I'm updating a datagrid which works fine, but after updating the datagrid does not return to 'view' status, remaining in 'edit' status. Has anybody else had this problem? Thanks, Mike
2
1671
by: Greg | last post by:
I'm using the Framework 1.1, so I don't have access to the new DataGridView object. What I'm wondering is, is there a really simple way to bind a plain datagrid to a database in such a way that changes made to cells in the datagrid call an update on the corresoonding data within a database? I've looked in vain for a really simple way to do this with minimal coding, but I'm coming to the conclusion that I will have to trap the events of...
2
1319
by: marcmc | last post by:
Hey, I have never used a datagrid/dataset/adaptor/table method for updating data, I have always used direct updates to the database so ADO is quite new to me. I have a datagrid and I change a value in one of the fields. I hit save and the following code runs... Dim da As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter("Select * from MISRE_Threshold", cn) da.Update(dsThreshold, "MISRE_Threshold")
0
1467
by: Chet | last post by:
I have a Datagrid that is bound to a Datatable at runtime. I allow the user to select a number of rows using the mouse and then click a button that says "check selected rows", which then cycles through the selected rows and sets a check box field in the datagrid (and thus the databound datatable) to checked/true. It works like this: Dim cel As DataGridViewCheckBoxCell For Each row In DataGrid.SelectedRows cel = row.Cells(0) cel.Value =...
2
3309
by: =?Utf-8?B?VmFuZXNzYQ==?= | last post by:
Hi All! I am with a situation where I am not getting the right updating to the form's fields. The situation is the following one: I have one combobox and one textbox. I am using the CurrentChanged event of the BindingSource of the combobox to update the textbox. When selecting an item in the combobox or when selecting a row in the grid, it is updating the textbox correctly. The problem is when I apply a filter in the grid, and then...
0
8428
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8335
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8747
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8528
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8627
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2752
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1976
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.