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----------------------------->
<aspataGrid 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>
</aspataGrid>
<!---------------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 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----------------------------->
<aspataGrid 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>
</aspataGrid>
<!---------------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?