Posts

Showing posts with the label C#

How to Read list of objects from json file and convert it into list of objects in c# .net core

  public List<CityMaster> getcities()         {             List<CityMaster> cities = new List<CityMaster>();             try             {                 var res = new List<CityMaster>();                 string path1 = $"{Directory.GetCurrentDirectory()}{@"\wwwroot\assets\citieslist.json"}";                 using (StreamReader r = new StreamReader(path1))                 {                     string json = r.ReadToEnd();                     res = JsonConvert.DeserializeObject<List<CityMaster>>(json);                 }      ...

How to Get Datetime Wrt Time Zone in C#

  public static DateTime GetDateTime()   { //here we are trying to get the indian datetime irrespective of hosted server location             TimeZoneInfo timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");             DateTime now = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, timeZoneInfo);             return now;   }

How to Read Countries, States, Cities data from Json file and Convert to List Objects in C#.

 Here I'm attaching the Json String of Countries List, States List, Cities List and also the scripts for all countries, states, cities. You can just , use and convert the Json string to List Object and use them. Models For Country, State, City: public class CountryMaster { public int ID { get; set; } public string CountryName { get; set; } public string CountryCode { get; set; } } public class StateMaster { public int ID { get; set; } public string StateName { get; set; } public int CountryID { get; set; } } public class CityMaster { public int ID { get; set; } public string CityName { get; set; } public int StateID { get; set; } } Note: The above models are as per json   For Sample, To get the country list you can use below code,  var res = JsonConvert.DeserializeObject<List<CountryMaster>>("country json string");  Drive link: drivelink     

How to Show only certain characters and bind '...' at end in mvc view in c#

  <span class="text-muted"> //here we show upto 200 characters and then '...' are shown @(item.Content.Length > 200 ? item.Content.Substring(0, 200) + "..." : item.Content) </span> 

How to Preview the image and download which is in base64string format in mvc view using C#

  <div class="col-lg-9 col-xl-6">                                                     @if (reclaimdetai != null)                                                     {                                                         @foreach (var item in reclaimdetai.RequestFileList)                                                         {                                 ...

How to Change Layout based on viewbag value in mvc view using Mvc,C#

 @{     if (@ViewBag.lang == "en")     {         Layout = "Layout Name 1"; //This content uses above layout     }     if (@ViewBag.lang == "ar")     {         Layout = "Layout Name 2"; //This content uses above layout     } }

How to Get the current url and redirect to other url(case:button click english to arabic screen using parameter in url) using jquery,mvc,c#

   <a class="btn btn-icon btn-clean btn-lg w-40px h-40px btnlang" id="btnlang" > Change Lang </a> sample url:  https://localhost:44310/Claims/AddClaim?lang=en <script src="~/lib/jquery/dist/jquery.min.js"></script> <script>     $(document).ready(function () {         $('.btnlang').click(function () {             debugger;             var pageURL = $(location).attr("href");//gets the current url             var baseaddress = pageURL.substring(0, pageURL.indexOf('?'));//get the base address before '?'             var tech = getUrlParameter('lang');//gets the parameter using parameter name i.e,lang             if (tech == "en") {                 location.href = baseaddress + "?lang=ar";             }...

How to write Function to validate input password pattern in mvc using Jquery

 function validatepin(cpin) {     //var paswd = /^(?=.*\d)(?=.*[^a-zA-Z0-9])(?!.*\s).{8,8}$/;     if (containsCharacters(cpin) && containsNumber(cpin) && containsSpecialCharacters(cpin)) {                return true;     } else {                return false;     } } function containsCharacters(val) { var regex = /[A-z]/; var isValid = regex.test(val); return isValid; } function containsNumber(val) { var regex = /[0-9]/; var isValid = regex.test(val); return isValid; } function containsSpecialCharacters(val) { var regex = /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/; var isValid = regex.test(val); return isValid; }

How to write api method to Read file and save it in ftp server using webapi c#

[Route("getdata")]         [HttpPost]         public string ReadFromAzureAS(string path)         {             //FTP Server URL.             string ftp = "ftp://localhost:21/";             //FTP Folder name. Leave blank if you want to upload to root folder.             string ftpFolder = "Uploads/";             string status = "File Transfer Failed";             FileStream stream = File.OpenRead(path);             byte[] fileBytes = new byte[stream.Length];             stream.Read(fileBytes, 0, fileBytes.Length);             stream.Close();             //Begins the process of writing the byte array back to a file      ...

Read any file using filepath and save it in a folder in c#

// Read file to byte array FileStream stream = File.OpenRead( @" c:\path\to\your\file\here.txt" ); byte [] fileBytes= new byte [stream.Length]; stream.Read(fileBytes, 0 , fileBytes.Length); stream.Close(); // Begins the process of writing the byte array back to a file using (Stream file = File.OpenWrite( @" c:\path\to\your\file\here.txt" )) { file.Write(fileBytes, 0 , fileBytes.Length); }

How to get or extract the data from different json format in c#, webapi

                        JSON  C# Code  Model Class Sample 1: {    "EmployeeList": [       {          "EmpNumber": "10029597-3",          " JobCode ": "03363-001",          " Location ": "Triad North"       },       {          " EmpNumber ": "10029600-1",          " JobCode ": "03363-001",          " Location ": "Triad North"       },       {          " EmpNumber ": "10029626-2",     ...