sandeep sir

Tuesday, 14 July 2015

Hello frnds today I will explain you what is linq and what is advantage over simple sql query and how to use linq query so lets start with it 

Introduction :
Linq  is stands for Language Integrated Query its named because it looks very similar to c# .With the help of linq we can access various data sources like database and XML .Normally linq expression uses lambda  expression to execute  query statement .It works in similar manner as just like sql  query structure .It start with from keyword . Linq has own it query processing engine .Basically it follows the concept of class and objects .It treats data-sources as object  ,rather than a data base .

Advantages over Sql:

Easy to understand
Language behavioral
Check at compile time rather than runtime  

Basic Syntax Example for Linq Structure :

The code shown below stores the names in an array then selects the names that starts with S.

string[]  names={ "Sandeep" ,"Ram","Shobha","Sonali"};
            
var shortName=from name in names 
                          
where name.StartsWith("S")
                          
select name;
            
foreach(string sname in shortName) 
            
Console.WriteLine("Short Name {0}",sname);             
            
Console.ReadLine(); 

The above code will print all the name starts with  “S” in array format .So it is more expressive way to selecting the names than if we  had to write code without using LINQ .

Output :



So this is the basic structure of linq to understand .In further articles I will explain you more uses of linq and use with database.

Thanks I hope it will helpful for u 

Saturday, 11 July 2015

Hello frnds to day I will explain you how to use entity framework in mvc in this tutorial we will perform crud operation(Create ,Read,Update and Delete) so lets start with this .For this I have table named student ,sql script of my table are shown below.

Sql Script :


CREATE TABLE [dbo].[Student](
      [SID] [int] IDENTITY(1,1) NOT NULL,
      [Std_Name] [varchar](50) NULL,
      [Std_RollNo] [varchar](50) NULL,
      [Std_Address] [varchar](50) NULL,
 CONSTRAINT [PK_Student] PRIMARY KEY CLUSTERED
(
      [SID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

Go
After Create the table add this table into visual studio to create entity framework .

Step 1: Create New Project in Visual Studio.
File->New->Project->Select Asp.net mvc project


Choose Internet Application

Now Create new folder DAL in your website

Right click on DAL folder and select ADO.net entity data model named it with your database name whatever your database name


Click Next


Choose your Table or select all tables I have select student table
Now your table entity and its model class is ready to use

Now create your controller to use this model and classes I have create controller named it Home

Choose template and model class and data context class as shown in below click add button name your controller Home


Now our code is ready to use with complete crud operation.Debug your code press f5

This is very basic article on entity framework ,in further days I will explain more and operation using entity framework.
 Keep enjoying thanks…












Sunday, 5 July 2015


Hello frnds this is my another article on stored procedure in which I will explain  how to get data from database using stored procedure .Also I will use exception handling in this stored procedure for handling runtime error so let’s start  

Step 1: create a database in sql server name it what you want .Now create a table for demo purpose I have create a demotable whose script are shown in the below code

CREATE TABLE COMMAND


CREATE TABLE [dbo].[Demo_User](
      [User_ID] [int] IDENTITY(1,1) NOT NULL,
      [User_Name] [varchar](100) NOT NULL,
      [User_Mail] [varchar](100) NOT NULL,
 CONSTRAINT [PK_demo_user] PRIMARY KEY CLUSTERED
(
      [User_ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO


Now our table is ready to go .
Step 2: Now create stored procedure to get data from it before proceed it please put some data in the table

SELECT STORED PROCEDURE

Alter PROCEDURE sp_Get_DemoUser
     
AS
BEGIN Try
     
select [User_Name],[User_Mail] from Demo_User order by [User_Id] desc

End Try

begin catch
SELECT ERROR_NUMBER() AS ErrorNumber
     ,ERROR_SEVERITY() AS ErrorSeverity
     ,ERROR_STATE() AS ErrorState
     ,ERROR_PROCEDURE() AS ErrorProcedure
     ,ERROR_LINE() AS ErrorLine
     ,ERROR_MESSAGE() AS ErrorMessage;
END CATCH

                   


·         ERROR_NUMBER() returns the number of the error.
·         ERROR_SEVERITY() returns the severity.
·         ERROR_STATE() returns the error state number.
·         ERROR_PROCEDURE() returns the name of the stored procedure or trigger where the error occurred.
·         ERROR_LINE() returns the line number inside the routine that caused the error.
·         ERROR_MESSAGE() returns the complete text of the error message. The text includes the values supplied for any substitutable parameters, such as lengths, object names, or times.

 I hope this will helpful for you .
Thanks

Friday, 3 July 2015


Introduction 

Hello frnds  this is my first  article in sql server today  I will start with create table and insert data into the table using  stored  procedure .As you all well know that how can we directly create table in sql server
And put data into on right click onto the table and using edit table option put some data into table (depends on which version you are using of sql server.)But now we will use stored procedure .There are some benefits of using stored procedure in sql server because it quick in response and more secure from coding point of you once you create procedure you will be able to use it just call by procedure name. So let we start with create a table and after that I will create procedure for inserting data.

Step 1:To create table first right click on database name and open  new  query window and write the following code
//Create Table Command

Create Table Demo_User(

[User_ID] [int] IDENTITY (1,1) not null,
[User_Name] [varchar](100) not null,
[User_Mail] [varchar](100) not null,

 CONSTRAINT [PK_demo_user] PRIMARY KEY CLUSTERED
(
      [User_ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

Now table has been created.

Step 2:Now again open new query editor window to write the stored procedure code for inserting data.


create procedure sp_insert_DemoUser

@User_Name varchar(100),
@User_Mail varchar(100)

as
begin

insert into Demo_User([User_Name],[User_Mail])values (@User_Name,@User_Mail)

end


Here is my simple insert procedure code  Blue one is standard text which we have to write in this manner   “@” is use to declare variable in Stored Procedure  .
So try this way and in next tutorials I will explain you to how to execute stored procedure and use it in C#.
Thanks guys



Thursday, 2 July 2015

Introduction :

Hello friends in this article i will explain to you  what is the basic difference between Web API and WCF(windows communication foundation).so lets start to understand the concepts of web api and wcf ,its uses .

WEB API(Asp.net Web API)


  1. Web API is a framework who works basically  for the HTTP services.
  2. It is very usefull  service for the different type of clients like Mobile , iPhone ,Tab ,Android phone etc.
  3. We are creating a REST (Representational State Transfer) service.
  4. Web API builds the Http Services and it handles the request (GET,POST, PUT,DELETE) through the http protocols.
  5. Web API support MVC features like controller, media formatters , routing etc.
  6. Web Api Represent two type of data format JSON & XML.

                                                       Defined Architecture 
                                             

Uses(When we should have to choose web api in our application):

  • When you want a servic0e which have all the HTTP features such as request header,response header ,URIs versioning in this case you can use the web api.
  • When we have a requirement to use the HTTP services to a broad range of clients like Mobile phone, iPhone and other browsers etc.

What is WCF(windows communication foundation)

Basically wcf is a framework who creates a service oriented application  , it is  hosted on IIS , and it works for the send the data from one end point to another , It send the data asynchronously , wcf provides the process for the secure business transaction,It includes exchanging the data from their service.



Uses


  • When we want to service which supports one way messaging , message queues , duplex   communication etc 
  • When we want a service which uses fast transport channel such as TCP,UDP,Namedpipe etc.


Thanks guys I hope u like this post it will quite helpful for begginers who are looking for this in simple and understanding way 

Saturday, 27 June 2015

Hello frnds ,In this article i will explain you how to bind dropdown list dynamically with database using jquery(ajax,json).
 There are many ways to bind dropdown in mvc .Here i am using jquery to get data from database and show into dropdown to achieve this i am using ,entity framework 5.0 and sql server 2008.
 I have a country table to explain you .Below is Sql Create Command to create table just copy and paste into your data base

SQL Command 


                                 CREATE TABLE [dbo].[Country](
[Country_ID] [int] IDENTITY(1,1) NOT NULL,
[Country_Name] [varchar](200) NULL,
 CONSTRAINT [PK_Country] PRIMARY KEY CLUSTERED 
(
[Country_ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO



Now our table is ready to use just before use it ,out some data into table.




Now Create a DAL folder in your root of the project

And create Entity for country table I feel you are aware from entity framework.Now create Controller with name Home. and create following Code .



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication19.DAL;

namespace MvcApplication19.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        private ContryEntities db = new ContryEntities();


        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public ActionResult GetCountry()
        {
            var country = (from c in db.Countries.ToList() select new { Country_ID = c.Country_ID, Country_Name = c.Country_Name });
            return Json(country, JsonRequestBehavior.AllowGet);
        }
    }
}

Now Create View For this controller :



@{
    ViewBag.Title = "Index";
}
<div style="border:1px;width:50%;padding-left:50%;">
<h2>Index</h2>
<select id="ddlCountry" onchange="onCountry(this)" class="form-control">
    <option>Loading.....</option>
</select>
<input type="hidden" id="Country_Name" name="Country_Name" value="1" />
</div>
@section scripts{
<script>

    function onCountry(e) {
        document.getElementById('Country_Name').value = e.value;
    }
    $(document).ready(function () {
        $.ajax({
            cache: true,
            type: "GET",
            url: "/Home/GetCountry",
            success: function (data) {
                $("#ddlCountry").html('');
                $.each(data, function (id, option) {
                    $("#ddlCountry").append($('<option></option>').val(option.Country_ID).html(option.Country_Name));
                });
            }
        });

    });
</script>
    }
Dropdown With Data
Finally Dropdown is bind, i hope u will like it. if any suggestion so plz mention in comment ok thanks.






Friday, 19 June 2015

Hello frnds ,in this article i will show you  how to use client side validation in MVC 4,without any page refresh we can validate data .Before start it i wish to inform you that visual studio 2012 already has client side validation enabled in web.config page but you can easily enabled or disabled this code simply by writing this code


<configuration> <appSettings> <add key="ClientValidationEnabled" value="true" /> <add key="UnobtrusiveJavaScriptEnabled" value="true" /> </appSettings> </configuration>
Now Add New Class in Model Folder and add the following code in it
using System.ComponentModel.DataAnnotations;
namespace ClientSideValidation.Models
{
    public class Test
    {
        [Required(ErrorMessage = "Name is Required")]
        public string Name { get; set; }
        [Required(ErrorMessage = "Email is Required")]
        [RegularExpression(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
                            @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
                            @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$",
                            ErrorMessage = "Email is not valid")]
        public string Email { get; set; }
    }
}
Now Add Associate View which Should be strongly type view as shown in picture below


Thanks and keep enjoying...