Thursday, September 16, 2010

Ajax assembly version conflicts and resolution

The following could be faced during your Staging to Production movement or Implementation server.

System.Web.Script.Services.ScriptMethodAttribute exists in both 'C:\Windows\Microsoft.Net\Framework64\v2.050727\Temporary ASP.Net....' 
and C:\Windows\assembly\GAC_MSIL\System.Web.Extensions\3.5.0.0_...'

BindingRedirects


First step is to clear the Temporary ASP.Net folder and try, even then you face the same error message, spend some time in the below described.

When you build a .NET Framework application against a strong-named assembly, the application uses that version of the assembly at run time by default, even if a new version is available. However, you can configure the application to run against a newer version of the assembly.

You can redirect more than one assembly version by including multiple elements in a element.

The following example shows how to redirect one assembly version to another.



Visual Studio 2008 automatically adds binding redirects in the web.config file
This solves many of the interoperability issues and nothing wrong in creating applications with reference to System.Web.Extensions of older version (v1.0) with VS2008

Wednesday, September 1, 2010

Basics; Updating a row item in GridView

GridView Row Edit and Update would be one among the basics a .Net fresher programmer to start with.

   CODE BEHIND File

   protected void GrvCannibalize_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GrvCannibalize.EditIndex = e.NewEditIndex;
        BindCannibaliseGrid();
    }
    protected void GrvCannibalize_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GrvCannibalize.EditIndex = -1;
        BindCannibaliseGrid();
    }
   protected void GrvCannibalize_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        TrnFutureSales oEntity = new TrnFutureSales();  // Business Entity
FutureSalesLogic oLogic = new FutureSalesLogic();  // Business Logic Layer
      
TextBox oWRActual = GrvCannibalize.Rows[e.RowIndex].FindControl("TxtWRActual") as TextBox;
        TextBox oWIActual = GrvCannibalize.Rows[e.RowIndex].FindControl("TxtWIActual") as TextBox;
        TextBox oCNNActual = GrvCannibalize.Rows[e.RowIndex].FindControl("TxtCNNActual") as TextBox;

        HiddenField oFutureSalesId = GrvCannibalize.Rows[e.RowIndex].FindControl("HdnFutureSalesId") as HiddenField;

        oEntity.ActualCan = Convert.ToDecimal(oWRActual.Text);
        oEntity.ActualRR = Convert.ToDecimal(oCNNActual.Text);
        oEntity.ActualImpact = Convert.ToDecimal(oWIActual.Text);
        oEntity.FutureSalesId =  Convert.ToInt64(oFutureSalesId.Value);

oLogic.UpdateFutureSalesForId(oEntity);    // This method has to be added as yours to update the database. 

        GrvCannibalize.EditIndex = -1;
        BindCannibaliseGrid(); // Bind the grid again to load with the new values.
    }

ASPX (MARK UP) File

<asp:GridView ID="GrvCannibalize" DataKeyNames="FutureSalesID" runat="server" AutoGenerateColumns="false" EmptyDataText="No Record Found" EmptyDataRowStyle-CssClass="redstar" AllowPaging="true" Width="100%" PageSize="5" ShowFooter="true" OnRowEditing="GrvCannibalize_RowEditing" OnRowCancelingEdit="GrvCannibalize_RowCancelingEdit"
OnRowUpdating="GrvCannibalize_RowUpdating" AutoGenerateEditButton="false">

<Columns>
<asp:CommandField ButtonType="Image" ShowEditButton="True" EditImageUrl="~/App_Themes/images/Edit.gif" UpdateImageUrl="~/App_Themes/images/Update.gif" CancelImageUrl="~/App_Themes/images/Cancel.jpeg">
</<asp:CommandField>

<asp:TemplateField HeaderText="Actual WI">
  <ItemTemplate>
  <asp:Label ID="LblWIActual" runat="server" Text='<%# Bind("ActualImpact") %>'></<asp:Label>
 <asp:HiddenField ID="HdnFutureSalesId" runat="server" Value='<%# Eval("FutureSalesID") %>'></<asp:HiddenField>
  </ItemTemplate>
    <EditItemTemplate>
      <asp:TextBox ID="TxtWIActual" runat="server" CssClass="txtboxAmount" Text=''></<asp:TextBox>
      <asp:RequiredFieldValidator ID="RfvWIActual" runat="server" ErrorMessage="Required" CssClass="redstar" Text="Required" ControlToValidate="TxtWIActual">
</<asp:RequiredFieldValidator>
</EditItemTemplate>
</<asp:TemplateField>

<asp:TemplateField HeaderText="Actual WR">
<ItemTemplate>
<asp:Label ID="LblWRActual" runat="server" Text='<%# Bind("ActualRR") %>'></<asp:Label>
</ItemTemplate>
<EditItemTemplate>
  <asp:TextBox ID="TxtWRActual" runat="server" CssClass="txtboxAmount" Text=''></<asp:TextBox>
    <asp:RequiredFieldValidator ID="RfvWRActual" runat="server" ErrorMessage="Required" CssClass="redstar" Text="Required" ControlToValidate="TxtWRActual"></<asp:RequiredFieldValidator>
</EditItemTemplate>
</<asp:TemplateField>

</Columns>
</<asp:GridView>

Clientside Validation for Controls inside GridView

Server Side validations for the Controls inside the Gridview will be bit heavy for the pages havingmore than two gridviews. Javascripts can be used to iterate through the controls inside the particular column of grid. We will have to create a Javascript Two dimensional array to hold both the Server ID and Client ID of the control from the GridView PreRender Event. Same can be achieved from GridView RowDataBound Event also.

The following PreRender Event registers a Two Dimensional Array with control's Server ID and Client ID.


protected void GrvPostPromotion_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ClientScriptManager oCsm = this.Page.ClientScript;
            TextBox oGrossSales = e.Row.FindControl("TxtGrossSalesPOP") as TextBox;
            TextBox oNetToGross = e.Row.FindControl("TxtNetToGrossPOP") as TextBox;
            TextBox oSGM = e.Row.FindControl("TxtPSGMPOP") as TextBox;

            oCsm.RegisterArrayDeclaration("TxtGrossSalesPOP", "'" + oGrossSales.ClientID + "'");
            oCsm.RegisterArrayDeclaration("TxtNetToGrossPOP", "'" + oNetToGross.ClientID + "'");
            oCsm.RegisterArrayDeclaration("TxtPSGMPOP", "'" + oSGM.ClientID + "'");                
        }
    }


Your Javascript Validation Function can do the Job like this

function ValidateDraftPostPromoGrid() {
    var rerunThis = false;
    var vFlag = 0;
    var oDisp = document.getElementById('divMessage'); // DIV, where you display the error message
    for (i = 0; i < TxtGrossSalesPOP.length; i++) {
        var oGrossSales = document.getElementById(TxtGrossSalesPOP[i])
        if (oGrossSales.value == "") {
            oGrossSales.className = "txtboxRedBorder";
            oDisp.innerHTML = "Post Promotion Details is Mandatory ";
            vFlag = 1;
            rerunThis = false;
            break;
        }
        else {
            if (isNaN(oGrossSales.value) == false) {
                oGrossSales.className = "txtboxNormal";
                oDisp.innerHTML = "";
                rerunThis = true;
            }
            else {
                oGrossSales.className = "txtboxRedBorder";
                oDisp.innerHTML = "Gross Sales to be entered in numbers ";
                vFlag = 1;
                rerunThis = false;
                break;
            }
        }
    }
return rerunThis;
}