Friday, July 23, 2021

How to add zeros after decimal point

 var a= 10.1;

var result  = a.ToString("f2");

result = 10.10;

Monday, February 1, 2021

Cannot drop database "abc" because it is currently in use.

We will face the problem:

USE TEstDb;
DROP DATABASE TEstDb;

Solution:

 Use Master

DROP DATABASE TEstDb

Tuesday, September 8, 2020

Reading Excel From C# using OLEDB Connection

 First, You need to add below name spaces to read Excel file in OLEDB


using System.Data.OleDb;
using System.Configuration;
using System.Data.SqlClient;

And we need establish a connection to the Database and OLEDB. Add below connection code in web.config file


<connectionStrings>
   <add name="AD_Phase2" connectionString="Data Source=localhost;Database=DatabaseName;User Id=sa;Password=Password" providerName="System.Data.SqlClient"/>
    <add name="ExcelConnection" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=NO'\"/>
    <add name="Excel03ConString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR=NO'"/>
    <add name="Excel07ConString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0;HDR=NO'"/>
  </connectionStrings>

Below is the code for read the Excel data and fill into datatable

//Read the Excel file and fill in datatable .
        public DataTable GetDataTable(string strSheetName)
        {
            try
            {
                string strComand;

                if (strSheetName.IndexOf("|") > 0)
                {
                    /* if Range is provided.*/
                    string SheetName = strSheetName.Substring(0, strSheetName.IndexOf("|"));
                    string Range = strSheetName.Substring(strSheetName.IndexOf("|") + 1);
                    strComand = "select * from [" + SheetName + "$" + Range + "]";
                }
                else
                {
                    //= Images_Path + "" + SaveStudentDetails.FileName;
                    strComand = "select * from [Sheet1$]";
                }
                //HttpPostedFile StudentPhotoFile = SaveStudentDetails.PostedFile;
                string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FinalPath + ";Extended Properties='Excel 8.0;HDR=NO;'";
                OleDbConnection connection = new OleDbConnection(excelConnectionString);
                OleDbDataAdapter daAdapter = new OleDbDataAdapter(strComand, connection);
                DataTable dt = new DataTable("Datatable");

                DataColumn dc = new DataColumn();

                DataRow dr = dt.NewRow();
                dt.Rows.Add(dr);

                daAdapter.Fill(dt);

                return dt;

            }

            catch (Exception ex)
            {
                throw new Exception("Select location" + ex.Message);
            }
        }

Monday, September 7, 2020

Solid Principles in C#

The SOLID Design Principles and Design Patterns play an important role in achieving all of the above key points.

The SOLID Design Principles are the design principles that help us to solve most of the software design problems. These design principles provide us with multiple ways to move the tightly coupled code between the software components which makes the software designs more understandable, flexible, and maintainable. 

S stands for the Single Responsibility Principle which is also known as SRP.

O stands for the Open-Closed Principle which is also known as OSP.

L stands for the Liskov Substitution Principle which is also known as LSP.

I stand for the Interface Segregation Principle which is also known as ISP.

D stands for Dependency Inversion Principle which is also known as DIP.

Wednesday, September 2, 2020

Why we need Partial Views in ASP.NET MVC Application?

When we need a common part of the user interface at multiple pages in a web application then we develop a partial view, hence the partial view is a regular view that can be used multiple times in an application and has the file extension .cshtml.

Sometimes we also use a partial view to divide a web page into small parts such as header, footer, and menu on Layout. Other examples are comments in blogging site, shipping and billing address in the invoice in e-commerce site, etc. 

If you are coming from asp.net web-forms background, then you can realize that partial views in MVC are similar to user controls in asp.net web forms. 

Tuesday, September 1, 2020

use of "New" keyword

Using a new keyword for re-implementing the methods in the child class is optional and if used will give information to hiding.

Friday, August 28, 2020

Generics example

namespace Generics

{

    public class MainClass

    {

        private static void Main()

        {

            //bool IsEqual = ClsCalculator.AreEqual<int>(10, 20);

            //bool IsEqual = ClsCalculator.AreEqual<string>("ABC", "ABC");

            bool IsEqual = ClsCalculator.AreEqual<double>(10.5, 20.5);

            if (IsEqual)

            {

                Console.WriteLine("Both are Equal");

            }

            else

            {

                Console.WriteLine("Both are Not Equal");

            }

            Console.ReadKey();

        }

    }

    public class ClsCalculator

    {

        public static bool AreEqual<T>(T value1, T value2)

        {

            return value1.Equals(value2);

        }

    }

}

Thursday, August 27, 2020

What is IEnumerable in C#?

 IEnumerable in C# is an interface that defines one method, GetEnumerator which returns an IEnumerator interface. This allows read-only access to a collection then a collection that implements IEnumerable can be used with a for-each statement.

Wednesday, August 26, 2020

Is Vs as C#?

 The difference between is and as operators are as follows: The is operator is used to check if the run-time type of an object is compatible with the given type or not whereas as operator is used to perform conversion between compatible reference types or Nullable types.

What is difference between generic and non generic in C#?

A Generic collection is a class that provides type safety without having to derive from a base collection type and implement type-specific members. The key difference between Generic and Non-generic Collection in C# is that a Generic Collection is strongly typed while a Non-Generic Collection is not strongly typed.