Saturday, 18 March 2017

Insert Data on Server using Web Methods in Asp.Net


Database
Create a Table in Database

create table insert_user
(
id int primary key identity(1,1),
Name varchar(20),
contact_number varchar(20),
email varchar(20),
username varchar(10),
password varchar(10),
gender varchar(10),
country varchar(10)
)

Create Stored Procedure to insert data in table

create proc insert_sp_user
@Name varchar(20),
@contact_number varchar(20),
@email varchar(20),
@username varchar(10),
@password varchar(10),
@gender varchar(10),
@country varchar(10),
@success int out
as
begin
insert into insert_user(Name,contact_number,email,username,password,gender,country) values(@Name, @contact_number, @email, @username, @password,@gender,@country)
end




Web.Config
<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <appSettings>
    <add key="constr" value="Database=******; Initial catalog=*****; user id=*****; password=*****;"/>
    <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5.1"/>
    <httpRuntime targetFramework="4.5.1"/>
  </system.web>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs"
        type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701"/>
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
        type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"/>
    </compilers>
  </system.codedom>
</configuration>


BusinessLayer

Add a class with the name BusinessLayer.

 public class BusinessLayer
    {
        string constring = ConfigurationManager.AppSettings["constr"].ToString();
        public int Add_user(string name,Int64 contact,string email,string username, string password,string gender, string country)
        {
            using (SqlConnection con = new SqlConnection(constring))
            using (SqlCommand cmd = new SqlCommand("insert_sp_user", con))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                SqlParameter outputvalue = new SqlParameter("@success", SqlDbType.Int);
                outputvalue.Direction = ParameterDirection.Output;
                cmd.Parameters.AddWithValue("@Name", name);
                cmd.Parameters.AddWithValue("@contact_number", contact);
                cmd.Parameters.AddWithValue("@email", email);
                cmd.Parameters.AddWithValue("@username", username);
                cmd.Parameters.AddWithValue("@password", password);
                cmd.Parameters.AddWithValue("@gender", gender);
                cmd.Parameters.AddWithValue("@country", country);
                cmd.Parameters.Add(outputvalue);
                con.Open();
                cmd.ExecuteNonQuery();
                int result = 1;
                if (outputvalue.Value != DBNull.Value)
                    result = Convert.ToInt32(outputvalue.Value);
                return result;
            }
        }
    }

User.aspx



Add a web frm with the name User
Insert Jquery reference on the form.


<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="Scripts/jquery-1.10.2.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#<%=btn_submit.ClientID%>").click(function () {
                debugger;
                var name = $("#<%=txt_name.ClientID%>").val();
                var contact = $("#<%=txt_contact.ClientID%>").val();
                var email = $("#<%=txt_email.ClientID%>").val();
                var username = $("#<%=txt_username.ClientID%>").val();
                var password = $("#<%=txt_password.ClientID%>").val();
                var gender = "";
                if ($("#<%=rdbmale.ClientID%>").is(":checked"))
                    gender = "Male";
                else
                    gender = "Female";

                var country = $("#<%=ddl_country.ClientID%> option:selected").text();
                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: "User.aspx/user_insertion",
                    data: '{"name":"' + name + '","contact":"' + contact + '","email":"' + email + '","username":"' + username + '","password":"' + password + '","gender":"' + gender + '","country":"' + country + '"}',
                    dataType: "json",
                    success: function (data) {
                        debugger;
                        alert("Insert Successfully");
                    }
                });
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <table>
            <tr>
                <td>

               
    <asp:Label ID="lbl_name" Text="Name" runat="server">
    </asp:Label>
                    </td>
                <td>
        <asp:TextBox ID="txt_name" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="rfv_name" runat="server" ControlToValidate="txt_name" ErrorMessage="Enter Name Please"></asp:RequiredFieldValidator>
                     </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="lbl_contact" runat="server" Text="Contact Number"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txt_contact" runat="server" TextMode="Number"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="rfv_contact" runat="server" ControlToValidate="txt_contact" ErrorMessage="Enter Contact Number"></asp:RequiredFieldValidator>
                </td>
            </tr>
          <tr>
                <td>

               
    <asp:Label ID="lbl_email" Text="Email Address" runat="server">
    </asp:Label>
                    </td>
                <td>
        <asp:TextBox ID="txt_email" runat="server" TextMode="Email"></asp:TextBox>
        <asp:RequiredFieldValidator ID="rfv_email" runat="server" ControlToValidate="txt_email" ErrorMessage="Enter Email Address Please"></asp:RequiredFieldValidator>
                     </td>
            </tr>
         <tr>
                <td>

               
    <asp:Label ID="lbl_username" Text="User Name" runat="server">
    </asp:Label>
                    </td>
                <td>
        <asp:TextBox ID="txt_username" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="rfv_username" runat="server" ControlToValidate="txt_username" ErrorMessage="Enter UserName Please"></asp:RequiredFieldValidator>
                     </td>
            </tr>
          <tr>
                <td>

               
    <asp:Label ID="lbl_password" Text="Password" runat="server">
    </asp:Label>
                    </td>
                <td>
        <asp:TextBox ID="txt_password" runat="server" TextMode="Password"></asp:TextBox>
        <asp:RequiredFieldValidator ID="rfv_password" runat="server" ControlToValidate="txt_password" ErrorMessage="Enter password Please"></asp:RequiredFieldValidator>
                     </td>
            </tr>
         <tr>
                <td>
                    <asp:Label ID="lbl_gender" runat="server" Text="Gender"></asp:Label>
                </td>
                <td>
                    <asp:RadioButton ID="rdbmale" runat="server" Text="Male" GroupName="Gender" Checked="true" />
                    <asp:RadioButton ID="rdb_female" runat="server" Text="Female" GroupName="Gender" />
                </td>
            </tr>
          <tr>
                <td>
                    <asp:Label ID="lbl_state" runat="server" Text="State"></asp:Label>
                </td>
                <td>
                    <asp:DropDownList ID="ddl_country" runat="server">
                        <asp:ListItem>India</asp:ListItem>
                        <asp:ListItem>USA</asp:ListItem>
                        <asp:ListItem>Newzeland</asp:ListItem>
                        <asp:ListItem>UK</asp:ListItem>
                        <asp:ListItem>Sri Lanka</asp:ListItem>
                    </asp:DropDownList>
                </td>
            </tr>
          <tr>
                <td></td>
                <td>
                    <asp:Button ID="btn_submit" runat="server" Text="Submit"/>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>


User.aspx.cs

Add namespace for the web [WebMethod] using System.Web.Services;


 [WebMethod]
        public static string user_insertion(string name, Int64 contact,string email,string username, string password, string gender,string country)
        {
            BusinessLayer objbusiness = new BusinessLayer();
            string status = "true";
            int result = objbusiness.Add_user(name, contact, email, username, password, gender, country);
            if(result==0)
            {
                status = "false";
            }
            return status;
        }







Thursday, 16 March 2017

Bind Data in DropDown Dynamically using Web Methods in Asp.Net

Database


1> Create a Table in SQL with the name country.
2> Insert atleast 5 country name within the table.
3>Create a stored procedure with the name

Create proc country_bind_sp
as
begin
select id,country from country
end


Web.Config File

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <appSettings>
    <add key="constr" value="Data source=*****;Initial catalog=*****; user id=*****; password=*****;"/>
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5.1"/>
    <httpRuntime targetFramework="4.5.1"/>
  </system.web>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs"
        type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701"/>
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
        type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"/>
    </compilers>
  </system.codedom>
</configuration>




Business Layer

Create a class with the name BusinessLayer to write the ADO Code.


using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace DropDownBind
{
    public class BusinessLayer
    {
        string constring = ConfigurationManager.AppSettings["constr"].ToString();
        public DataTable countrybind()
        {
            using (SqlConnection con = new SqlConnection(constring))
            using (SqlCommand cmd = new SqlCommand("country_bind_sp", con))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                DataTable dt = new DataTable();
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                adapt.Fill(dt);
                return dt;
            }
        }
    }
}


Web Form

ADD Web Form in Project with the name Country

Country.Aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Country.aspx.cs" Inherits="DropDownBind.Country" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="Scripts/jquery-1.10.2.min.js"></script>
    <script src="Scripts/jquery-1.10.2.js"></script>
    <script type="text/javascript">

        $(document).ready(function () {
            binding();
        });

        function binding()
        {
            debugger
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "Country.aspx/BindCountry",
                data: "{}",
                dataType: "json",
                success: function (data) {
                    debugger;
                        $("#<%=ddl_country.ClientID %>").append($("<option></option>").val(0).html("-Select SubCategory-"));
                    $.each(data.d, function (key, value) {
                        $("#<%=ddl_country.ClientID %>").append($("<option></option>").val(value.DataValueField).html(value.DataTextField));
                    });
                },
                error: function (result) { }
            });
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:DropDownList ID="ddl_country" runat="server"></asp:DropDownList>
    </div>
    </form>
</body>
</html>


Country.Aspx.cs


  [WebMethod]
        public static Country1[] BindCountry()
        {
            List<Country1> cntry = new List<Country1>();
            BusinessLayer objbusiness = new BusinessLayer();
            DataTable dt = objbusiness.countrybind();
           for(int i=0;i<dt.Rows.Count;i++)
            {
                Country1 cont = new Country1();
                cont.DataValueField = Convert.ToInt32(dt.Rows[i]["id"]);
               cont.DataTextField = dt.Rows[i]["country"].ToString();
                cntry.Add(cont);
            }
            return cntry.ToArray();
        }

       
    }
    public class Country1
    {
        public int DataValueField { get; set; }
        public string DataTextField { get; set; }
    }
}

If you have any query please post in the comment
                                                                       sharmarahulsoftwareengineer.blogspot.in

Sunday, 12 February 2017

Visual Studio:-


Microsoft Visual Studio is an integrated development environment (IDE) from Microsoft. It is used to develop computer programs for Microsoft Windows, as well as web sites, web applications and web services.

Visual Studio supports different programming languages and allows the code editor and debugger to support (to varying degrees) nearly any programming language, provided a language-specific service exists. Built-in languages include C, C++ and C++/CLI (via Visual C++), VB.NET (via Visual Basic .NET), C# (via Visual C#), and F# (as of Visual Studio 2010). Support for other languages such as Python,[ RubyNode.js, and M among others is available via language services installed separately. It also supports XML/XSLTHTML/XHTMLJavaScript and CSS. Java (and J#) were supported in the past.


Versions of Visual Studio:-

Product nameCodenameVersion numberVersion of
cl.exe
Supported .NET
Framework versions
Release date
Visual Studio 97Boston5.0N/AN/AFebruary 1997
Visual Studio 6.0Aspen6.012.00N/AJune 1998
Visual Studio .NET (2002)Rainier7.013.001.0February 13, 2002
Visual Studio .NET 2003Everett7.113.101.1April 24, 2003
Visual Studio 2005Whidbey8.014.002.0, 3.0November 7, 2005
Visual Studio 2008Orcas9.015.002.0, 3.0, 3.5November 19, 2007
Visual Studio 2010Dev10/Rosario10.016.002.0 – 4.0April 12, 2010
Visual Studio 2012Dev1111.017.002.0 – 4.5.2September 12, 2012
Visual Studio 2013Dev1212.018.002.0 – 4.5.2October 17, 2013
Visual Studio 2015Dev1414.019.002.0 – 4.6July 20, 2015
Visual Studio 2017[64]Dev1515.019.102.0 – 4.6.2; Core 1.0Not released

Thursday, 9 February 2017

MVC (Model View Controller):-


Model:-
              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:-
         Refers to the components that define an application's user interface.

Controller:-
                Refers to a set of classes that handle communication from the user and the overall application flow.