Thursday 24 April 2014

Call method asynchronously using Delegate

        public delegate string tempDel(string str);
        protected void Page_Load(object sender, EventArgs e)
        {
            tempDel tdel = new tempDel(TestMethod);
            tdel.BeginInvoke("Ajay Patel", callback, tdel);
        }

        private void callback(IAsyncResult ar)
        {
            AsyncResult asynch = (AsyncResult)ar;
            tempDel tdel = asynch.AsyncDelegate as tempDel;
            string str = tdel.EndInvoke(ar); // this will return object,
        // you can then cast it to your return type
       }

        string TestMethod(string str)
        {
            return str;

        }

Tuesday 15 April 2014

Compile and execute C# code at runtime

Add this assemblies

using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.Reflection;


string source = @"namespace NSTest
                              {
                                public class clstest
                                 {
                                      public string SayMyName(string Name)
                                      {
                                          return Name;
                                      }
                                 }
                              }";
Dictionary<string, string> providerOptions = new Dictionary<string, string>
 {
     {"CompilerVersion", "v3.5"}
 };
CSharpCodeProvider provider = new CSharpCodeProvider(providerOptions);

CompilerParameters compilerParams = new CompilerParameters
{
    GenerateInMemory = true,
    GenerateExecutable = false
};

/// add assembly runtime
compilerParams.ReferencedAssemblies.Add("System.Drawing.dll");

// Compile code
CompilerResults results = provider.CompileAssemblyFromSource(compilerParams, source);

// Check if code has any error ??                       
if (results.Errors.HasErrors)
 {
      StringBuilder error = new StringBuilder();
      foreach (CompilerError item in results.Errors)
      {
          error.AppendLine(item.ErrorText);
       }
       throw new Exception(error.ToString());
 }

// create instance of class using Reflection
object objClass = results.CompiledAssembly.CreateInstance("NSTest.clstest");
// Find method
MethodInfo objMI = objClass.GetType().GetMethod("SayMyName");
// Invoke method

string str = objMI.Invoke(objClass, new object[] { "Ajay" }).ToString();




Monday 14 April 2014

Generic Repository with Object Context in Linq

Generic Repository with Object Context


Generic Class :

public class GenericRepository<T> : IDisposable where T : class
    {
        private XSharingEntities _ObjContext;
        IObjectSet<T> objSet;
        public GenericRepository(XSharingEntities context)
        {
            _ObjContext = context;
            objSet = _ObjContext.CreateObjectSet<T>();
        }

        public void Insert(T entity)
        {           
            if (entity != null)
            {
                objSet.Attach(entity);
                objSet.AddObject(entity);
            }
        }
        public void Delete(T entity)
        {
            if (entity != null)
            {
                objSet.DeleteObject(entity);
            }
        }
        public T getDetail(Expression<Func<T, bool>> where)
        {
            return objSet.Where(where).FirstOrDefault();
        }
        public IEnumerable<T> GetList()
        {
            return objSet.ToList();
        }
        public IEnumerable<T> GetList(Expression<Func<T,bool>> where)
        {

            return objSet.Where(where).ToList();
        }
        public void Save()
        {           
            _ObjContext.SaveChanges();
        }

        public void Dispose()
        {
            _ObjContext.Dispose();
            GC.SuppressFinalize(this);
        }
    }



Use generic class in our code


XSharingEntities objContext = new XSharingEntities();
public GenericRepository<SharingComment> objCFSCode
{
    get
    {

       GenericRepository<SharingComment> obj =
      new GenericRepository<SharingComment>(objContext);
      return obj;
    }
}

Do openrations in User Code
using (objCFSCode)
            {

                /// For Update
                var cmnt = objCFSCode.getDetail(p => p.CommentPK == 60217);               
                cmnt.Comment = "for Now";
                cmnt.CommentDate = DateTime.Now;
                cmnt.NotificationFK = 14;
                cmnt.UserFK = 1;
                objCFSCode.Save();

                // For Insert  
                SharingCFSCode obj = new SharingCFSCode();
                // SharingCFSCode is table
                obj.Application = "Test";
                obj.CFSCode = "AAAAAA";
                obj.CFSDescription = "This is Test CFS";
                obj.CFSPK = 123458;
                objCFSCode.Insert(cmnt);
                objCFSCode.Save();

                // for Delete
                objCFSCode.Delete(cmnt);
                objCFSCode.Save();
            }

Thursday 10 April 2014

Object Oriented Javascript

Hello,

Let’s see the implementation of object oriented programming in JavaScript :

      function students(FName, LName, CityName) { 
            this.City = CityName
            this.FirstName = FName;
            this.LastName = LName;
            this.GetName = function () {
                return this.FirstName + " " + this.LastName;
            }
        }
       
        // This method is also child method of "students" Method
        students.prototype.ChangeName = function (FName, LName) {
            this.FirstName = FName;
            this.LastName = LName;
        }

        function Check() {
            var stud = new students("Ajay", "Patel", "kalol");
            var GetFullName = stud.GetName();
            alert(GetFullName + " FROM " + stud.City);
            stud.ChangeName("A", "P");
            GetFullName = stud.GetName();
            alert(GetFullName + " FROM " + stud.City);


            // You will get other result for new object of Method
            var stud1 = new students("Hitesh", "Patel", "kalol");
            var GetFullName = stud1.GetName();
            alert(GetFullName + " FROM " + stud1.City);
            stud1.ChangeName("H", "P");
            GetFullName = stud1.GetName();
            alert(GetFullName + " FROM " + stud1.City); 

        }

Wednesday 9 April 2014

OFFSET FETCH in SQL Server



If you are given task to do custom paging then what you will do ?  you will write where condition and use row_number() function. But here SQL provide new way to get records as per our requirement for paging. Just write below query and pass start Record number and total record to get and you will gget your result :

SELECT * FROM  Table1
ORDER BY 1
OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY


Arguments Explanation :


OFFSET : Specifies the number of rows to skip, before starting to return rows from the query expression. The argument for the OFFSET clause can be an integer or expression that is greater than or equal to zero. You can use ROW and ROWS interchangeably.


FETCH (FIRST|NEXT): Specifies the number of rows to return, after processing the OFFSET clause. The argument for the FETCH clause can be an integer or expression that is greater than or equal to one. You can use ROW and ROWS interchangeably. Similarly, FIRST and NEXT can be used interchangeably



Why Should be a fan as you are star of tomorrow....

Thursday 3 April 2014

Treeview using Javascript

CSS  :

<style>
        body {
            font-family: Verdana, helvetica, arial, sans-serif;
            font-size: 0.8em;
        }

        .treeview, .treeview ul {
            padding: 0;
            margin: 0;
            list-style: none;
        }

            .treeview ul {
                background-color: white;
                margin-top: 4px;
            }

            .treeview .hitarea {
                background: url(image/treeview-default.gif) -64px -25px no-repeat;
                height: 16px;
                width: 16px;
                margin-left: -16px;
                float: left;
                cursor: pointer;
            }
        /* fix for IE6 */
        * html .hitarea {
            display: inline;
            float: none;
        }

        .treeview li {
            margin: 0;
            padding: 3px 0pt 3px 16px;
        }

        .treeview li {
            background: url(image/TrLine.gif) 0 0 no-repeat;
        }

            .treeview li.collapsable, .treeview li.expandable {
                background-position: 0 -174px;
            }

        .treeview .expandable-hitarea {
            background-position: -80px -3px;
        }

        .treeview li.last {
            background-position: 0 -1763px;
        }

        .filetree span.folder, .filetree span.file {
            padding: 1px 0 1px 22px;
            display: inline;
        }

        .filetree span.folder {
            background: url(image/folder.gif) 2px 0 no-repeat;
        }

        .filetree span.file {
            background: url(image/inbox.gif) 2px 0 no-repeat;
        }

        .lastIcon {
            background-position: -48px -45px !important;
        }

        .click:hover {
            cursor: pointer;
        }
    </style>

Javascript :
<script>
        $(document).ready(function () {
            $(".click").click(function () {
                $(this).next().slideToggle(200);
            })
        })
    </script>

HTML :

<div style="width:400px;">
            <ul id="browser" class="filetree treeview">
                <li class="closed expandable">
                    <div class="click">
                        <div class="hitarea closed-hitarea expandable-hitarea"></div>
                        <span class="folder">Folder 1</span>
                    </div>
                    <ul style="display: none;">
                        <li class="last"><span class="file">Item 1</span></li>
                    </ul>
                </li>
                <li class="collapsable">
                    <div class="click">
                        <div class="hitarea collapsable-hitarea"></div>
                        <span class="folder">Folder 2</span>
                    </div>
                    <ul>
                        <li class="last"><span class="file">Item 1</span></li>
                    </ul>
                </li>
                <li class="collapsable">
                    <div class="click">
                        <div class="hitarea collapsable-hitarea"></div>
                        <span class="folder">Folder 3</span>
                    </div>
                    <ul>
                        <li class="collapsable">
                            <div class="click">
                                <div class="hitarea collapsable-hitarea"></div>
                                <span class="folder">Subfolder 1</span>
                            </div>
                            <ul id="folder21" style="display: block;">
                                <li><span class="file">Item 1</span></li>
                                <li class="last"><span class="file">Item 2</span></li>
                            </ul>
                        </li>
                        <li class="last"><span class="file">Item 1</span></li>
                    </ul>
                </li>
                <li class="last closed expandable">
                    <div class="click">
                        <div class="lastIcon hitarea closed-hitarea expandable-hitarea"></div>
                        <span class="folder">Folder 4</span>
                    </div>
                    <ul style="display: none;">
                        <li class="last"><span class="file">Item 1</span></li>
                    </ul>
                </li>

            </ul>

        </div>


Download source code from here