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:

Thursday, September 13, 2018

Half pyramid program for fibonacci series



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

namespace fibonaccipyramid
{
    class Program
    {
        static void Main(string[] args)
        {
            int i,a=0,b=1,n,c,j,k;
            Console.WriteLine("Enter the limit");
            n=Convert.ToInt32(Console.ReadLine());
            Console.WriteLine(a);
            Console.WriteLine(b);
            for (i = 1; i <= n; i++)
            {
         
                for (j = 1; j <= i; j++)
                {
               
                        c = a + b;
                        a = b;
                        b = c;

                        Console.Write(c);
                        Console.Write("\t");
               
            }
                Console.Write("\n");
            } Console.ReadLine();
        }
    }
}



Share:

Wednesday, September 12, 2018

Factorial program using recursion in c#




We have come across the situations where a function calls another function but there is one more possibility that a function calls itself.
When a function calls itself it is known as recursion. It is usually used where we have to repeat a process again and again.
The following program for finding factorial using recursion is an example of recursive function and its implementation.

  1. using System;    
  2.     
  3. namespace Recursion_Example   
  4. {    
  5.     class Program    
  6.     {    
  7.         static void Main(string[] args)    
  8.         {    
  9.            int n;
  10.             Console.WriteLine("Enter the Number");    
  11.     
  12.                
  13.              n =Convert.ToInt32(Console.ReadLine());     //read number from user
  14.     
  15.             double factorial = Factorial(n);    //invoke the static method    
  16.     
  17.             //print the factorial result    
  18.             Console.WriteLine("factorial of"+n+"="+factorial.ToString());    
  19.     
  20.         }    
  21.         public static double Factorial(int n)    
  22.         {    
  23.             if (n == 0)    
  24.                 return 1;    
  25.             return n * Factorial(n-1);//Recursive call    
  26.     
  27.         }    
  28.     }    
  29. }    
Share:

Friday, September 7, 2018

Main() Method In C#

Main() Method In C# 

Hello Friends In this article we will learn about What is Main() Method in C#.



  • Main() Method is an entry point of an application.
  • Main () Method means That execution of code start from Main() method.
  • When an application starts Execution C# compiler looks for in the source file is the Main() method.
  • You must include Main () method in a class make your program executable.


Example:-

public Class HelloCsharp
{
public static void Main(string[] args)  //Main () method 
System.Console.WriteLine("Hello Friends Welcome To Csharp4tech");
}
}
Share:

SOCIAL PROFILES

Search This Blog

Powered by Blogger.

Fixed Menu (yes/no)

yes

Contact Form

Name

Email *

Message *

Tags

Follow by Email

Ads Section

Tags

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