Wednesday 25 September 2013

Fix the Filter on top while scrolling the page


Hello All,
Some times we need to fix the filter on the top of the screen at the time we scroll the page. for that simply we need to change the css of that div in which we placed the filter. I have also fixed the header of the gridview.
For this code as follow

<script type="text/javascript">
        jQuery(function ($) {
           
            $(window).scroll(fixDiv);
            fixDiv();
        });
        function fixDiv() {
            var $cache = $('#getFixed');
            var $grid = $('#gvImage .FrozenHeader');
            if ($(window).scrollTop() > 80) {
                $cache.css({ 'position': 'fixed', 'top': '0px', 'padding-top': '10px', 'width': '1122px', 'margin': '0px 0px 0px 0px', 'border-bottom': '20px solid gray', 'border-top': '10px solid gray' });
                $grid.css({ 'position': 'fixed', 'top': '60px', 'left': '8px', 'width': '620px', 'padding-top': '0px', 'margin-top': '0px' });
            }
            else {
                $cache.css({ 'position': 'relative', 'width': '100%', 'top': '10px', 'padding-top': '10px' });
                $grid.css({ 'position': 'relative' });
            }
        }
</script>


and code for my filter and gridview is

<div id="getFixed" style="background-color: Yellow; height: 30px; top: 10px;">
            Filter :        
            <asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
<asp:Button id="btnSearch" runat="server" Text="Search" />
</div>


<asp:GridView ID="gvImage" runat="server" AutoGenerateColumns="false" ClientIDMode="Static" BackColor="White">
                            <HeaderStyle CssClass="FrozenHeader" />
                            <Columns>
                                <asp:TemplateField HeaderText="Sr No." HeaderStyle-HorizontalAlign="Center">                                   
                                    <HeaderStyle Width="100" />
                                    <ItemStyle HorizontalAlign="Center" Width="100" />
                                    <ItemTemplate>
                                        <asp:Label ID="lblID" CssClass="lblID" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>
                            </Columns>
                            <Columns>
</asp:GridView>
 

Friday 20 September 2013

Configure Log4Net in your ASP.NET Web Application

Hello Friends,

So manytimes we have to get the error logs comes at the time of production so at that time we don't able to find the exac error in the system. It will be tough if you try to find that bug from overall proejct. You need to have log4net.dll for this after adding reference to the project you can process for the next step. so first Download the dll file and give reference to your project.

First of all create an SQL Table for the log use following script to create table

CREATE TABLE [dbo].[GEN_Log](
 [Id] [bigint] IDENTITY(1,1) NOT NULL,
 [Date] [datetime] NOT NULL,
 [Thread] [varchar](255) NOT NULL,
 [Level] [varchar](50) NOT NULL,
 [Logger] [varchar](255) NOT NULL,
 [Message] [varchar](8000) NOT NULL,
 [Exception] [varchar](8000) NULL
)


Now Put the following Code in the <Confuguration>, Change the connection string as per your suggestion.

<log4net>
    <root>
      <level value="ALL" />
      <appender-ref ref="ADONetAppender" />
    </root>
    <appender name="ADONetAppender"
type="log4net.Appender.ADONetAppender">
      <bufferSize value="1" />
      <connectionType
value="System.Data.SqlClient.SqlConnection, System.Data,
Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089" />
      <connectionString value="server=10.5.4.31; uid=admin;
pwd=password; database=ProjectDB" />
      <commandText value="INSERT INTO GEN_Log ([Date],
[Thread], [Level], [Logger], [Message], [Exception]) VALUES
(@log_date, @thread, @log_level, @logger, @message,
@exception)" />
      <parameter>
        <parameterName value="@log_date" />
        <dbType value="DateTime" />
        <layout type="log4net.Layout.RawTimeStampLayout" />
      </parameter>
      <parameter>
        <parameterName value="@thread" />
        <dbType value="String" />
        <size value="255" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%thread" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@log_level" />
        <dbType value="String" />
        <size value="50" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%level" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@logger" />
        <dbType value="String" />
        <size value="255" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%logger" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@message" />
        <dbType value="String" />
        <size value="8000" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%message" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@exception" />
        <dbType value="String" />
        <size value="8000" />
        <layout type="log4net.Layout.ExceptionLayout" />
      </parameter>
    </appender>
  </log4net>


Next Put the following code in <configSections>

<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net" />


Now In Global.asax file
 void Application_Start(object sender, EventArgs e)
        {
            // Code that runs on application startup
            XmlConfigurator.Configure();
        }


In the page where you want to get the error log first create the object like the following code.

private static readonly ILog log = LogManager.GetLogger(typeof(PageName));


and then use this code to get exceptions in the catch part

log.Error(ex.InnerException != null ? string.Format("Page Load : Inner Exception: {0} --- Exception: {1}", ex.InnerException.Message, ex.Message) : string.Format("Page Load : {0}",ex));