Thursday, October 4, 2018

Published 6:03 AM by with 0 comment

Sealed Classes

Sealed Class In C#:

What is Sealed Class?

In some situation we don't want to inherited  a base class, So for that purpose we declared it a sealed class.

    public sealed class baseclass 
{


 } 
Read More
      edit
Published 5:25 AM by with 0 comment

Polymorphism In C#

Polymorphism:

An object contains many forms/types.

Compile time Polymorphism(its also Early binding, overloading, static polymorphism)
Compile time Polymorphism is also known as method overloading. Method overloading means having two or more methods with the same name but with different signatures.
Run time Polymorphism(its also Late binding, overriding, dynamic polymorphism)
Run time Polymorphism is also known as method overriding. Method overriding means having two or more methods with the same name and same signature, but with a different implementation.

Overloading: Two or more method have same name but different signatures(parameters)
Overriding: Two or more method have same name and signatures(parameters) but implimentations is different.
Read More
      edit
Published 2:41 AM by with 0 comment

What is IndexOutOfRangeException?


What is System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection OR IndexOutOfRangeException?

Solution:

1.What Is meant of ArgumentOutOfRangeException OR IndexOutOfRangeException?



In C#,When you try to access a collection of item in an array through using an invalid index  it say IndexOutOfRangeException. 

When an index is invalid?

An index of an array is invalid when its lower that the collection's lower bound or greater than or equal to the number of items the collection contains.

when it throw an IndexOutOfRangeException?

Given an array is declares as:

            
var idList = new int[4] { 8, 10, 11, 12 };

The amount in the square bracket is index/es.it range is 4 means you access this array from 0 to 3.and  8,10,11,12 are the items it contain, when you try to access this value outside this range it will through exception IndexOutOfRangeException. So remember this when you try to access any array.Range/Length of arrays:
In C#, arrays range is start from 0 index and its end on range-1(where the range is total number of items an array contains ).
so this code will not work :

Itemlist = idList[10]
because indexes range is 4 and we call 10.





Read More
      edit

Wednesday, September 19, 2018

Published 2:16 AM by with 0 comment

AspNet Core 2.1- Services/Repostory Pattern

Service /Repository Pattern:

What Is Services\Repository Pattern:

    public class StudentClassService : IRepository<STD_CLASS>, IDisposable
    {

        private readonly _DbContext _DbContext;
        public StudentClassService(_DbContext dbContext)
        {
            _DbContext = dbContext;
        }

        public void Add(STD_CLASS entity)
        {
            _DbContext.STDCLASS.Add(entity);
            _DbContext.SaveChanges();
        }

        public async Task<bool> AddAsync(STD_CLASS entity)
        {
            await _DbContext.STDCLASS.AddAsync(entity);
            await _DbContext.SaveChangesAsync();
            return true;
        }

        public async Task<bool> AddRangeAsync(IEnumerable<STD_CLASS> entity)
        {
            await _DbContext.STDCLASS.AddRangeAsync(entity);
            await _DbContext.SaveChangesAsync();
            return true;

        }

        public void Delete(STD_CLASS entity)
        {
            _DbContext.Entry(entity).State = EntityState.Modified;
            _DbContext.SaveChanges();
        }

        public async Task<bool> DeleteAsync(STD_CLASS entity)
        {
            _DbContext.Entry(entity).State = EntityState.Modified;
            await _DbContext.SaveChangesAsync();
            return true;
        }

        public void DeleteByFlag(STD_CLASS entity)
        {
            throw new NotImplementedException();
        }

        public void DeleteById(int id)
        {
            throw new NotImplementedException();
        }

        public Task<bool> DeleteRange(STD_CLASS entity)
        {
            throw new NotImplementedException();
        }

        public void DeleteRange(IEnumerable<STD_CLASS> entity)
        {
            throw new NotImplementedException();
        }

        public void Dispose()
        {
            _DbContext.Dispose();
        }

        public STD_CLASS Get(int id)
        {
            return _DbContext.STDCLASS.Where(c => c.Id == id && c.IsDeleted == false).FirstOrDefault();
        }
        public STD_CLASS Get(string Name)
        {
            return _DbContext.STDCLASS.Where(c => c.ClassName.ToLower() == Name.ToLower() && c.IsDeleted == false).FirstOrDefault();
        }

        public IEnumerable<STD_CLASS> GetAll()
        {
            return _DbContext.STDCLASS.Where(c => c.IsDeleted==false).ToList();
        }

        public void Update(STD_CLASS entity)
        {
            _DbContext.Entry(entity).State = EntityState.Modified;
            _DbContext.SaveChanges();

        }

        public async Task<bool> UpdateAsync(STD_CLASS entity)
        {
            _DbContext.Entry(entity).State = EntityState.Modified;
            await _DbContext.SaveChangesAsync();
            return true;
        }
    }

Read More
      edit
Published 2:13 AM by with 0 comment

AspNet Core MVC 2.1-Delete Action Method


In Asp.net Core the Delete Action is work as follows:

You Need To Create  Get or Post Method


Get Action:


        public async Task<IActionResult> Delete(int? id)
        {
            if (id== null)
            {
                return NotFound();
            }
            var s = await _dbContext.STUDENTINFO.AsNoTracking()
                .FirstOrDefaultAsync(m=>m.Id ==id);
            if(s == null)
            {
                return NotFound();
            }
            //if(saveChangesError.GetValueOrDefault)
            return View();
        }

Post Method:

[HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Delete(int? id, IFormCollection collection)
        {
            if (id == null)
            {
                return NotFound();
            }
            var s = await _dbContext.STUDENTINFO.AsNoTracking()
                .FirstOrDefaultAsync(m => m.Id == id);

            if (s == null)
            {
                return NotFound();
            }
            try
            {
                // TODO: Add delete logic here
                //var d= await _dbContext.STUDENTINFO.FindAsync(id);
                s.UpdatedBy = 1;
                s.UpdatedDateTime = DateTime.Now;
                await _studentInfoService.DeleteAsync(s);
                Flag = !Flag;
                return RedirectToAction(nameof(Index));
                
            }
            catch
            {
                return View();
            }
        }

Here you need to create a service pattern or Repository pattern but Its Optional
Remember:Repository Pattern is optional .

Read More
      edit
Published 2:00 AM by with 0 comment

About Us

About Us:
Hi, My name is Muhammad Abdullah. I'm Fulltime Blogger.
Programming Forum is a bloging forum where is share Code,Project,Problem Solution faced by me and other developers.
I also Invite you to write post freely.

I'm full-time Asp.net core Developer
Read More
      edit
Published 1:51 AM by with 0 comment

Privacy

Privacy Policy:
  • The Post you see on this blog are all unique and not copy by another place.
  • Information We take
    We may take information about you for just information purpose or to sent you notifation about the updates
    We may collect your information like( Name,Email ,Country ,Location).
    The Information we collect will not be shared by any Third Person.
  • You can delete your date any time when you want.
Read More
      edit