Saturday, 11 March 2017

How to create a simple Hello World website in ASP.NET MVC using Razor Syntax:

(Note: I am using Visual Studio 2012 )


Step 1:
Create a new empty mvc project in razor syntax.

Fileà Newà Project à Select Web in the left side panel à ASP .NET MVC4 Web applicationàEnter the name as follows”SimpleHelloWorld”à Then Click ‘OK’.



Step 2:

Select which type of view engine you want to use.

EmptyàRazorà Click ‘Ok’.


(Note:If you have made an testing select the check box near create a unit test project.)


Step 3: 

View Solution Explorer its created many folders named as App_Data,App_Start,Controllers,Models,Views.... etc.

Model: In this folder used to create the database or Class Model files.It refers to a set of classes that describes the data that the application works with.In addition these classes define the business logic that governs how the data can be manipulated.

View: In this folder used to create design and view engine files.It refers to the components that define an application’s user interface.For example, the login form that contains text boxes and buttons.

Controllers: In this folder used to create the class files(.cs).It refers to a set of classes that handle communication from the user and the overall applicaion flow.A controller responds to user input,communicates with the model, and decides the view to render.

App_Start:
RouteConfig.cs – Route map of the wep application.

Ends with .cshtml - These files contain the user interface code.

Ends with .cs - These files contain C# code.

Step 4: 

Creating new “HomeController”

Right Click Controllers FolderàAddà ControlleràRename Controller name as follows “HomeController”.



Step 5: 

Create a new folder name “Home”.

Right Click Viewsà Addà New Folderà Home


Step 6:

Now create a new view inside home folder .

Right Click Home Folderà Addà Viewà Rename view name as follows “Index”à Click Add.




Step 7: 

Now double tab the HomeController.cs file .Enter code below and save it.

ViewBag.Message=”Simple Hello World”;


ViewData:

ViewData is a dictionary of objects from the System.Web.Mvc.ViewDataDictionary class.It enables you to pass the data between a controller and view.Here ,values can be set using key/value pairs in the controller action method ,as shown in the following code snippet:

ViewData[“Message”]=”Welcome to our website”;
ViewData[“ServerTime”]=”DateTime.Now”;

You can acess the values added in the preceding code by using the Razor syntax, as shown below.

<p>
The Message is:@ViewData[“Message”]
The Date and Time is :@ViewData[“ServerTime”]
</p>

ViewBag:

ViewBag is a dynamic object that allows passing data between a controller and a view.You can add properties of multiple types and their values by using the ViewBag object in a controller action method.The syntax of the code snippet:

ViewBag.Message=”Welcome to our website”;
ViewBag.ServerTime=DateTime.Now;

You can acess the values added in the preceding code by using the Razor syntax, as shown below.

<p>
The Message is:@ViewBag.Message
The Date and Time is :@ViewBag.ServerTime
</p>


Step 8: 

Then double tab the Index.cshtml file.Enter code in <p> tag in below and save it.

<div>
<p>
@ ViewBag.Message
</p>
</div>


Step 9:  

Now go to App_Start folder double tab the RouteConfig.cs file.

Check the below code snippet .

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }

The above code represents and checks both folders.

Controller=”Home”-
Action=”Index”


Step 10: 

 Finally run the application the output will be.


Download Source File below link :


https://drive.google.com/file/d/0B9wsZ0ZE_dezTzhVaWtrdDhEWFU/view?usp=sharing

Friday, 23 December 2016

Checking Duplicate entry while insert in Database And Entire Gridview rows :  

Aspx Page: 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="samp.aspx.cs" Inherits="samp" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            height: 23px;
        }
        .auto-style3 {
            height: 23px;
            width: 161px;
            text-align: right;
        }
        .btn {
  background: #3498db;
  background-image: -webkit-linear-gradient(top, #3498db, #2980b9);
  background-image: -moz-linear-gradient(top, #3498db, #2980b9);
  background-image: -ms-linear-gradient(top, #3498db, #2980b9);
  background-image: -o-linear-gradient(top, #3498db, #2980b9);
  background-image: linear-gradient(to bottom, #3498db, #2980b9);
  -webkit-border-radius: 28;
  -moz-border-radius: 28;
  border-radius: 28px;
  font-family: Arial;
  color: #ffffff;
  font-size: 20px;
  padding: 10px 20px 10px 20px;
  text-decoration: none;
}

.btn:hover {
  background: #3cb0fd;
  background-image: -webkit-linear-gradient(top, #3cb0fd, #3498db);
  background-image: -moz-linear-gradient(top, #3cb0fd, #3498db);
  background-image: -ms-linear-gradient(top, #3cb0fd, #3498db);
  background-image: -o-linear-gradient(top, #3cb0fd, #3498db);
  background-image: linear-gradient(to bottom, #3cb0fd, #3498db);
  text-decoration: none;
}
.enjoy-css {
  display: inline-block;
  -webkit-box-sizing: content-box;
  -moz-box-sizing: content-box;
  box-sizing: content-box;
  padding: 10px 20px;
  border: 1px solid #b7b7b7;
  -webkit-border-radius: 3px;
  border-radius: 3px;
  font: normal 20px/normal Verdana, Geneva, sans-serif;
  color: rgba(0,142,198,1);
  -o-text-overflow: clip;
  text-overflow: clip;
  background: rgba(252,252,252,1);
  -webkit-box-shadow: 2px 2px 2px 0 rgba(0,0,0,0.2) inset;
  box-shadow: 2px 2px 2px 0 rgba(0,0,0,0.2) inset;
  text-shadow: 1px 1px 0 rgba(255,255,255,0.66) ;
  -webkit-transition: all 200ms cubic-bezier(0.42, 0, 0.58, 1);
  -moz-transition: all 200ms cubic-bezier(0.42, 0, 0.58, 1);
  -o-transition: all 200ms cubic-bezier(0.42, 0, 0.58, 1);
  transition: all 200ms cubic-bezier(0.42, 0, 0.58, 1);
}
        .auto-style9 {
            height: 57px;
            width: 161px;
            text-align: right;
            font-family: "Lucida Sans", "Lucida Sans Regular", "Lucida Grande", "Lucida Sans Unicode", Geneva, Verdana, sans-serif;
            color: #000080;
        }
        .auto-style11 {
            height: 57px;
        }
        .auto-style15 {
            height: 49px;
        }
        .auto-style16 {
            height: 23px;
            width: 366px;
        }
        .auto-style17 {
            height: 57px;
            width: 366px;
        }
        .auto-style18 {
            height: 49px;
            width: 366px;
        }
        .auto-style19 {
            width: 366px;
        }
        .auto-style20 {
            height: 23px;
            width: 161px;
        }
        .auto-style21 {
            height: 57px;
            width: 161px;
        }
        .auto-style22 {
            height: 49px;
            width: 161px;
        }
        .auto-style23 {
            width: 161px;
        }

        .auto-style24 {
            color: #CC9900;
        }

    </style>
</head>
<body>
    <form id="form1" runat="server">
   <center>

        <div>
            <h1 class="auto-style24">
               Insert Data And Check Duplicate In Both Database And Gridview
            </h1>
        </div>
    </center>

    <div>
        <table style="width: 100%;">
            <tr>
                <td class="auto-style16"></td>
                <td class="auto-style3"></td>
                <td class="auto-style20"></td>
                <td class="auto-style1"></td>
            </tr>
            <tr>
                <td class="auto-style17"></td>
                <td class="auto-style9" >ID</td>
                <td class="auto-style21">
                    <asp:TextBox ID="TextBox1" runat="server" Height="23px" Width="224px" CssClass="enjoy-css"></asp:TextBox>
                </td>
                <td class="auto-style11"></td>
            </tr>
            <tr>
                <td class="auto-style18"></td>
                <td class="auto-style9">NAME</td>
                <td class="auto-style22">
                    <asp:TextBox ID="TextBox2" runat="server" Height="23px" Width="224px" CssClass="enjoy-css"></asp:TextBox>
                </td>
                <td class="auto-style15"></td>
            </tr>
            <tr>
                <td class="auto-style16">&nbsp;</td>
                <td class="auto-style3">&nbsp;</td>
                <td class="auto-style20">
                    &nbsp;</td>
                <td class="auto-style1">&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style16"></td>
                <td class="auto-style3">
                   
                </td>
                <td class="auto-style20">
                    
                </td>
                <td class="auto-style1">
                    </td>
            </tr>
            <tr>
                <td class="auto-style19">&nbsp;</td>
                <td class="auto-style23">&nbsp;</td>
                <td class="auto-style23">&nbsp;</td>
                <td>&nbsp;</td>
            </tr>
        </table>
    </div>
        <center>
        <div>
             <asp:Button ID="Button1" runat="server" Text=" Insert & Check Database" CssClass="btn"  OnClick="Button1_Click" />&nbsp;&nbsp;&nbsp;
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" CssClass="btn" Text=" Insert & Check Gridview" />
        </div>
            </center>
        <Center>

        <div>

            &nbsp;

            <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" Width="389px">
                <AlternatingRowStyle BackColor="White" />
                <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
                <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
                <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
                <SortedAscendingCellStyle BackColor="#FDF5AC" />
                <SortedAscendingHeaderStyle BackColor="#4D0000" />
                <SortedDescendingCellStyle BackColor="#FCF6C0" />
                <SortedDescendingHeaderStyle BackColor="#820000" />
            </asp:GridView>
        </div>
                    </Center>

    </form>
</body>
</html>


C# Page : 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
using System.Data;
using System.Configuration;
using System.Globalization;
using System.IO;
using System.Drawing;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Text;
using System.Collections.Specialized;
using System.Web.Services;

public partial class samp : System.Web.UI.Page
{
    MySqlConnection con1 = new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["constring"].ConnectionString);

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bind();
        }


    }
    protected void Button1_Click(object sender, EventArgs e)
    {
                    con1.Open();
                    MySqlCommand cmd= new MySqlCommand("Select count(*) from sample where id='"+TextBox1.Text+"' and name='"+TextBox2.Text+"'", con1);
                    int count = Convert.ToInt32(cmd.ExecuteScalar());
                    if (count == 0)
                    {

                        MySqlCommand cmdl = new MySqlCommand("insert into sample(id,name)values('" + TextBox1.Text + "','" + TextBox2.Text + "')", con1);
                        cmdl.ExecuteNonQuery();
                        this.bind();
                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertmessage", "alert('Inserted Sucessfully')", true);
                        TextBox1.Text = "";
                        TextBox2.Text = "";
                        con1.Close();
                    }
                    else
                    {
                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertmessage", "alert('Already Exits.')", true);
                        TextBox1.Text = "";
                        TextBox2.Text = "";
                        con1.Close();
                    }

    }
    protected void bind()
    {
  
        MySqlCommand cmd = new MySqlCommand("select upper(id) as 'ID' ,upper(name) as 'NAME' from sample", con1);
        MySqlDataAdapter da = new MySqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();


    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        bool entryFound = false;
        foreach (GridViewRow row in GridView1.Rows)
        {

            string val4 = row.Cells[0].Text;
       
        

            if (val4 != null && val4.ToString() ==TextBox1.Text)
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertmessage", "alert('Already Exits !')", true);
                entryFound = true;
                break;
            }

        }

        if (!entryFound)
        { 
                        con1.Open();
                        MySqlCommand cmdl = new MySqlCommand("insert into sample(id,name)values('" + TextBox1.Text + "','" + TextBox2.Text + "')", con1);
                        cmdl.ExecuteNonQuery();
                        this.bind();
                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertmessage", "alert('Inserted Sucessfully')", true);
                        TextBox1.Text = "";
                        TextBox2.Text = "";
                        con1.Close();
        }
         else
            {
                 ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertmessage", "alert('Already Exits.')", true);
                        TextBox1.Text = "";
                        TextBox2.Text = "";
                        con1.Close();
            }
        }

    }



Screen Shots : 


 Showing Error message :







Download Source File with Table Structure :


     

Tuesday, 22 November 2016

HOW TO DEPLOY ASP .NET WEBSITE IN A LOCAL COMPUTER USING IIS(7).

Step 1:  Install the IIS setup in your local computer. Double click the file named ”inetmgr.exe “.

Here is the link:

32 Bit OS:


64 Bit OS:


All :


             Install IIS in your local system .While installing its showing some Framework console error.


 (Note: So follow below steps).

Step 2 : Now  Turn on the windows featues and install the IIS in your local computer .

          To learn how to enable IIS and the required IIS components on Windows 7,8/8.1, see the instructions below.
  1. Open Control Panel and click Programs and Features > Turn Windows features on or off.
  2. Enable Internet Information Services.
  3. Expand the Internet Information Services feature and verify that the web server components listed below are enabled.
  4. Click OK.

Required IIS components
          The IIS components listed below satisfy the minimum requirements to run the Web Adaptor. If other IIS components are enabled, they do not need to be removed.
  • Web Management Tools
    • IIS 6 Management Compatibility
      • IIS Metabase and IIS 6 configuration compatibility
    • IIS Management Console
    • IIS Management Scripts and Tools
    • IIS Management Service
  • World Wide Web Services
    • Application Development Features
      • .NET Extensibility 4.5
      • ASP.NET 4.5
      • ISAPI Extensions
      • ISAPI Filters
    • Common HTTP Features
      • Default Document
      • Static Content
    • Security
      • Basic Authentication
      • Request Filtering
      • Windows Authentication.
Step 3 :After installing IIS you need to install Framework 4.0 for running purpose in a website.

       1.run the command prompt as administrator
       2.type the line in the command prompt below any one

      %windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -i

                                                      or

     %windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_regiis.exe -i

      dont change the directory of cmd.



Step 4 :Then create a folder in exact location.

C:\inetpub\wwwroot\your folder name.

Step 5 :Open your website in Visual Studio.

1.Right click the website “ Publish Web Site”.

2. Select the physical path ” C:\inetpub\wwwroot\your folder name.”

Step 6 :Now open the IIS (or) Internet Information Services (IIS) Manager.

1. In Run command enter the keyword “inetmgr”.

2. Control Panel\All Control Panel Items\Administrative Tools\ Internet Information Services (IIS) Manager.




3.Right click the Sites à Add Web Site.Enter details like below image.



4. Select the default web page .Like name it “ Default.aspx”.




5.Set the Application Pool in Framework 2.0 into Framework 4.0.Enter the name it shows the ok button.


6.After adding the website .Now Right click the websiteà Manage the website à Advance Settings.


7 .Select the Framework 4.0.


8 .Now right click the website à manage web site àbrowse the website .Enjoy the website.

Sample Website with table structure :
Download below link :


How to create a simple Hello World website in ASP.NET MVC using Razor Syntax: (Note: I am using Visual Studio 2012 ) Step 1: ...