Friday, June 19, 2020
Populate dropdownlist using jquery
June 19, 2020
Create the required tables and stored procedures
Table Script -
Create Table DepartmentTable
(
Id int identity primary key,
DepartmentName nvarchar(20)
)
Go
Insert into DepartmentTable values ('Science')
Insert into DepartmentTable values ('Arts')
Insert into DepartmentTable values ('Commerce')
Go
Stored Procedure -
CREATE PROCEDURE USP_GetDepartment
AS
BEGIN
Select Id, DepartmentName from DeparmentTable
END
GO
Step 2-
Create new asp.net web application project. Name it JqueryDemo.
Step 3-
Include a connection string in the web.config file to your database.
<add name="CS"
connectionString="server=.;database=SampleDB;integrated security=SSPI"/>
Step 4-
Add a class file to the project. Name it Department.cs. Copy and paste the following code.
namespace JqueryDemo
{
public class Department
{
public int Id { get; set; }
public string DepartmentName { get; set; }
}
}
Step 5-
Add a WebService (ASMX) to the project. Name it WebService.asmx. Copy and paste the following code.
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.Script.Serialization;
using System.Web.Services;
namespace JqueryDemo
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class WebService: System.Web.Services.WebService
{
[WebMethod]
public void GetDepartment()
{
string cs = ConfigurationManager.ConnectionStrings["CS"].ConnectionString;
List<Department> Department= new List<Department>();
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("USP_GetDepartment", con);
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Department ObjDepartment = new Department();
ObjDepartment.Id = Convert.ToInt32(rdr["Id"]);
ObjDepartment.Name = rdr["DepartmentName"].ToString();
Department.Add(ObjDepartment);
}
}
JavaScriptSerializer js = new JavaScriptSerializer();
Context.Response.Write(js.Serialize(Department));
}
Step 6-
Add a WebForm to the ASP.NET project. Copy and paste the following HTML and jQuery code.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
</style>
<script src="https://code.jquery.com/jquery-2.2.4.js"></script>
<script type="text/javascript">
$(document).ready(function () {
var DDLDepartment = $('#DDLDepartment');
$.ajax({
url: 'WebService.asmx/GetDepartment',
method: 'post',
dataType: 'json',
success: function (data) {
DDLDepartment.append($('<option/>', { value: -1, text: 'Select Department' }))
$(data).each(function (index, item) {
DDLDepartment.append($('<option/>', { value: item.Id, text: item.DepartmentName }));
});
},
error: function (err) {
alert(err);
}
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table align="center" style="width: 261px">
<tr>
<td>
<asp:Label ID="lblDepartment" runat="server" Text="Department : "></asp:Label></td>
<td>
<asp:DropDownList ID="DDLDepartment" runat="server">
</asp:DropDownList></td>
</tr>
</table>
</div>
</form>
</body>
</html>
Saturday, June 6, 2020
How to Integrate AutoMapper in ASP.NET Core
June 06, 2020
What is AutoMapper ?
AutoMapper is a mapper between two objects, it means that AutoMapper is object-object mapper. It maps properties of two different Objects by transforming input object of one type to output object of another type.
Integrate AutoMapper-
Step 1-
Go to NuGet Package Manager and add package AutoMapper.Extensions.Microsoft.DependencyInjection
Step 2-
Create two files Customer.cs and CustomerModel.cs and add two classes Customer,CustomerModel
public class Customer {
public int CustomerId { get; set; }
public string CompanyName { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public string Phone { get; set; }
public string Country { get; set; }
public string City { get; set; }
public string Pincode { get; set; }
}
public class CustomerModel {
public int CustomerId { get; set; }
public string FullName { get; set; }
public string Phone { get; set; }
public string Country { get; set; }
public string City { get; set; }
public string Pincode { get; set; }
}
Step 3-
Create Mapping profile as CustomerProfile.cs
public class CustomerProfile : Profile {
public CustomerProfile () {
// Mapping properties from Customer to CustomerModel
CreateMap<Customer, CustomerModel> ()
.ForMember (dest =>
dest.FullName,
opt => opt.MapFrom (src => src.FirstName + " " + src.MiddleName + " " + src.LastName));
// ForMember is used incase if any field doesn't match
}
}
Step 4-
To invoke mapped object in code
public class HomeController : Controller {
private readonly IMapper _mapper;
public HomeController (IMapper mapper) {
_mapper = mapper;
}
public IActionResult Index () {
Customer customerdetails = new Customer () {
CustomerId = 1,
CompanyName = "ABC",
Address = "Banaglore",
Phone = "000",
FirstName = "Shwetha",
MiddleName = "Amit",
LastName = "Naik",
City = "Bangalore",
Country = "India",
Pincode = "560091"
};
var customerModel = _mapper.Map<CustomerModel> (customerdetails);
var fullname = customerModel.FullName;
var address = customerModel.City;
return View ();
}
}
Friday, June 5, 2020
Different types of bindings supported by WCF
June 05, 2020
What is Binding in WCF ?
WCF bindings are basically objects used to specify the communication details that are required to connect to the endpoint of WCF
Types of WCF Binding -
1. BasicHttpBinding
2. WSHttpBinding
3. WSDualHttpBinding
4. WSFederationHttpBinding
5. NetTcpBinding
6. NetNamedPipeBinding
7. NetMsmqBinding
8. NetPeerTcpBinding
9. MsmqIntegrationBinding
2. WSHttpBinding
3. WSDualHttpBinding
4. WSFederationHttpBinding
5. NetTcpBinding
6. NetNamedPipeBinding
7. NetMsmqBinding
8. NetPeerTcpBinding
9. MsmqIntegrationBinding
1) BasicHttpBinding -
It is suitable for communicating with ASP.NET Web Service that comfort with WS-Basic Profile conformant web service. It uses Http protocol for transport and encodes the messages
2) WSHttpBinding -
This binding also supports interoperability and by default reliable sessions are disabled. It uses HTTP and HTTPS transport for communication.
3) WSDualHttpBinding -
WSHtppBinding and WSDualHtppBinding are same ,except it supports duplex service. Duplex service is service which uses duplex message pattern.
4) WSFederationHttpBinding -
This binding supports federated security and it supports WS-Federation Protocol.
5) NetTcpBinding -
NetTcpBinding provides secure and reliable binding environment for .Net to .Net cross machine communication. It also supports TCP and Binary Protocol for encoding method.
6) NetNamedPipeBinding -
provides secure and reliable binding environment for on-machine cross process communication. It uses named pipes as transport for SOAP messages.
7) NetMsmqBinding -
provides secure and reliable queued communication for cross-machine environment . This Msmq as transport.
8) NetPeerTcpBinding -
NetPeerTcpBinding provides secure binding for peer-to-peer environment and network applications. It uses TCP Protocol for communication.
9) MsmqIntegrationBinding -
It is provided by MsmqIntegrationBinding class and offers support to communicate with existing systems that communicate via MSMQ.
Subscribe to:
Posts (Atom)