I've not posted binding specific messages to the gridview part. This shows updating that particular message or message from a list of messages databound with an XML file. My main concern is omitting redundant code in the else
part. Any suggestions?
protected void grdMessage_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
DataRow xRow;
DataSet mdsRedirect = new DataSet();
mdsRedirect.ReadXml(st);
if (Request["MessageID"] != null && Convert.ToInt16(Request["MessageID"]) != 0)
{
//Find specific item from XML and set updated value
mdsRedirect.Tables[0].DefaultView.RowFilter = "MessageID=" + Convert.ToInt16(Request["MessageID"]);
if (mdsRedirect.Tables[0].DefaultView.Count > 0)
{
mdsRedirect.Tables[0].DefaultView[0]["MessageText"] = ((TextBox)(grdMessage.Rows[e.RowIndex].FindControl("txtupdMessage"))).Text;
mdsRedirect.Tables[0].DefaultView.RowFilter = string.Empty;
}
else {
xRow = mdsRedirect.Tables[0].Rows[e.RowIndex];
xRow["MessageText"] = ((TextBox)(grdMessage.Rows[e.RowIndex].FindControl("txtupdMessage"))).Text;
}
}
else
{
xRow = mdsRedirect.Tables[0].Rows[e.RowIndex];
xRow["MessageText"] = ((TextBox)(grdMessage.Rows[e.RowIndex].FindControl("txtupdMessage"))).Text;
}
grdMessage.EditIndex = -1;
mdsRedirect.WriteXml(st);
mdsRedirect.Dispose();
getxml();//read XML file and bind it
}
DataSet mdsRedirect = new DataSet();
throughmdsRedirect.Dispose();
by wrapping the creating within ausing
block. \$\endgroup\$