ASP.NET MVC, MVC, C#, PYTHON, JQUERY

Friday, July 3, 2020

Insert,Update and Delete Using Asp.net MVC



 In this article, we will learn about how to Insert, Update and Delete Records in ASP.NET MVC 5.

LET'S START ASP.NET MVC 5 WITH 

 In this example, I am going to create a simple student registration form in MVC5 using CRUD  (Create, Read, Update and Delete) Operation. In this tutorial, We will use  entity framework.

 <pre>
 <code class="csharp">
public List<RecruitmentRegistrationModel> GetUserRegistrationRecord(RecruitmentRegistrationModel model, int ProcId)
        {
            var sqlParam = new SqlParameter[]{
                          new SqlParameter { ParameterName = "@ProcId" , Value =ProcId },
                          new SqlParameter { ParameterName = "@Id" , Value =model.ApplicationPostId },
                          new SqlParameter { ParameterName = "@Gender" , Value =model.Gender??"" },
                          new SqlParameter { ParameterName = "@Age" , Value =model.Age??0 },
                          new SqlParameter { ParameterName = "@TotalExp" , Value =model.TotalExp??0 },
                          new SqlParameter { ParameterName = "@EmployerType" , Value =model.EmployerType??"" }
                    };
            var sqlQuery = @"proc_GetUserRegistrationRecord @ProcId,@Id,@Gender,@Age,@TotalExp,@EmployerType";
            var res = this.Database.SqlQuery<RecruitmentRegistrationModel>(sqlQuery, sqlParam).ToList();
            return res;
        }
                               </code></pre>





Share:

Monday, June 29, 2020

Download All Files In ZIP Format In Asp.Net MVC.NET



Hello Friends In this article, we will create a application for download all media file’s in zip format. We have to follow some simple steps to create a project and export some files in zip format in ASP.NET MVC
Step 1 – Create a Project
Opening Visual Studio, next, we need to create an ASP.NET MVC project. For doing that, just click File – New – Project.
After choosing a project, a new dialog will pop up. In that, select ASP.NET Web Application in the Web tab and give your project the location and a name. Then, click the “Ok” button. Choose the MVC Template.
Step 2: Install the SharpZipLib from Nuget package console
Install-Package SharpZipLib
Install-Package SharpZipLib


Step 3 – Create a Action Method in Controller for Download zip file
Write code into Our DownloadZipController and give Media file path into List for Media file that you want to download in the zip file and create a folder named DownloadedMedia in root directory. Right now, I gave the static image path but you can set dynamic image path according to your requirements.
using System;
using System.Drawing;
using System.Web;
using System.Web.Mvc;
using System.Xml.Linq;
using ICSharpCode.SharpZipLib.Zip;
namespace DownloadZipProject.Controllers
{
  public class DownloadZipController:Controller
  {
    private DataAccessLayer _db = new DataAccessLayer ();
    public FileResult DownloadMediaFilesInZip ()
    {
      var FileName = string.Format ("{1}_AllDocumentsFiles_{0}.zip",
    DateTime.Today.
    Date.ToString ("dd-MM-yyyy") + "_1",
    SessionManager.ApplicantUserName);
      var OutPutTempPath =
Server.MapPath (Url.Content ("/DownloadedMedia/") + FileName;
using (ZipOutputStream s =
       new ZipOutputStream (System.IO.File.Create
    (OutPutTempPath)))
{
s.SetLevel (9);
/byte[]buffer = new byte[4096];
var AllMediaList = new List < string > ();
DataSet ds = _db.RecruitmentApplicationPrint ();
var MediaFilesList = ds.Tables[0];
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
MediaFilesList.Add (Server.MapPath
    (ds.Tables[1].Rows[i]["DocPath"].
     ToString ()));}
for (int i = 0; i < MediaFilesList.Count; i++)
{
ZipEntry entry =
new ZipEntry (Path.GetFileName (MediaFilesList[i]));
entry.DateTime = DateTime.Now;
entry.IsUnicodeText = true; s.PutNextEntry (entry);
using (FileStream fs =
       System.IO.File.OpenRead (MediaFilesList[i]))
{
int sourceBytes;
do
{
sourceBytes = fs.Read (buffer, 0, buffer.Length);
s.Write (buffer, 0, sourceBytes);}
while (sourceBytes > 0);}
}
s.Finish (); s.Flush (); s.Close ();}
byte[]finalResult =
System.IO.File.ReadAllBytes (OutPutTempPath);
if (System.IO.File.Exists (OutPutTempPath)) System.IO.
File.Delete (OutPutTempPath);
if (finalResult == null
    || !finalResult.
    Any ())throw new Exception (String.
Format
("No Media File found"));
return File (finalResult, "application/zip",
     fileName);}
}
Step 4 – Implement button on view page for download Media files from the view side
Share:

Sunday, May 31, 2020

Program for Betting in Dice Game

Code Description:
   This program is basically showing random number after rolling dice. Here are two dice where total count value is between 2 to 12. User can play this game and bet their amount on given 3 choice as per instructions.

Source Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace ConsolePractice
{
    class Program
    {
        static void Main(string[] args)
        {

           DiceGame();

        }

        public static void DiceGame()
        {
            string name;
            int betamount, betChoice, DiceNumber;

            Console.WriteLine("==================Dice Game==================");
            Console.WriteLine("===========================================================");

            Console.WriteLine("You have three option for play Dice Game\nAfter entering your good name and deposit the betting amount.\n");

            Console.WriteLine("Option-1: Dice Number should between 1 to 5 ~~Winning Amount Double\nOption-2- Dice Number should between 6 to 7 ~~Winning Amount Triple\nOption-3: Dice Number should between 8 to 12 ~~Winning Amount Double\n");
            Console.WriteLine("Your Lucky Name");
            name = Console.ReadLine();
            Console.WriteLine("Your bet Amount");
            betamount = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Choose your Choice between 1,2 or 3");
            betChoice = Convert.ToInt32(Console.ReadLine());
            Random ran = new Random();
            DiceNumber = ran.Next(2, 13);

            if (betChoice==1)
            {
                Console.WriteLine("Dice is rolling . . . .");
                Thread.Sleep(2000);
                Console.WriteLine("Dice Result is {0}", DiceNumber);
                if (DiceNumber<=5)
                {
                    Console.WriteLine("Hi," + name + " your bet amount is " + betamount + " and You Win !! Your amount is now " + (betamount * 2));
                    
                }
                else
                {
                    Console.WriteLine("Oops!! Sorry" + name + ", You Loose !");

                }
            }
            else if (betChoice==2)
            {
                Console.WriteLine("Dice is rolling . . . .");
                Thread.Sleep(2000);
                Console.WriteLine("Dice Result is {0}", DiceNumber);
                if (DiceNumber==6 || DiceNumber==7)
                {
                    Console.WriteLine("Hi," + name + " your bet amount is " + betamount + " and You Win !! Your amount is now " + (betamount * 3));
                }
                else
                {
                    Console.WriteLine("Oops!! Sorry" + name + ", You Loose !");
                }
                
            }
            else if (betChoice==3)
            {
                Console.WriteLine("Dice is rolling . . . .");
                Thread.Sleep(2000);
                Console.WriteLine("Dice Result is {0}", DiceNumber);
                if (DiceNumber>=8)
                {
                    Console.WriteLine("Hi," + name + " your bet amount is " + betamount + " and You Win !! Your amount is now " + (betamount * 2));
                }
                else
                {
                    Console.WriteLine("Oops!! Sorry" + name + ", You Loose !");
                }
            }
            else
            {
                Console.WriteLine("Wrong Choice!! Choose correct Option.");
            }
            Console.ReadLine();
        }
    }

}

Output:


Share:

Program for swap two integer values

Source Code:

using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleProgramming
{
    class Program
    {
        static void Main(string[] args)
        {
            SwapNumbers();
        }
       
        //Swap two numbers
        public static void SwapNumbers()
        {
            int a = 10, b = 5;
            Console.WriteLine("Before swapping numbers are a={0},b={1}", a, b);
            a = a - b;
            b = a + b;
            Console.WriteLine("After swapping numbers are a={0}, b={1}", a, b);
            Console.ReadLine();
        }
     }
}

Output:

Share:

SOCIAL PROFILES

Search This Blog

Powered by Blogger.

Fixed Menu (yes/no)

yes

Contact Form

Name

Email *

Message *

Tags

Ads Section

Tags

ASP.NET MVC (7) C# (11) jQuery (1)