S
st
Hi,
I'm new to ASP.NET in general and just getting to grips with C# and web
controls.
So far I've a simple form with a DataGrid that is populated by a
DataSet that is itself populated by a SQL Server table.
The table contents are rendered and so far, so good.
My next step was to create edit button columns and wire up edit events.
I then attempted to persist the DataSet and DataAdapter across
postbacks by assigning to Session, finally adding code to call Update
against the DataAdapter in the DataGrid update event.
Here's where I run into a problem, because, no matter how I rearrange
the code, the table fails to update and the unchanged table state is
given back at the next grid fill.
Checking the DataSet table contents at various stages shows that its
contents also never seem to relect the edit. I thought the DataSet,
being bound to the DataGrid, would change in line with edits? Do I have
to take the contents of the edited grid, change them at the appropriate
points in the DataSet and then call the update against the DataAdapter?
No compilation/run-time errors, by the way - just not able to update.
Thank you.
Simon
Here's my code:
public class Maint : System.Web.UI.Page
{
protected System.Data.SqlClient.SqlConnection
sqlConnection1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.DataGrid
dgMasterGeneric;
protected String stringTable;
protected DataSet dsMaster;
//private SqlCommand sqlMaster;
protected SqlDataAdapter daMaster;
private void Page_Load(object sender, System.EventArgs
e)
// Put user code to initialize the page here
{
stringTable = Request.QueryString["table"];
if (!IsPostBack)
//DB read to grid
BindMasterGeneric(false);
else
{
daMaster =
(SqlDataAdapter)Session["da"];
dsMaster = (DataSet)Session["ds"];
};
}
private void Page_Unload(object sender, System.EventArgs e)
{
//Add the data store to the session
Session["da"]=daMaster;
Session["ds"]=dsMaster;
}
public void BindMasterGeneric(bool boolPersist)
{
//if (stringTable == string.Empty) return;
if (!boolPersist)
{
dsMaster = new DataSet();
daMaster = new SqlDataAdapter("SELECT * FROM "
+
stringTable,sqlConnection1);
SqlCommandBuilder commandBuilder = new
SqlCommandBuilder(daMaster);
}
daMaster.Fill(dsMaster,stringTable);
dgMasterGeneric.DataSource = dsMaster;
dgMasterGeneric.DataBind();
}
public void Update_MasterGeneric(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
//The clicked/edited row is derived from e
dgMasterGeneric.EditItemIndex =
e.Item.ItemIndex;
daMaster.Update(dsMaster,stringTable);
//Redisplay the data
BindMasterGeneric(true);
}
I'm new to ASP.NET in general and just getting to grips with C# and web
controls.
So far I've a simple form with a DataGrid that is populated by a
DataSet that is itself populated by a SQL Server table.
The table contents are rendered and so far, so good.
My next step was to create edit button columns and wire up edit events.
I then attempted to persist the DataSet and DataAdapter across
postbacks by assigning to Session, finally adding code to call Update
against the DataAdapter in the DataGrid update event.
Here's where I run into a problem, because, no matter how I rearrange
the code, the table fails to update and the unchanged table state is
given back at the next grid fill.
Checking the DataSet table contents at various stages shows that its
contents also never seem to relect the edit. I thought the DataSet,
being bound to the DataGrid, would change in line with edits? Do I have
to take the contents of the edited grid, change them at the appropriate
points in the DataSet and then call the update against the DataAdapter?
No compilation/run-time errors, by the way - just not able to update.
Thank you.
Simon
Here's my code:
public class Maint : System.Web.UI.Page
{
protected System.Data.SqlClient.SqlConnection
sqlConnection1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.DataGrid
dgMasterGeneric;
protected String stringTable;
protected DataSet dsMaster;
//private SqlCommand sqlMaster;
protected SqlDataAdapter daMaster;
private void Page_Load(object sender, System.EventArgs
e)
// Put user code to initialize the page here
{
stringTable = Request.QueryString["table"];
if (!IsPostBack)
//DB read to grid
BindMasterGeneric(false);
else
{
daMaster =
(SqlDataAdapter)Session["da"];
dsMaster = (DataSet)Session["ds"];
};
}
private void Page_Unload(object sender, System.EventArgs e)
{
//Add the data store to the session
Session["da"]=daMaster;
Session["ds"]=dsMaster;
}
public void BindMasterGeneric(bool boolPersist)
{
//if (stringTable == string.Empty) return;
if (!boolPersist)
{
dsMaster = new DataSet();
daMaster = new SqlDataAdapter("SELECT * FROM "
+
stringTable,sqlConnection1);
SqlCommandBuilder commandBuilder = new
SqlCommandBuilder(daMaster);
}
daMaster.Fill(dsMaster,stringTable);
dgMasterGeneric.DataSource = dsMaster;
dgMasterGeneric.DataBind();
}
public void Update_MasterGeneric(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
//The clicked/edited row is derived from e
dgMasterGeneric.EditItemIndex =
e.Item.ItemIndex;
daMaster.Update(dsMaster,stringTable);
//Redisplay the data
BindMasterGeneric(true);
}