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;
}

Tuesday, August 31, 2010

Grouping/Appending GridView Header

GridView header to have a Parent Header(Grouped Header) is as simple as the following.







protected void
GrvPrePromotion_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            GridView HeaderGrid = (GridView)sender;
            GridViewRow HeaderGridRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
            TableCell HeaderCell = new TableCell();
            HeaderCell.Text = "Promotion Product Group";
            HeaderCell.ColumnSpan = 3;
            HeaderGridRow.Cells.Add(HeaderCell);
            HeaderGridRow.CssClass = "gridheadtop";

            HeaderCell = new TableCell();
            HeaderCell.Text = "Sales and SGM";
            HeaderCell.ColumnSpan = 4;
            HeaderGridRow.Cells.Add(HeaderCell);

            HeaderCell = new TableCell();
            HeaderCell.Text = "Special Discount";
            HeaderCell.ColumnSpan = 3;
            HeaderGridRow.Cells.Add(HeaderCell);

            HeaderCell = new TableCell();
            HeaderCell.Text = "Rebate";
            HeaderCell.ColumnSpan = 1;
            HeaderGridRow.Cells.Add(HeaderCell);

            HeaderCell = new TableCell();
            HeaderCell.Text = "Free Goods / Prize";
            HeaderCell.ColumnSpan = 1;
            HeaderGridRow.Cells.Add(HeaderCell);

            GrvPrePromotion.Controls[0].Controls.AddAt(0, HeaderGridRow);
        }
    }

 How to Display/Calculate Total/Percentage in the Gridview footer.

Rounding a number to decimal places, Javascript.

As Javascript's Math.Round() cant be directly used to round to decimal places, we should find the workarounds.
Have a look at the following function which does the job.

function RoundNumber(ThisNumber, rlength) { // Arguments: number to round, number of decimal places
  var RoundedNumber = Math.round(ThisNumber*Math.pow(10,rlength))/Math.pow(10,rlength);
  return RoundedNumber ; // returns the resulting number. 
}

Friday, August 27, 2010

WebConfig settings for Sharepoint (WSS and MOSS Portals)

Get Microsoft ASP.NET 2.0 AJAX Extensions 1.0 running on the WFE (WHere the Instance of your WSS/MOSS is running. Open up the Web.config of your WebSite from C:/INETPUB/VIRTUAL DIRECTORIES/WSS/(PORT NUMBER OF YOUR SITE) and do some settings which are required for AJAX to run on your Portal. Web.Config wont have Ajax settings in it even if AJAX is installed on the Server.

FOR WSS

FOR MOSS

ASP.Net Application to run on IIS 7.0

Upgrading an ASP.NET application from IIS 6.0 or lower version to IIS 7.0 will have a lot of work. Microsoft provides an application that does the settings for your application to run on IIS 7.0. [Classic ASP.NET Integration Mode to IIS 7.0 Integrated Mode]

IIS 7.0 takes care of migrating the application by using the APPCMD.EXE command line tool to perform the migration. The migration error message contains the command that is executed in command line window (which you must run--right click the Programs\Accessories\Command Prompt icon, and choose "Run as administrator") in order to instantly migrate your application to Integrated mode.

The format of the migration command is :

%windir%\system32\inetsrv\APPCMD.EXE migrate config

where Application Path is the virtual path of the application containing the site name, such as "Default Web Site/app1"