Saturday, 5 November 2016

DataBase First Approach With Asp.Net MVC Framework

What is Entity Frame Work?

  • Entity Framework is an ORM Framework.
  • ORM stands for Object Relational Mapping.
  • ORM automatically creates the classes based on database tables and vice versa.
  • By using ORM framework we can save a lot of time and effort when interact with database.
  • we have three approaches.
  1. Database approach
  2. Model -First approach
  3. Code -First approach
  • we can choose any one of the above approach to reduce the time in development.

Understanding the Database Approach


First create the Database with the Name of DatabaseFirstApproach in SQL SERVER.
Now create the table withe name Departments and Employees.

/* create Database */
create database DatabaseFirstApproach 


/* Use Databse */
use DatabaseFirstApproach 


/* create Departments */
create table Departments
(
Id int primary key not null,
Names varchar(40),
Locations varchar(40)
)

/* create Employee*/

create table Employees
(
Id int primary key identity(1,1),
FirstNames varchar(40),
LastName varchar(40),
Gender varchar(40),
Salary int,
DepartmentId int foreign key references Departments(Id)
)

/* Insert Values in Departments */

insert into Departments values(1,'IT','NewYork')
insert into Departments values(2,'HR','London')
insert into Departments values(3,'Payroll','Sidney')

/* Insert Values in Employee */

insert into Employees values('Mark','Hasting','Male',6000,1)
insert into Employees values('Murali','Krishna','Male',9000,2)
insert into Employees values('Mark','Hasting','Male',2000,1)
insert into Employees values('Mark','Hasting','Male',1000,2)
insert into Employees values('Mark','Hasting','Male',8000,3)
insert into Employees values('Mark','Hasting','Male',9000,2)

/* Select Department and Employee for check the values are inserted or not */
select *from Employee
select *from Departments


  • Next Open Visual Studio of any version and create a new project
  • goto File --> select Website -> choose website empty template
  • now open Solution Explorer -->Right click on solution check Manage NuGet Packages for Solution.
  • if it is not available then go to tools -->check Extension and Updates 
  • one pop up Wizard appear and see a search box at right side of  wizard.
  • now you check NuGet Packages manager and install it.
  • now goto solution explorer -->right click -->check Solution check Manage NuGet Packages for Solution 
  • Now open it search for Entity Framework and install it.
  • Now right click on DatabaseFirstApproach Project -->add-->Asp.net Folder-->add App_Code.
  • Now right click on App_Code -->add -->AddNewItem --> and search for ADO.NET ENTITY DATA MODEL.

DataBase First Approach With Asp.Net MVC Framework

DataBase First Approach With Asp.Net MVC Framework
DataBase First Approach With Asp.Net MVC Framework
  • Now add new form to the project and drag and drop the GridView and EntityDataSource

          <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
        <asp:EntityDataSource ID="EntityDataSource1" runat="server">
        </asp:EntityDataSource>


  • Next Flip into Design and go to Entity Data Source Task --> Configure it . Before Configure .You should Build the Project.
  • next go to grid View  Task and choose the Entity Data Source. and build it finally run the program. you can see the out put






Read more »

Thursday, 3 November 2016

RequiredFieldValidator in Asp.Net

  • RequiredFieldValidator is one of top Asp.net control and it is very simple and very use full.
  • we can use as the user has entered something in the TextBox or not.
For example : Add the RequiredFieldValidator to our page and also add the TextBox to validate as well as add the Button to submit the form

 <form id="form1" runat="server">
    <div>
         <br />
 &nbsp;
  Username:    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                     <asp:RequiredFieldValidator ID="RequiredFieldValidator1"                                 runat="server" ErrorMessage="RequiredFieldValidator">                                 </asp:RequiredFieldValidator>
        <br />
        <br />
 
    </div>
        <div style="margin-left: 80px">
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click"     Text="CHECK REQUIRE FIELD VALIDATION" />
            <br />
        </div>
    </form>



  • Now Run the above program you see the form like as below image.
RequiredFieldValidator in AspNet

  • if your browser supports the JavaScript,which most modern browsers do,So you can notice that the webpage isn't being posted back to server but the validation happens in client side!!!.
  • This is the one of the good thing about validators of asp.net  validation is performed in client side.
  • If we use the attribute enableclientscript="false" to RequiredFieldValidator then the validator performed at server side
  • If necessary you want to perform the validation in server side then use the attribute enableclientscript enable as false then you can see the browser posting back to server, but the result will be the same(still the validator works very well)

if the page is valid then we will change this by adding an onclick event to the button control

 <asp:Button ID="Button1" runat="server" OnClick="Button1_Click"     Text="CHECK REQUIRE FIELD VALIDATION" />


  • In code behind file, we add the below code and run the page 


public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            Button1.Text = "form submitted succefully";
        }
    }
}

Read more »

Validation Controls in Asp.Net

Every web application is a collection of web pages and every web pages is the collection of controls and these controls need to be validation before they are submitted to Server.

What is Validation?

  •  The process of checking proper input for application or page is called validation.

Where the Validation is performed ?

  • Normally we performed the Validation in Client Side using JavaScript. But by using JavaScript to validate the Fields in client Side then it is time consuming to develop in JavaScript and complex.
  • But we can perform the validation in server side also by using app.net controls.
  • In Dot Net provides the mainly  6 types of Validations controls
  1.  Required Field Validation 
  2.  Range Validation
  3.  Compare Validation
  4.  Custom Validation
  5.  Dynamic Validation
  6.  Regular Expression Validation

How asp.net Validation Controls will work ?

  • When user makes a request to web page where a validation control is present.
  • Then a validation controls generates Server side code and also client side code.
  • From the server form that is HTML and JavaScript will be sent to client
  • in client when the user completes the inputs with acceptance from JavaScript code that is client side code then it will go to  server and performs same validation at server

Why Validation is performed in two places (Client Side & Server Side)

When data is validated and submitted to server there a possibility of  malfunctioning our values that is why validation is repeated at server side.

So By using asp.net Validation controls .it takes too much time to validate and burden on server but if the client "Off's the JavaScript in Browser setting" then the java files never work on website so indirectly we depend on asp.net controls 
  
Read more »

Wednesday, 2 November 2016

How To Create Website Using Asp.Net

Creating New Website in asp.net is very easy task. W have only few steps to create the new website

Step 1 : open Visual Studio
Step 2 : Choose File
Step 3 : Select Website


Creating website in aspnetCreating website in aspnet

As show above images, You get two window wizards after that it goes to main page to create website . From Here you can start coding as your wish.

Create Website Using Asp.Net

Read more »

Friday, 28 October 2016

How to Encrypt and Decrypt Any Fields in in ASP.NET or MVC in C#

Hi This Battu ,in this tutorial i show how to Encryption and Decryption the given field in login form, Admin Form or any other forms .When we use the given code then the original field values encrypted in certain format and when you use the value then the values is decrypted , but the value stored in database as encryption format. so hackers cant hack our account as well as if you use sessions then the account gets full tied so hackers cant hack our password or any other data, in same way if you use stored procedure in database site then it is impossible to hack any data of you.

we have two methods in the below code

1) public string Encrypt(string plainText,string encryptionKey)
2) public string Decrypt(string encryptText, string decryptionKey)

in above methods we used two parameters ,the second paramere using for key

using System.IO;
using System.Text;
using System.Security.Cryptography;

the above three namespaces we have import.
now the code shown below if you want use in project just check if it works then use it


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Text;
using System.Security.Cryptography;

namespace battu.common
{
  
    public class Utility
    {
         
        #region Protected Field

        protected string sqlCommandText;

        #endregion

        #region Encrypt & Decrypt Methods

        public string Encrypt(string plainText,string encryptionKey)
        {
            byte[] clearBytes = Encoding.Unicode.GetBytes(plainText);
            using (Aes encryptor = Aes.Create())
            {
                Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(encryptionKey, new byte[]{                   0X49,0X76,0X61,0X6e,0X20,0X4d,0X65,0X64,0X76,0X65,0X64,0X65,0X76 });
                encryptor.Key = pdb.GetBytes(32);
                encryptor.IV = pdb.GetBytes(16);

                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms,                                                   encryptor.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(clearBytes, 0, clearBytes.Length);
                        cs.Close();
                    }

                    plainText = Convert.ToBase64String(ms.ToArray());
                }
            }
            return plainText;
        }

        public string Decrypt(string encryptText, string decryptionKey)
        {
            byte[] encryptBytes = Convert.FromBase64String(encryptText);
            using (Aes encryptor = Aes.Create())
            {
                Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(decryptionKey, new byte[]
                  {
                    0X49,0X76,0X61,0X6e,0X20,0X4d,0X65,0X64,0X76,0X65,0X64,0X65,0X76 });
                encryptor.Key = pdb.GetBytes(32);
                encryptor.IV = pdb.GetBytes(16);

                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms,                                            encryptor.CreateDecryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(encryptBytes, 0, encryptBytes.Length);
                        cs.Close();
                    }

                    encryptText = Encoding.Unicode.GetString(ms.ToArray());
                }
            }
            return encryptText;
        }

        #endregion
    }
}  
Read more »

Wednesday, 26 October 2016

SQL Server Select Insert Update Delete in Single Stored Procedure With Example

  • Store Procedure is the Collection of structure query Language SQL Server.
  • in this tutorial i explain "How to create the Stored Procedure".
  • First we will Perform the DML commands .
  • we have hour DML Commands they are Insert,Delete,Update and Select
  • To create the SP .we can use the any one of the SQL SERVER 2008,2010,2012
  • Stored Procedure Reduces the burden on SQL Server 
  • Now we start "how to create the SP"



1.First create the data base with the name NTierWebFormdDB as shown below,This is First Step

       
create database NTierWebFormDB


2. Create the table with the name Employee with the Five Fields this is step two

       
create table emp
(
EmpId int Not Null Identity(1,1) Primary Key  ,
Empname varchar(30), 
EmpJob varchar(40), 
EmpSalary money not null,
DeptName varchar(100)
)


       

3. For testing purpose test the table by inserting the values as shown below if it works well then go to next step step otherwise check again

       
insert into emp values('Murali','Software','10000','')
select *from emp


4. Create the first Store Procedure withe a Name as Shown Below

       
CREATE PROCEDURE sp_Insertemp
(
@EmpName VARCHAR(30),
@EmpJob VARCHAR(40),
@EmpSalary MONEY,
@DeptName varchar(100)
)
as
begin
insert into emp(EmpJob,Empname,EmpSalary,DeptName)
values(@EmpJob,@EmpName,@EmpSalary,@DeptName)
end

5. Create the Delete Store Procedure as Shown below
    
CREATE PROCEDURE sp_DeleteEmp
(
@EmpId int
)
as
begin
delete from emp where EmpId=@EmpId;
end
select *from emp


6. Now create the select Stored Procedure

CREATE PROCEDURE sp_GetEmp
(
@EmpId int
)
as
begin
select *from emp where EmpId=@EmpId
end


7. Final step is the create the Update stored procedure

       
CREATE PROCEDURE sp_UpdateEmp
(
@Empid int,
@EmpName VARCHAR(30),
@EmpJob VARCHAR(40),
@EmpSalary MONEY,
@DeptName varchar(100)
)
as
begin
update emp set
EmpJob=@EmpJob,
Empname=@EmpName,
EmpSalary=@EmpSalary,
DeptName=@DeptName 
where EmpId=@Empid
end


If you get any doudths on stored procedure in sql server please comment below and if you have any queries personaly then send the email to battusclasses@gmail.com. Dont shy ask any question through email.
Thanq

Read more »

Monday, 24 October 2016

Introduction To WCF (Windows Communication Foundation)

  • It Stands for Windows Communication Foundation
  • it was introduced in ASP.NET 3.0.
  • We can built the Distributed(Connected System) and Inter-operable Applications.
  • Distributed Application (Connected System):
  •  Distributed Application is an Application it runs two or   More Computer Nodes.
  • Inter-operable Application :
  • Inter-operable Application can communicate with other application that was built on any other platform that may developed in java or PHP but it was not work for Remoting Services.
Before WCF ,we have 3 Services
  1.  Enterprise Services
  2.  Dot Net Remoting
  3.  Web Services
Example
 we have two clients
Client 1:
  Requirements are HTTP Protocol and XML message format
Client 2 :
  Requirement are TCP protocol and Binary Format

so above two clients are different if we have one client his requirements are TCP protocol and Binary format but developer should know every thing so Microsoft decided to unify and bring these communication under one root. that is the Windows Communication Foundation





Read more »