DataGrid Update not picking up updated values!

G

Guest

A few issues/questions about using DataGrid from ADO.NET to update a column.

I have walked through and follow the Example "Walkthrough: Editing an Access
Database with ADO.NET" from
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnadonet/html/adon_wtaccessdb.asp>
, with the exception that I use OdbcConnection to an Access DB instead of
OleDB

Anyway,

I have the following snippet that is of interest:
<!---------------Start of FormSchedule.aspx----------------------------->
<asp:DataGrid id="FormListDataGrid" runat="server"
HeaderStyle-Height="25px" HeaderStyle-CssClass="header"
EditItemStyle="data" ShowHeader="true" AutoGenerateColumns="False"
AllowSorting="true" OnSortCommand="SortForm_OnClick"
OnEditCommand="FormListDataGrid_Edit"
OnCancelCommand="FormListDataGrid_Cancel"
OnUpdateCommand="FormListDataGrid_Update"
DataKeyField="count">
<ItemStyle CssClass="data"></ItemStyle>
<HeaderStyle Height="25px" CssClass="header"></HeaderStyle>
<Columns>
<asp:HyperLinkColumn DataNavigateUrlField="FormName"
DataNavigateUrlFormatString="FormSchedule.aspx?form={0}&process=1"
DataTextField="FormName" SortExpression="FormName" HeaderText="Form
Name"></asp:HyperLinkColumn>
<asp:BoundColumn DataField="count" SortExpression="count" HeaderText="Due
Date-Business Day"></asp:BoundColumn>
<asp:EditCommandColumn EditText="Edit" CancelText="Cancel"
UpdateText="Update" />


</Columns>
</asp:DataGrid>
<!---------------End of FormSchedule.aspx----------------------------->

/***************Start of FormSchedule.aspx.cs***************/
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
// Put user code to initialize the page here

if (Request.QueryString["process"]!="1")
{
DataSet dataList = new DataSet();

OdbcConnection Conn = new OdbcConnection("Driver={Microsoft Access
Driver (*.mdb)};DBQ=" + Request.PhysicalApplicationPath +
ConfigurationSettings.AppSettings["DBLocation"]);

Conn.Open();
OdbcDataAdapter accessDataAdapter = new OdbcDataAdapter("Select * from
FormDefinitions",Conn);
accessDataAdapter.Fill(dataList);

DataView oView= new DataView(dataList.Tables[0]);
FormListDataGrid.DataSource= oView;
FormListDataGrid.DataBind();
Conn.Close();
}
else
{
FormListDataGrid.Visible= false;
}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

protected void SortForm_OnClick(Object source,
DataGridSortCommandEventArgs e)
{
((DataView) FormListDataGrid.DataSource).Sort= e.SortExpression;
FormListDataGrid.DataBind();
}

protected void FormListDataGrid_Edit(Object sender,
DataGridCommandEventArgs e)
{

// Set the EditItemIndex property to the index of the item clicked
// in the DataGrid control to enable editing for that item. Be sure
// to rebind the DateGrid to the data source to refresh the control.
FormListDataGrid.EditItemIndex = e.Item.ItemIndex;
FormListDataGrid.DataBind();

}

protected void FormListDataGrid_Cancel(Object sender,
DataGridCommandEventArgs e)
{

// Set the EditItemIndex property to -1 to exit editing mode.
// Be sure to rebind the DateGrid to the data source to refresh
// the control.
FormListDataGrid.EditItemIndex = -1;
FormListDataGrid.DataBind();

}

protected void FormListDataGrid_Update(object source,
DataGridCommandEventArgs e)
{
//int ID = (int) FormListDataGrid.DataKeys[(int) e.Item.ItemIndex];

string count = ((TextBox)e.Item.Cells[1].Controls[0]).Text;
string formName= ((HyperLink)e.Item.Cells[0].Controls[0]).Text;

string sql =
"UPDATE FormDefinitions SET [count]=" + count +
" WHERE FormName='" + formName + "'";


//ExecuteNonQuery(sql);
OdbcConnection Conn = new OdbcConnection("Driver={Microsoft Access Driver
(*.mdb)};DBQ=" + Request.PhysicalApplicationPath +
ConfigurationSettings.AppSettings["DBLocation"]);

Conn.Open();
OdbcCommand cmd = new OdbcCommand(sql,Conn);
cmd.ExecuteNonQuery();




FormListDataGrid.EditItemIndex = -1;
FormListDataGrid.DataBind();
}
/***************End of FormSchedule.aspx.cs***************/

Fundamental problem number 1!!!:
- within FormListDataGrid_Update,
((TextBox)e.Item.Cells[1].Controls[0]).Text is picking up the OLD field
value, not the value I just entered having clicked the "Edit" link! Why?
Very confused!

-Also, Are there any better method of doing the update (note in future I
will be using an OleDB Connection to SQL Server). In particular, if I update
the DataSet/DataView which is the DataGrid's Datasource, would the underlying
record in the database be updated?
 
I

intrader

Perhaps you may need to look into IsPostBack. It allows you to test
whether your form code is executing the first time or on postback.

On Thu,
A few issues/questions about using DataGrid from ADO.NET to update a column.

I have walked through and follow the Example "Walkthrough: Editing an Access
Database with ADO.NET" from
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnadonet/html/adon_wtaccessdb.asp>
, with the exception that I use OdbcConnection to an Access DB instead of
OleDB

Anyway,

I have the following snippet that is of interest:
<!---------------Start of FormSchedule.aspx----------------------------->
<asp:DataGrid id="FormListDataGrid" runat="server"
HeaderStyle-Height="25px" HeaderStyle-CssClass="header"
EditItemStyle="data" ShowHeader="true" AutoGenerateColumns="False"
AllowSorting="true" OnSortCommand="SortForm_OnClick"
OnEditCommand="FormListDataGrid_Edit"
OnCancelCommand="FormListDataGrid_Cancel"
OnUpdateCommand="FormListDataGrid_Update"
DataKeyField="count">
<ItemStyle CssClass="data"></ItemStyle>
<HeaderStyle Height="25px" CssClass="header"></HeaderStyle>
<Columns>
<asp:HyperLinkColumn DataNavigateUrlField="FormName"
DataNavigateUrlFormatString="FormSchedule.aspx?form={0}&process=1"
DataTextField="FormName" SortExpression="FormName" HeaderText="Form
Name"></asp:HyperLinkColumn>
<asp:BoundColumn DataField="count" SortExpression="count" HeaderText="Due
Date-Business Day"></asp:BoundColumn>
<asp:EditCommandColumn EditText="Edit" CancelText="Cancel"
UpdateText="Update" />


</Columns>
</asp:DataGrid>
<!---------------End of FormSchedule.aspx----------------------------->

/***************Start of FormSchedule.aspx.cs***************/
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
// Put user code to initialize the page here

if (Request.QueryString["process"]!="1")
{
DataSet dataList = new DataSet();

OdbcConnection Conn = new OdbcConnection("Driver={Microsoft Access
Driver (*.mdb)};DBQ=" + Request.PhysicalApplicationPath +
ConfigurationSettings.AppSettings["DBLocation"]);

Conn.Open();
OdbcDataAdapter accessDataAdapter = new OdbcDataAdapter("Select * from
FormDefinitions",Conn);
accessDataAdapter.Fill(dataList);

DataView oView= new DataView(dataList.Tables[0]);
FormListDataGrid.DataSource= oView;
FormListDataGrid.DataBind();
Conn.Close();
}
else
{
FormListDataGrid.Visible= false;
}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

protected void SortForm_OnClick(Object source,
DataGridSortCommandEventArgs e)
{
((DataView) FormListDataGrid.DataSource).Sort= e.SortExpression;
FormListDataGrid.DataBind();
}

protected void FormListDataGrid_Edit(Object sender,
DataGridCommandEventArgs e)
{

// Set the EditItemIndex property to the index of the item clicked
// in the DataGrid control to enable editing for that item. Be sure
// to rebind the DateGrid to the data source to refresh the control.
FormListDataGrid.EditItemIndex = e.Item.ItemIndex;
FormListDataGrid.DataBind();

}

protected void FormListDataGrid_Cancel(Object sender,
DataGridCommandEventArgs e)
{

// Set the EditItemIndex property to -1 to exit editing mode.
// Be sure to rebind the DateGrid to the data source to refresh
// the control.
FormListDataGrid.EditItemIndex = -1;
FormListDataGrid.DataBind();

}

protected void FormListDataGrid_Update(object source,
DataGridCommandEventArgs e)
{
//int ID = (int) FormListDataGrid.DataKeys[(int) e.Item.ItemIndex];

string count = ((TextBox)e.Item.Cells[1].Controls[0]).Text;
string formName= ((HyperLink)e.Item.Cells[0].Controls[0]).Text;

string sql =
"UPDATE FormDefinitions SET [count]=" + count +
" WHERE FormName='" + formName + "'";


//ExecuteNonQuery(sql);
OdbcConnection Conn = new OdbcConnection("Driver={Microsoft Access Driver
(*.mdb)};DBQ=" + Request.PhysicalApplicationPath +
ConfigurationSettings.AppSettings["DBLocation"]);

Conn.Open();
OdbcCommand cmd = new OdbcCommand(sql,Conn);
cmd.ExecuteNonQuery();




FormListDataGrid.EditItemIndex = -1;
FormListDataGrid.DataBind();
}
/***************End of FormSchedule.aspx.cs***************/

Fundamental problem number 1!!!:
- within FormListDataGrid_Update,
((TextBox)e.Item.Cells[1].Controls[0]).Text is picking up the OLD field
value, not the value I just entered having clicked the "Edit" link! Why?
Very confused!

-Also, Are there any better method of doing the update (note in future I
will be using an OleDB Connection to SQL Server). In particular, if I update
the DataSet/DataView which is the DataGrid's Datasource, would the underlying
record in the database be updated?
 
K

Kevin Yu [MSFT]

Hi,

Just as Intrader mentioned, you can take a look at IsPostBack property of
the page. If IsPostBack is false, it mean that the page is loaded the first
time, you can load data from the database using DataAdapter. If IsPostBack
is true, you just ignore the part of code.

Since you have executed the following SQL statement, the data in the
underlying database will of course be updated.

"UPDATE FormDefinitions SET [count]=" + count + " WHERE FormName='" +
formName + "'";

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
G

Guest

Not working STill!!

1) As expected, Page.IsPostBack==true when
1.1) page is loaded when I click the "Edit" link
1.2) Page is loaded after I changed the text box value for the field I want
to update and clicked the "Update" Link

2) Cannot undetrstand why ((TextBox)e.Item.Cells[1].Controls[0]).Text is not
picking up the value I entered!

3) I know I can invoke inline SQL or called a stored procedure to update the
underlying database, but is this the *best* way? If I update the underlying
dataset/dataview which the the datagrid's datasource, would the underlying
database be updated by reference?
 
K

Kevin Yu [MSFT]

Hi,

1. Yes, this is the expeted behavior.

2. First please check if the EnableViewState property of the page has been
set to true. You can step through all code after clicking the Update link
to see if the data source has been reloaded. The data loading process has
to be bypassed if post back.

3. I think this is the best way. Actually, this is the only way to update
database. The underlying database will not be updated automatically if you
change DataSet/DataView. All the database update has to be achieved by SQL
statement or stored procedures.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
G

Guest

2) Found the problem- the page_load event was over-writing the DataGrid even
when Update is clicked!
 
K

Kevin Yu [MSFT]

It is nice to hear that you have had the problem resolved. Thanks for
sharing your experience with all the people here. If you have any
questions, please feel free to post them in the community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
G

Guest

This is very weird now. I now added another column to the DataGrid, and
changed my code as follows:
- put code that renders the DataGrid in the populateDataGrid method
- invoke populateDataGrid in page_load ONLY if !Page.IsPostBack
- invoke populateDataGrid following UPDATE SQL is run within
FormListDataGrid_Update

The weird thing is whilst the rendering behave as expected when I run the
code (ASP.NET 1.1) via Visual Studio .NET 2003 on IIS5.1@WinXP Pro SP1, when
I run the same debug code (but not debugging via Visual Studio), the values
in the DataGrid does not get refreshed with what I just updated following
FormListDataGrid_Update

Snippet again as follows:

Confused what has gone wrong!?
<!--------------------------Start of
FormSchedule.aspx---------------------------------------------->
<asp:DataGrid id="FormListDataGrid" runat="server"
HeaderStyle-Height="25px" HeaderStyle-CssClass="header"
EditItemStyle="data" ShowHeader="true" AutoGenerateColumns="False"
AllowSorting="true" OnSortCommand="SortForm_OnClick"
OnEditCommand="FormListDataGrid_Edit"
OnCancelCommand="FormListDataGrid_Cancel"
OnUpdateCommand="FormListDataGrid_Update"
DataKeyField="Date">
<ItemStyle CssClass="data"></ItemStyle>
<HeaderStyle Height="25px" CssClass="header"></HeaderStyle>
<Columns>
<asp:BoundColumn DataField="FormName" SortExpression="FormName"
HeaderText="Form Name" ReadOnly="True"></asp:BoundColumn>
<asp:BoundColumn DataField="Date" SortExpression="Date" HeaderText="
Date"></asp:BoundColumn>
<dataGridCol:DataBoundColumn DataField="type" SortExpression="type"
HeaderText="type" HeaderStyle-Height="25px"
HeaderStyle-CssClass="header"></dataGridCol:DataBoundColumn>
<asp:EditCommandColumn EditText="Edit" CancelText="Cancel"
UpdateText="Update" />
</Columns>
</asp:DataGrid>
<!--------------------------Start of
FormSchedule.aspx---------------------------------------------->

/*******************Start of FormSchedule.aspx.cs *******************/
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.Odbc;
using System.Configuration;
using Csfb.WebControls;

namespace MyCompany.It.Forms
{
/// <summary>
/// Summary description for FormSchedule.
/// </summary>
public class FormSchedule : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid FormListDataGrid;
protected DataSet lookupDataSet;

private void Page_Load(object sender, System.EventArgs e)
{
DataBoundColumn formTypeLookupCol;
ArrayList formTypeList = new ArrayList();
// Put user code to initialize the page here
// Put user code to initialize the page here
if (!Page.IsPostBack)
{
populateDataGrid();
}
lookupDataSet = new DataSet();

OdbcConnection conn = new OdbcConnection("Driver={Microsoft Access Driver
(*.mdb)};DBQ=" + Request.PhysicalApplicationPath +
ConfigurationSettings.AppSettings["DBLocation"]);

conn.Open();
OdbcCommand accessCommand = new OdbcCommand("Select type from
formtype",conn);
OdbcDataReader accessReader;
accessReader =
accessCommand.ExecuteReader(CommandBehavior.CloseConnection);

formTypeLookupCol = (DataBoundColumn)FormListDataGrid.Columns[2];

//formTypeLookupCol.LookupDataSource= lookupDataSet;

while (accessReader.Read())
{
formTypeList.Add(accessReader["type"]);
}
formTypeLookupCol.DataSource= formTypeList;



}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

protected void SortForm_OnClick(Object source,
DataGridSortCommandEventArgs e)
{
populateDataGrid();
((DataView) FormListDataGrid.DataSource).Sort= e.SortExpression;
FormListDataGrid.DataBind();
}

protected void FormListDataGrid_Edit(Object sender,
DataGridCommandEventArgs e)
{

// Set the EditItemIndex property to the index of the item clicked
// in the DataGrid control to enable editing for that item. Be sure
// to rebind the DateGrid to the data source to refresh the control.
populateDataGrid();
FormListDataGrid.EditItemIndex = e.Item.ItemIndex;
FormListDataGrid.DataBind();

}

protected void FormListDataGrid_Cancel(Object sender,
DataGridCommandEventArgs e)
{

// Set the EditItemIndex property to -1 to exit editing mode.
// Be sure to rebind the DateGrid to the data source to refresh
// the control.
populateDataGrid();
FormListDataGrid.EditItemIndex = -1;
FormListDataGrid.DataBind();

}

protected void FormListDataGrid_Update(object source,
DataGridCommandEventArgs e)
{
//int ID = (int) FormListDataGrid.DataKeys[(int) e.Item.ItemIndex];

string formName= e.Item.Cells[0].Text;
string date = ((TextBox)e.Item.Cells[1].Controls[0]).Text;
string formType =
((DropDownList)e.Item.Cells[2].Controls[0]).SelectedValue;

string sql =
"UPDATE FormDefinitions SET [date-BD]=" + date +
",[type]='" + formType +
"' WHERE FormName='" + formName + "'";


//ExecuteNonQuery(sql);
OdbcConnection Conn = new OdbcConnection("Driver={Microsoft Access Driver
(*.mdb)};DBQ=" + Request.PhysicalApplicationPath +
ConfigurationSettings.AppSettings["DBLocation"]);

Conn.Open();
OdbcCommand cmd = new OdbcCommand(sql,Conn);
cmd.ExecuteNonQuery();



populateDataGrid();
FormListDataGrid.EditItemIndex = -1;
FormListDataGrid.DataBind();

}

private void populateDataGrid()
{
DataSet dataList = new DataSet();

OdbcConnection Conn = new OdbcConnection("Driver={Microsoft Access Driver
(*.mdb)};DBQ=" + Request.PhysicalApplicationPath +
ConfigurationSettings.AppSettings["DBLocation"]);

Conn.Open();
OdbcDataAdapter accessDataAdapter = new OdbcDataAdapter("Select * from
FormDefinitions",Conn);
accessDataAdapter.Fill(dataList);

DataView oView= new DataView(dataList.Tables[0]);
FormListDataGrid.DataSource= oView;
FormListDataGrid.DataBind();
Conn.Close();
}


}

}
/*******************End of FormSchedule.aspx.cs *******************/
 
G

Guest

Sorted!

I didn't close the Connection following the update SQL

Patrick said:
This is very weird now. I now added another column to the DataGrid, and
changed my code as follows:
- put code that renders the DataGrid in the populateDataGrid method
- invoke populateDataGrid in page_load ONLY if !Page.IsPostBack
- invoke populateDataGrid following UPDATE SQL is run within
FormListDataGrid_Update

The weird thing is whilst the rendering behave as expected when I run the
code (ASP.NET 1.1) via Visual Studio .NET 2003 on IIS5.1@WinXP Pro SP1, when
I run the same debug code (but not debugging via Visual Studio), the values
in the DataGrid does not get refreshed with what I just updated following
FormListDataGrid_Update

Snippet again as follows:

Confused what has gone wrong!?
<!--------------------------Start of
FormSchedule.aspx---------------------------------------------->
<asp:DataGrid id="FormListDataGrid" runat="server"
HeaderStyle-Height="25px" HeaderStyle-CssClass="header"
EditItemStyle="data" ShowHeader="true" AutoGenerateColumns="False"
AllowSorting="true" OnSortCommand="SortForm_OnClick"
OnEditCommand="FormListDataGrid_Edit"
OnCancelCommand="FormListDataGrid_Cancel"
OnUpdateCommand="FormListDataGrid_Update"
DataKeyField="Date">
<ItemStyle CssClass="data"></ItemStyle>
<HeaderStyle Height="25px" CssClass="header"></HeaderStyle>
<Columns>
<asp:BoundColumn DataField="FormName" SortExpression="FormName"
HeaderText="Form Name" ReadOnly="True"></asp:BoundColumn>
<asp:BoundColumn DataField="Date" SortExpression="Date" HeaderText="
Date"></asp:BoundColumn>
<dataGridCol:DataBoundColumn DataField="type" SortExpression="type"
HeaderText="type" HeaderStyle-Height="25px"
HeaderStyle-CssClass="header"></dataGridCol:DataBoundColumn>
<asp:EditCommandColumn EditText="Edit" CancelText="Cancel"
UpdateText="Update" />
</Columns>
</asp:DataGrid>
<!--------------------------Start of
FormSchedule.aspx---------------------------------------------->

/*******************Start of FormSchedule.aspx.cs *******************/
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.Odbc;
using System.Configuration;
using Csfb.WebControls;

namespace MyCompany.It.Forms
{
/// <summary>
/// Summary description for FormSchedule.
/// </summary>
public class FormSchedule : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid FormListDataGrid;
protected DataSet lookupDataSet;

private void Page_Load(object sender, System.EventArgs e)
{
DataBoundColumn formTypeLookupCol;
ArrayList formTypeList = new ArrayList();
// Put user code to initialize the page here
// Put user code to initialize the page here
if (!Page.IsPostBack)
{
populateDataGrid();
}
lookupDataSet = new DataSet();

OdbcConnection conn = new OdbcConnection("Driver={Microsoft Access Driver
(*.mdb)};DBQ=" + Request.PhysicalApplicationPath +
ConfigurationSettings.AppSettings["DBLocation"]);

conn.Open();
OdbcCommand accessCommand = new OdbcCommand("Select type from
formtype",conn);
OdbcDataReader accessReader;
accessReader =
accessCommand.ExecuteReader(CommandBehavior.CloseConnection);

formTypeLookupCol = (DataBoundColumn)FormListDataGrid.Columns[2];

//formTypeLookupCol.LookupDataSource= lookupDataSet;

while (accessReader.Read())
{
formTypeList.Add(accessReader["type"]);
}
formTypeLookupCol.DataSource= formTypeList;



}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

protected void SortForm_OnClick(Object source,
DataGridSortCommandEventArgs e)
{
populateDataGrid();
((DataView) FormListDataGrid.DataSource).Sort= e.SortExpression;
FormListDataGrid.DataBind();
}

protected void FormListDataGrid_Edit(Object sender,
DataGridCommandEventArgs e)
{

// Set the EditItemIndex property to the index of the item clicked
// in the DataGrid control to enable editing for that item. Be sure
// to rebind the DateGrid to the data source to refresh the control.
populateDataGrid();
FormListDataGrid.EditItemIndex = e.Item.ItemIndex;
FormListDataGrid.DataBind();

}

protected void FormListDataGrid_Cancel(Object sender,
DataGridCommandEventArgs e)
{

// Set the EditItemIndex property to -1 to exit editing mode.
// Be sure to rebind the DateGrid to the data source to refresh
// the control.
populateDataGrid();
FormListDataGrid.EditItemIndex = -1;
FormListDataGrid.DataBind();

}

protected void FormListDataGrid_Update(object source,
DataGridCommandEventArgs e)
{
//int ID = (int) FormListDataGrid.DataKeys[(int) e.Item.ItemIndex];

string formName= e.Item.Cells[0].Text;
string date = ((TextBox)e.Item.Cells[1].Controls[0]).Text;
string formType =
((DropDownList)e.Item.Cells[2].Controls[0]).SelectedValue;

string sql =
"UPDATE FormDefinitions SET [date-BD]=" + date +
",[type]='" + formType +
"' WHERE FormName='" + formName + "'";


//ExecuteNonQuery(sql);
OdbcConnection Conn = new OdbcConnection("Driver={Microsoft Access Driver
(*.mdb)};DBQ=" + Request.PhysicalApplicationPath +
ConfigurationSettings.AppSettings["DBLocation"]);

Conn.Open();
OdbcCommand cmd = new OdbcCommand(sql,Conn);
cmd.ExecuteNonQuery();



populateDataGrid();
FormListDataGrid.EditItemIndex = -1;
FormListDataGrid.DataBind();

}

private void populateDataGrid()
{
DataSet dataList = new DataSet();

OdbcConnection Conn = new OdbcConnection("Driver={Microsoft Access Driver
(*.mdb)};DBQ=" + Request.PhysicalApplicationPath +
ConfigurationSettings.AppSettings["DBLocation"]);

Conn.Open();
OdbcDataAdapter accessDataAdapter = new OdbcDataAdapter("Select * from
FormDefinitions",Conn);
accessDataAdapter.Fill(dataList);

DataView oView= new DataView(dataList.Tables[0]);
FormListDataGrid.DataSource= oView;
FormListDataGrid.DataBind();
Conn.Close();
}


}

}
/*******************End of FormSchedule.aspx.cs *******************/


Kevin Yu said:
It is nice to hear that you have had the problem resolved. Thanks for
sharing your experience with all the people here. If you have any
questions, please feel free to post them in the community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 

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

Top