Using UPDATE with a DATAGRID problem
Hi I am having problems making the update datagrid property working. It still uses the old value, and I have followed the example in "ASP.NET in three weeks" and it won't work... here is my code:
firs the cs code behind:
PHP Code:
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.OleDb;
namespace WebApplication12
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Data.DataSet ds;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack )
{
BindData();
}
}
#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.ds = new System.Data.DataSet();
((System.ComponentModel.ISupportInitialize)(this.ds)).BeginInit();
this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_CancelCommand);
this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand);
this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_UpdateCommand);
//
// ds
//
this.ds.DataSetName = "ds";
this.ds.Locale = new System.Globalization.CultureInfo("sv-SE");
this.Load += new System.EventHandler(this.Page_Load);
((System.ComponentModel.ISupportInitialize)(this.ds)).EndInit();
}
#endregion
public void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = e.Item.ItemIndex;
BindData();
}
public void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = -1;
BindData();
}
public void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
ds.AcceptChanges();
DataGrid1.DataSource = ds;
BindData();
}
public void BindData()
{
OleDbConnection myConn = new OleDbConnection("Provider=SQLOLEDB;data source=4647117000N26;initial catalog=Northwind;persist security info=False;user id=logger;password=logger;workstation id=WS1321;packet size=4096");
string sqlQuery = "SELECT * FROM Customers";
//DataSet ds = new DataSet();
OleDbDataAdapter objCmd = new OleDbDataAdapter(sqlQuery,myConn);
OleDbCommandBuilder cmdB = new OleDbCommandBuilder(objCmd);
objCmd.Fill(ds, "Customers");
DataGrid1.DataSource = ds;
Page.DataBind();
}
}
}
And then there is my aspx page
PHP Code:
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication12.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 149px; POSITION: absolute; TOP: 95px" runat="server" Width="627px" Height="428px" AutoGenerateColumns="False" OnEditCommand="DataGrid1_EditCommand" OnUpdateCommand="DataGrid1_UpdateCommand" OnCancelCommand="DataGrid1_CancelCommand" DataSource="<%# ds %>">
<AlternatingItemStyle BackColor="#C0C0FF"></AlternatingItemStyle>
<Columns>
<asp:BoundColumn DataField="CustomerID" HeaderText="Kund-ID"></asp:BoundColumn>
<asp:BoundColumn DataField="ContactName" HeaderText="Namn"></asp:BoundColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Uppdatera" HeaderText="Ändra" CancelText="Avbryt" EditText="Ändra"></asp:EditCommandColumn>
</Columns>
</asp:DataGrid>
</form>
</body>
</HTML>
This is C# syntax, but the pricipal is the same in vb.net.
I am pretty sure I do something wrong in the update event, but I am not sure what to do really. I want to make the changes to my dataset and then use an OLEDBCOMMANDBUILDER object to update the database with the changes made to the local dataset. Then I want to update the datagrid with the new data...
HEEELP!
kind regards
Henrik