Thursday, 10 November 2016

How to use procedure in DapperClass in C# .Net

using Dapper;
using System;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Windows.Forms;
class DataClassDapper
    {
        private string connectionString;
        private static DataClassDapper dataClass;

        public static DataClassDapper getInstance()
        {
            if (dataClass == null)
                dataClass = new DataClassDapper();

            return dataClass;
        }

        private DataClassDapper()
        {
            connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["PRDb"].ConnectionString;
        }

        public IDbConnection OpenConnection()
        {
            IDbConnection connection = new SqlConnection();

            connection.ConnectionString = connectionString;

            try
            {
                connection.Open();
            }
            catch (SqlException)
            {
                MessageBox.Show("Error Connecting database.");
            }

            return connection;
        }

  internal static bool ExecuteProc(string sql, List<SqlParameter> paramList = null)
        {
            try
            {
                using (IDbConnection connection = DataClass.getInstance().OpenConnection())
                {

                    DynamicParameters dp = new DynamicParameters();
                    if (paramList != null)
                        foreach (SqlParameter sp in paramList)
                            dp.Add(sp.ParameterName, sp.SqlValue, sp.DbType);
                
                    return connection.Execute(sql, dp, commandType: CommandType.StoredProcedure) > 0;
                }
            }
            catch (Exception e)
            {
                //do logging
                return false;
            }
        }
}





Your Model:
    public class Company : IEquatable<Company>
    {
        public int id { get; set; }

        public string name { get; set; }
               
        public string address { get; set; }
               
        public string phone { get; set; }

        public string fax { get; set; }
       
        public string email { get; set; }

        public string website { get; set; }
     }


using System;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Threading.Tasks;
using System.Collections.Generic;
using PayrollManagementSystem.Model;
using PayrollManagementSystem.Database;
using Dapper;
public  class CompanyManager
    {
       public static CompanyManager companyManager;

        public CompanyManager()
        {
       
        }
  public bool AddNewCompanyProc(Company company)
        {
            string insertQuery = @"usp_insertCompany";
            List<SqlParameter> list = new List<SqlParameter>();
            SqlParameter param = new SqlParameter();
            param.ParameterName = "@name";
            param.Value = company.name;
            list.Add(param);
            SqlParameter param1 = new SqlParameter();
            param1.ParameterName = "@address";
            param1.Value = company.address;
            list.Add(param1);

         

            SqlParameter param2 = new SqlParameter();
            param2.ParameterName = "@phone";
            param2.Value = company.phone;
            list.Add(param2);
            SqlParameter param3 = new SqlParameter();
            param3.ParameterName = "@fax";
            param3.Value = company.fax;
            list.Add(param3);

            SqlParameter param4 = new SqlParameter();
            param4.ParameterName = "@email";
            param4.Value = company.email;
            list.Add(param4);

            SqlParameter param5 = new SqlParameter();
            param5.ParameterName = "@website";
            param5.Value = company.website;
            list.Add(param5);
            return DataClassDapper.ExecuteProc(insertQuery, list);
        }

}


CompanyEntryForm.cs





void SaveData()
        {

            try
            {


                Company company = new Company(0, tbxCompanyName.Text, tbxAddress.Text, tbxPhone.Text, tbxFax.Text, tbxEmail.Text, tbxWebAddress.Text);
                bool success= CompanyManager.getInstance().AddNewCompanyProc(company);
                if (success)
                {
                    KryptonMessageBox.Show("Company saved successfully!", "Add Company.", MessageBoxButtons.OK,
                        MessageBoxIcon.Information);
                }
                else
                {
                    KryptonMessageBox.Show("Company saved failed!", "Add Company.", MessageBoxButtons.OK,
                       MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {


            }

        }

How to convert list of model Data to DataTable in .Net

Convert List to DataTable is easy and light using JSon library.

Step 1: install Json library from Nuget package

>Go to Visual Studio Tools

> Library package manager > Package manager console

PM> Install-Package Newtonsoft.Json -Version 9.0.1

Your Model:
    public class Company : IEquatable<Company>
    {
        public int id { get; set; }

        public string name { get; set; }
               
        public string address { get; set; }
               
        public string phone { get; set; }

        public string fax { get; set; }
       
        public string email { get; set; }

        public string website { get; set; }
     }


private DataTable LoadCompany()
{
  private Company company;
  company = CompanyManager.getInstance().LoadCompanyList();
          
 string json = Newtonsoft.Json.JsonConvert.SerializeObject(company);
          
 var token = JToken.Parse(json);
          
 if (token.Type == JTokenType.Object)
          
     token = new JArray(token);
        
  DataTable dt = token.ToObject<DataTable>();
return dt;

}

Tuesday, 25 October 2016

Difference between ASP.NET MVC and ASP.NET Web API – Exposing Functionality of Web Application

ASP.NET MVC or Model View Controller is a three tier architectural deweb application development using ASP.NET technology. In MVC framework, models are the components responsible for information retrieval and storage in database, while view displays the information and controllers are used for managing and responding to user inputs.
sign or framework for
Talking about ASP.NET Web API, this is a framework used for developing HTTP services to provide response to the client requests. Web API is an open source technology that is used to develop web services for applications such that they can reach broader range of clients with HTTP protocols.
Now let us compare the two technologies with regards to exposing functionality of web applications.
While it is possible to expose functionality of web application through Web API, it can also be done through MVC by using the action methods. However, in order to decide between these two technologies to use for functionality exposing, there are a few main things to consider and they are as follows.
Exposing Functionality or Creating Full-Fledged REST Service:
When it comes to exposing functionality of a web application, there could be two considerations – one is exposing the functionality within the web application and the second is to expose it as a generic functionality that is independent of any specific application. Relying on MVC is a good option if exposing functionality within application is your requirement, because MVC controllers are tightly tied to a particular application. On the other hand with Web API, it would require to create a whole new API and hence, the process will get more complicated and time consuming.
Considering the second case, it’s better to create full-fledged REST service for the purpose using Web API because the REST service is not limited to just a single applications and can be used by several applications for their tasks. So, if your application functionality is data oriented then Web API offers an elegant solution, while MVC is good for developing the web application that replies as both data and views.
Formats to Deal With
MVC returns data in typical JSON format using JSONResult. Hence, one can use action methods to expose functionality of an application here. But, Web API returns various kinds of data including the JSON, XML or any other data format based on accept header. With MVC you are required to specify the data format explicitly while it is done automatically in Web API.
Hosting
Hosting is an important consideration during ASP.NET development. As MVC controller is part of an ASP.NET MVC application, it depends on IIS environment for application hosting while on other hand, Web API is a service based framework which can allow the developer to host his application in a custom host other than IIS. Hence, with Web API you can host your applications in light weight host server as per your needs and save overheads usually witnessed in IIS hosted ASP.NET applications. Web API is therefore a reliable technology for developing web, console and desktop applications.
Hence, by considering the above points during ASP.NET development, one can easily select between Web API and MVC technologies to expose the functionality of a web application.

Comments system

Advertising

Disqus Shortname