Monday, 15 December 2014

MVC - Entity Framework using Connection Extension (Insert,update,selet,Delete)

MVC - Entity Framework using Connection Extension (Insert,update,selet,Delete)

MVC - Entity Framework using Connection Extension (Insert,update,selet,Delete)


DB Context


using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using API.Models;

namespace API.Database
{
    public class APIDbContext: DbContext
    {
        public APIDbContext()
            : base("APIDbContext")
        { }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }
        public override int SaveChanges()
        {
            try
            {
                return base.SaveChanges();
            }
           
            catch (Exception e)
            {
                //Debug.WriteLine(e.Message);
                throw;
            }
        }
        public DbSet<User> Users { get; set; }
       
      
    }
}

Connection Extension:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Web;
using System.Data.Entity;
using API;


namespace API.Database
{
    public static class ConnectionExtensions
    {

//Save

       public static TEntity Save<TEntity>(this APIDbContextcontext, TEntity entity) where TEntity : class
        {
            try
            {
                context.Entry(entity).State = System.Data.Entity.EntityState.Added;
                context.SaveChanges();
               
            } catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
                                entity = null;
            }
            return entity;
        }

//Update

        public static TEntity Update<TEntity>(this APIDbContextcontext, TEntity entity) where TEntity : class
        {
            try
            {
                context.Entry(entity).State =           System.Data.Entity.EntityState.Modified;
                context.SaveChanges();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                
                entity = null;
            }
            return entity;
        }

//Delete

        public static TEntity Delete<TEntity>(this APIDbContextcontext, TEntity entity) where TEntity : class
        {
            try
            {
                context.Entry(entity).State = System.Data.Entity.EntityState.Deleted;
                context.SaveChanges();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                
                entity = null;
            }
            return entity;
        }


//DeleteById


        public static TEntity DeleteById<TEntity>(this APIDbContextcontext, long id) where TEntity : class, IDataModel
        {
            TEntity entity;
            try
            {
                var q = from o in context.Set<TEntity>()
                        where o.id == id
                        select o;
                entity = q.SingleOrDefault();
                context.Entry(entity).State = System.Data.Entity.EntityState.Deleted;
                context.SaveChanges();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
               return null;
            }
            return entity;
        }

//Get


        public static TEntity Get<TEntity>(this APIDbContextcontext, long id) where TEntity : class, IDataModel
        {
            TEntity entity;
            try
            {
                var q = from o in context.Set<TEntity>()
                        where o.id == id
                        select o;
                entity = q.SingleOrDefault();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                                return null;
            }
            return entity;
        }

//GetBy(this APIDbContextcontext, Expression<Func<TEntity, object>> field, string value)

public static TEntity GetBy<TEntity>(this APIDbContextcontext, Expression<Func<TEntity, object>> field, string value) where TEntity : class, IDataModel
        {
            IQueryable<TEntity> query = context.Set<TEntity>();
            query.Include<TEntity, object>(field);
            TEntity t = query.SingleOrDefault<TEntity>();
            return t;
        }


//GetBy(this APIDbContextcontext, Expression<Func<TEntity, object>> field, long value)

public static TEntity GetBy<TEntity>(this APIDbContextcontext, Expression<Func<TEntity, object>> field, long value) where TEntity : class, IDataModel
        {
            IQueryable<TEntity> query = context.Set<TEntity>();
            query.Include<TEntity, object>(field);
            TEntity t = query.SingleOrDefault<TEntity>();
            return t;
        }

//GetBy(this APIDbContextcontext, Expression<Func<TEntity, bool>> expr)


public static TEntity GetBy<TEntity>(this APIDbContextcontext, Expression<Func<TEntity, bool>> expr) where TEntity : class, IDataModel
        {
            TEntity t;
            try
            {
                IQueryable<TEntity> query = context.Set<TEntity>();
                t = query.Where<TEntity>(expr).SingleOrDefault<TEntity>();
                
            } catch (Exception ex)
            {
                t = null;
            }
            return t;
        }

//FetchBy

        public static IEnumerable<TEntity> FetchBy<TEntity>(this APIDbContextcontext, Expression<Func<TEntity, object>> field, object value) where TEntity : class, IDataModel
        {
            IQueryable<TEntity> query = context.Set<TEntity>();
            query.Include<TEntity, object>(field);
            return query.ToList<TEntity>();
        }

//FetchBy(this APIDbContextcontext, Expression<Func<TEntity, bool>> expr)

        public static IEnumerable<TEntity> FetchBy<TEntity>(this APIDbContextcontext, Expression<Func<TEntity, bool>> expr) where TEntity : class, IDataModel
        {
            
            IQueryable<TEntity> query = context.Set<TEntity>();
            IEnumerable<TEntity> t = query.Where<TEntity>(expr).ToList<TEntity>();
            return t;
           
        }



    }
}




Monday, 10 November 2014

MVC Check value Select insert using EntityFramework

MVC Check value Select insert using EntityFramework :


Controller :

 var skill = Skill.GetOrCreate(Db, skillData.skill.name);
                           
                            TGDb.Save<UserSkill>(new UserSkill
                            {
                                user_id = userid,
                                skill_id = skill.id,
                                priority = 10,
                            });


skill Models :

using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel.DataAnnotations.Schema;
using API.Database;


namespace API.Models
{
    public class Skill : IDataModel
    {
        public long id { get; set; }
        public string name { get; set; }

        public static Skill GetOrCreate(DbContext Db, string name)
        {
  //Select The Value
            var query = from s in Db.skills -->DBContext 
                        where s.name == name
                        select s;

            var skill = query.SingleOrDefault();


            
            if (skill == null)
            {
               
               
                 var newSkill=new Skill();
                 newSkill.name = name;
//Insert The Value
                skill = TGDb.Save<Skill>(newSkill);
            }
            return skill;
        }
    }

}


DB Context :
 
Add -->
 public DbSet<Skill> skills { get; set; }


MVC Entity Framework

MVC Entity Framework :


i)    Create MVC Empty Project.
ii)    Refrences Add in EntityFramework.




iii)    Create Folder Database.. using Database Context and Connections..

Models :  
Models Folder -- > Create New model in (Employee.cs)


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace FirstMVC.Models
{
    public class Employee
    {
        public int id { get; set; }
        public string name { get; set; }
    }
}  

Controller :

Controller Folder -- > Create New Controller in (HomeController.cs)


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
//NameSpace add EntityFramework
using System.Data.Entity;
 //NameSpace add Models and Database
using FirstMVC.Models;
using FirstMVC.Database;


namespace FirstMVC.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            ViewBag.Message = "Your Welcome Page";
            return View();
        }
       
       }
}


View:

Created Automatic (Controll Name FolderName )Home Folder and Index.cshtml

Index.cshtml :

@{
    ViewBag.Title = "Index";
   
}

<h2>Index</h2>
<div>
    @ViewBag.Message
    aaa
</div>


Connection String in Webconfig:

DB Context Name:MCVDbContext
<connectionStrings>
    <add name="MCVDbContext" connectionString="Data Source=Vijay;Initial Catalog=Sample;Integrated Security=False;User ID=sa;Password=sql" providerName="System.Data.SqlClient" />
  </connectionStrings>

DBContext :
 i)  DataBase Folder Create New Class.(MCVDbContext.CS)
 ii) Class Name(MCVDbContext.CS) and Connection string name are        
      same ((MCVDbContext.CS)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.Entity;
using FirstMVC.Models;

namespace FirstMVC.Database
{
    public class MCVDbContext : DbContext
    {
        //public class MVCDBContext:
       
    }
}


Insert The Values :

Controller wia insrting values:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.Entity;
using FirstMVC.Models;
using FirstMVC.Database; //Db Connection


namespace FirstMVC.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
          //Db Connection
            MCVDbContext objDB = new MCVDbContext();
            Employee Emp = new Employee
            {
                name = "vijay",
            };

            //Save DB
            objDB.Employees.Add(Emp); --> (MVCDbContextDBContext DBSet Name
            objDB.SaveChanges();
            return View();
        }
    }
}



Database Folder :

MCVDbContext.CS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.Entity;
using FirstMVC.Models;

namespace FirstMVC.Database
{
    public class MCVDbContext : DbContext
    {
        //public class MVCDBContext:
        public DbSet<Employee> Employees { get; set; }
    }
}


SQL :

Table Names and (Models -> Employee.cs ) Same and Fields Name.

Create Table Employee
(
     id Int Primary Key IDENTITY(1,1), 
     name Varchar(100)
)










Wednesday, 3 September 2014

visual studio expired resolve

visual studio expired resolve :

visual studio expired resolve 



 Step 1 :  
             go to run > type regedit.

 Step 2 : 
             registry editor wil open.

Step 3 :  
             Remove the registry key containing the license information: HKEY_CLASSES_ROOT\Licenses\77550D6B-6352-4E77-9DA3-537419DF564B.

Step 4 : 
              If you can't find the key, use sysinternals ProcessMonitor to check the registry access of VS2012 to locate the correct key which is always in HKEY_CLASSES_ROOT\Licenses.

Step 5 :
              After you remove this key, VS2012 will tell you that it's license information is incorrect. Go to "Programs and features" and repair VS2012.

After the repair, VS2012 is reverted to a 30 day trial and you can enter a new product key. This could also be used to stay in a trial version loop and never enter a producy key.

Friday, 22 August 2014

run exe with parameter from asp.net c#

run exe with parameter from asp.net c#  :

run exe with parameter from asp.net c# 

aspx:cs

public void RunEXE()
        {
            System.Diagnostics.Process process1 = new System.Diagnostics.Process();

            process1.StartInfo.UseShellExecute = true;
//Exe Name
            process1.StartInfo.FileName = @"Sample.exe";
//Path Name
            process1.StartInfo.WorkingDirectory = @"E:\vijay\Sample\Bin\";
//Parameter Name
            process1.StartInfo.Arguments = @"E:\vijay\Sample\Bin\"+ "Test.txt";
            process1.Start();

            process1.WaitForExit();

            Thread.Sleep(20000);//Assume within 20 seconds it will finish processing. 
            process1.Close();
        }

run exe in asp.net c#

run exe in asp.net c# :

run exe in asp.net c#

aspx:cs

 public void RunEXE()

        {
            System.Diagnostics.Process process1 = new System.Diagnostics.Process();

            process1.StartInfo.UseShellExecute = true;
//Exe Name
            process1.StartInfo.FileName = @"Sample.exe";
//Path Name
            process1.StartInfo.WorkingDirectory = @"E:\vijay\Sample\Bin\";
            
            process1.Start();

            process1.WaitForExit();

            Thread.Sleep(20000);//Assume within 20 seconds it will finish processing. 
            process1.Close();
        }

folder all images get in asp.net

Folder all images get in asp.net  :

Folder all images get in asp.net  :

aspx:cs 

public void getAllImages()
        {
            string SourceLocation;
    //Image Folder
            SourceLocation="D:\Projects\Sample\source";
            DirectoryInfo dir = new DirectoryInfo(SourceLocation);
            FileInfo[] file = dir.GetFiles();

            StringBuilder sb = new StringBuilder();
            StringBuilder sbFrontImageName = new StringBuilder();
            StringBuilder sbbakImageName = new StringBuilder();
            bool frontImgName, bakImageName;
            foreach (FileInfo file2 in file)
            {
                if (file2.Extension == ".tif")
                {
                    string fname = file2.ToString();
  //Finding Image Name Front or Back Using Contains 
 //Sample_front_4.tif
 //Sample_back_4.tif
                    frontImgName = fname.Contains("front");
                    bakImageName = fname.Contains("back");

                    string imgName = file2.ToString();

                    if (frontImgName == true)
                    {
                        sbFrontImageName.AppendLine(SourceLocation + "\\" + fname);
                       

                    }
                    else
                    {

                        bakImageName = fname.Contains("back");
                        if (bakImageName == true)
                        {
                            sbbakImageName.AppendLine(SourceLocation + "\\" + fname);
                        }
                    }
                }
            }
            string ImgFrontTestINP, ImgBackTestINP;
            ImgFrontTestINP = SourceLocation + "\\" + "Test.inp";
            ImgBackTestINP = SourceLocation + "\\" + "Test_back.inp";

            System.IO.File.WriteAllText(ImgFrontTestINP, sbFrontImageName.ToString());
            System.IO.File.WriteAllText(ImgBackTestINP, sbbakImageName.ToString());
        }


Writting File :

Front Image Name written NotePad Test.inp:
----
Sample_front_1.tif
Sample_front_2.tif
Sample_front_3.tif
Sample_front_4.tif
Sample_front_5.tif

Back Image Name written NotePad Test_bak.inp:
----
Sample_back_1.tif
Sample_back_2.tif
Sample_back_3.tif
Sample_back_4.tif
Sample_back_5.tif


Friday, 6 June 2014

iframe src attribute in C#

iframe src attribute in C#:

iFrame src changing c# 


aspx:

   <iframe name="iframeanimfx"  id="iFranmeGame" runat="server"
             scrolling="no" frameborder="no" height="400px" 
             width="750px">
  </iframe>



aspx:cs:

        DataSet ds = new DataSet();
        ds = objDal.PlayGame(GameID);
        if (ds.Tables[0].Rows.Count != 0)
        {
            iFranmeGame.Attributes["src"] = 
                          ds.Tables[0].Rows[0]["Game_Path"].ToString();
        }