Friday, June 21, 2013

Url rewriting gives me 404 - File or directory not found

This code will work in ASP.Net 3.0 and higher versions

 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <remove name="UrlRoutingModule"/>
      <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    </modules>
    <handlers>
      <add name="UrlRoutingHandler" preCondition="integratedMode" verb="*" path="UrlRouting.axd" type="System.Web.HttpForbiddenHandler, System.Web,
Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a"/>
    </handlers>       
    </system.webServer> 

Friday, May 31, 2013

vbLf alternate in C#

You can
vbCr = "\r"
vbLf = "\n"
vbCrLf = "\r\n"

Read HTML file in ASP.Net C#

  private void LoadStatement()
         {
         try
         {
            //Open a file for reading
            string FILENAME= Server.MapPath("ics.htm");
            StreamReader objStreamReader = File.OpenText(FILENAME);
            string contents= objStreamReader.ReadToEnd();
            objStreamReader.Close();
            contents = contents.Replace("<br /><br />", "\n");
            contents = contents.Replace("<br />", "\n");
            contents = RemoveHTML(contents);
            string strAgreement = "Independent Contractor Statement \r\n \r\n"+ contents;
            txtAgreement.Text = strAgreement;
         }
        catch{}
         }

Click here to see code in VB

Read HTML file in asp.net VB

 Protected Sub LoadStatement()

        Try
            'Open a file for reading
            Dim FILENAME As String = Server.MapPath("agreement.htm")

            'Get a StreamReader class that can be used to read the file
            Dim objStreamReader As StreamReader
            objStreamReader = File.OpenText(FILENAME)

            'Now, read the entire file into a string
            Dim contents As String = objStreamReader.ReadToEnd()
            objStreamReader.Close()
            contents = contents.Replace("<br /><br />", vbCrLf)
            contents = contents.Replace("<br />", vbCrLf)
            contents = RemoveHTML(contents)
            Dim strAgreement As String
            strAgreement = "Independent Contractor Agreement" & vbCrLf & vbCrLf & contents

            txtAgreement.Text = strAgreement
        Catch ex As Exception

        End Try

Click here to see code in C# 

Thursday, May 30, 2013

RegisterForEventValidation can only be called during Render() ASP.NET

The prefect solution is use EnableEventValidation="false" in Page tag.

<%@ Page Title="" Language="C#" MasterPageFile="MasterPage.master" AutoEventWireup="true" EnableEventValidation="false"
    CodeFile="checkout.aspx.cs" Inherits="LD.checkout" %>

 private string RenderGridView()
        {
            string returnvalue = string.Empty;
            try
            {
                StringWriter stringWrite = new StringWriter();
                HtmlTextWriter writer = new HtmlTextWriter(stringWrite);
                VerifyRenderingInServerForm(GridView2);
                GridView2.RenderControl(writer);
                returnvalue = writer.InnerWriter.ToString();
            }
            catch (Exception ex)
            {

            }
            return returnvalue;
        }
        public override void VerifyRenderingInServerForm(Control control)
        {
            return;
        }

Wednesday, May 22, 2013

Auto Update A datagrid/Grdiview by Timer in asp.net C#

 using System.Data.SqlClient;
using System.Data;

 protected void Timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                BindGridView();
            }

            catch { }
        }
        #region Bind datagrid Grdiview
        private void BindGridView()
        {
            SqlDataAdapter adp = new SqlDataAdapter("Select * from TableName", mySqlConnection);
            DataSet ds = new DataSet();
            adp.Fill(ds);
            try
            {
                DataView dv = new DataView(ds.Tables[0]);
                gdvListing.DataSource = dv;
                gdvListing.DataBind();
                if (gdvListing.Rows.Count <= 0)
                {
                    ltrErrorMsg.Text = "No Record Found.";
                }
            }
}

Click here to see ASPX html and Gridview Binding

How auto update a DataGrid in 1 seconds without refreshing page in asp.net

<asp:UpdatePanel runat="server" id="TimedPanel" updatemode="Conditional">
    <Triggers>
        <asp:AsyncPostBackTrigger controlid="UpdateTimer" eventname="Tick" />
        <%--<asp:PostBackTrigger ControlID="btnSubmit" />--%>
    </Triggers>
    <ContentTemplate>
     <asp:Timer runat="server" id="UpdateTimer" interval="2000"  OnTick="Timer1_Tick"  />
                        <asp:GridView Width="100%" ID="gdvListing" runat="server" GridLines="none" BackColor="#E6DCC3" CssClass="Forummainhead"
                            DataKeyNames="ProductId" AutoGenerateColumns="False" AllowPaging="True"
                            OnPageIndexChanging="gdvListing_PageIndexChanging" OnRowDataBound="gdvListing_RowDataBound"
                             CellSpacing="1" CellPadding="2" BorderWidth="0" EmptyDataRowStyle-Font-Bold="true">
                            <HeaderStyle HorizontalAlign="center" CssClass="Grid_HeaderStyle"   />
                            <RowStyle HorizontalAlign="left" CssClass="row0" />
                            <AlternatingRowStyle HorizontalAlign="left" CssClass="row1" />
                            <Columns>
                               <asp:TemplateField HeaderText="Product Name" HeaderStyle-HorizontalAlign="left">
                                    <ItemTemplate>
                                        <a href="ProductAddEdit.aspx?ID=<%#DataBinder.Eval(Container.DataItem, "ProductId") %>" >
                                            <%# DataBinder.Eval(Container.DataItem, "Name")%>
                                        </a>
                                    </ItemTemplate>
                                    <ItemStyle HorizontalAlign="left" />
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="User Name" HeaderStyle-HorizontalAlign="left">
                                    <ItemTemplate>
                                         <%# DataBinder.Eval(Container.DataItem, "UserName")%>
                                    </ItemTemplate>
                                    <ItemStyle HorizontalAlign="left" />
                                </asp:TemplateField>
                                 <asp:TemplateField HeaderText="Current bid Amount" HeaderStyle-HorizontalAlign="left">
                                    <ItemTemplate>
                                        <%# DataBinder.Eval(Container.DataItem, "CurrentBidAmount")%>
                                    </ItemTemplate>
                                    <ItemStyle HorizontalAlign="left" />
                                </asp:TemplateField>
                                  <asp:TemplateField HeaderText="Bid Min. Amount" HeaderStyle-HorizontalAlign="left">
                                    <ItemTemplate>
                                        <%# DataBinder.Eval(Container.DataItem, "BidMinValue")%>
                                    </ItemTemplate>
                                    <ItemStyle HorizontalAlign="left" />
                                </asp:TemplateField>
                                 <asp:TemplateField HeaderText="MRP" HeaderStyle-HorizontalAlign="left">
                                    <ItemTemplate>
                                        <%# DataBinder.Eval(Container.DataItem, "MRP")%>
                                    </ItemTemplate>
                                    <ItemStyle HorizontalAlign="left" />
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Sold" HeaderStyle-HorizontalAlign="left">
                                    <ItemTemplate>
                                        <%# DataBinder.Eval(Container.DataItem, "IsSold")%>
                                    </ItemTemplate>
                                    <ItemStyle HorizontalAlign="left" />
                                </asp:TemplateField>
                               
                               
                                </Columns>
                            <PagerSettings Mode="NumericFirstLast" Position="Bottom" />
                            <EmptyDataRowStyle CssClass="row0" Font-Underline="False" HorizontalAlign="center"
                                VerticalAlign="Middle" Width="100%" />
                            <PagerStyle CssClass="form_header" Font-Bold="True" ForeColor="#FFFFFF" HorizontalAlign="Right" />
                        </asp:GridView>   
                        </ContentTemplate></asp:UpdatePanel>

Click Here to view ASPX.CS file code