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}
Updating messages from a list of messages
c#
Not sure if it's optimized, but here's something of a simplification:    protected void grdMessage_RowUpdating(object sender, GridViewUpdateEventArgs e)    {        string messageIdString = Request[MessageID];        int messageId = messageIdString == null ? 0 : Convert.ToInt16(messageIdString);        using (DataSet mdsRedirect = new DataSet())        {            DataTable table = mdsRedirect.Tables[0];            string updateMessageText = ((TextBox)grdMessage.Rows[e.RowIndex].FindControl(txtupdMessage)).Text;            mdsRedirect.ReadXml(st);            table.DefaultView.RowFilter = messageId == 0 ? table.DefaultView.RowFilter : MessageID= + messageId;            if ((messageId != 0) && (table.DefaultView.Count > 0))            {                // Find specific item from XML and set updated value                table.DefaultView[0][MessageText] = updateMessageText;                table.DefaultView.RowFilter = string.Empty;            }            else            {                table.Rows[e.RowIndex][MessageText] = updateMessageText;            }            grdMessage.EditIndex = -1;            mdsRedirect.WriteXml(st);        }        getxml(); // read XML file and bind it    }