Wednesday, 28 February 2018

Insert, Update, Delete, Search, Display, Single Click Insert Delete Both (Single click many operation) In GridView Using ASP.Net C#

Insert, Update, Delete, Search, Display, Single Click Insert Delete Both (Single click many operation) In GridView Using ASP.Net C#

******************** SQL Command *********************

 
create database test
create table new(name varchar (20)
                           ,age int
                           ,mobile bigint
                           )

********************* .aspx ********************



<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="Name"></asp:Label>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />

        <asp:Label ID="Label2" runat="server" Text="Age"></asp:Label>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Textbox ID="textbox2" runat="server"></asp:Textbox><br />

        <asp:Label ID="Label3" runat="server" Text="Mobile"></asp:Label>
        &nbsp;&nbsp;
        <asp:Textbox ID="textbox3" runat="server"></asp:Textbox><br /><br />
       
        <asp:Button ID="Button1" runat="server" Text="Insert" OnClick="Insert" />&nbsp;&nbsp;
        <asp:Button ID="Button2" runat="server" Text="Update" OnClick="Update" />&nbsp; &nbsp;
        <asp:Button ID="Button3" runat="server" Text="Delete" OnClick="Delete" />&nbsp;&nbsp;
        <asp:Button ID="Button5" runat="server" Text="Display" OnClick="Display" />&nbsp;&nbsp;
        <asp:Button ID="Button6" runat="server" Text="Search" OnClick="Search" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
        <asp:Button ID="Button7" runat="server" Text="Single_Click_Insert_Delete_Both" OnClick="Single_Click_Insert_Delete_Both" />&nbsp;&nbsp;
        &nbsp;<br />
        &nbsp; <br />

        <asp:GridView ID="GridView1" runat="server"></asp:GridView>
    </div>
    </form>
</body>
</html>

********************** .cs  *******************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(@"Data Source=localhost;Initial Catalog=test;Integrated Security=True");
 public void display()
    {
        con.Open();
        SqlCommand cmd = con.CreateCommand();
        cmd.CommandText = "select * from new";
        cmd.ExecuteNonQuery();

        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
        con.Close();
    }
    public void delete()
    {
        con.Open();
        SqlCommand cmd = con.CreateCommand();
        cmd.CommandText = "delete from new where name='" + TextBox1.Text + "'";
        cmd.ExecuteNonQuery();
        con.Close();
        display();
    }
 protected void Insert(object sender, EventArgs e)
    {
        con.Open();
        SqlCommand cmd = con.CreateCommand();
        cmd.CommandText = "insert into new values('" + TextBox1.Text + "','" + textbox2.Text + "','" + textbox3.Text + "')";
        cmd.ExecuteNonQuery();
        con.Close();
        display();
    }

    protected void Update(object sender, EventArgs e)
    {
        con.Open();
        SqlCommand cmd = con.CreateCommand();
        cmd.CommandText = "update new set name='" + TextBox1.Text + "',mobile='" + textbox3.Text + "' where age='" + textbox2.Text + "'";
        cmd.ExecuteNonQuery();
        con.Close();
    }

    protected void Delete(object sender, EventArgs e)
    {
        con.Open();
        SqlCommand cmd = con.CreateCommand();
        cmd.CommandText = "delete from new where name='" + TextBox1.Text + "'";
        cmd.ExecuteNonQuery();
        con.Close();
        display();
    }

    protected void Display(object sender, EventArgs e)
    {
        display();
    }

    protected void Search(object sender, EventArgs e)
    {
        con.Open();
        SqlCommand cmd = con.CreateCommand();
        cmd.CommandText = "select * from new where name='" + TextBox1.Text + "'";
        cmd.ExecuteNonQuery();

        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
        con.Close();
    }

    protected void Single_Click_Insert_Delete_Both(object sender, EventArgs e)
    {
    con.Open();
    SqlCommand cmd = con.CreateCommand();
    cmd.CommandText = "insert into new values('" + TextBox1.Text + "','" + textbox2.Text + "','" + textbox3.Text + "')";
    // cmd.ExecuteNonQuery();
    // con.Close();
    // display();


    //con.Open();
    // SqlCommand cmd = con.CreateCommand();

    cmd.CommandText = "delete from new where name='" + TextBox1.Text + "'";
    cmd.ExecuteNonQuery();
    con.Close();
    // delete();
    display();
   }

}




Sunday, 25 February 2018

How to insert a variable value into a table in SQL

How to insert a variable value into a table in SQL

declare @x int;
set @x=100-1;


declare @z int=1000-1;
 

create table newt(b int, c varchar(10),a int, d varchar(20))
insert into newt values(10, 'san', @x, @z)
select * from newt 

 

 

Tuesday, 13 February 2018



Models in an mvc application


2-add controller 
3-controller
 
 4-add view and (select modelclass name)


5

6
 
7
 
Select, Insert, Update, Delete Using Stored Procedure in SQL Server 2008


Alter PROCEDURE MasterInsertUpdateDelete   
(   
@id INTEGER,   
@first_name VARCHAR(10),   
@last_name VARCHAR(10), 
 @salary DECIMAL(10,2),   
@city VARCHAR(20),   
@StatementType nvarchar(20) = ''   
 
AS   
BEGIN 

IF @StatementType = 'Insert'   
BEGIN   
insert into employee (id,first_name,last_name,salary,city) values( @id, @first_name, @last_name, @salary, @city)   END   

IF @StatementType = 'Select'   
BEGIN   
select * from employee   
END   

IF @StatementType = 'Update'   
BEGIN   
UPDATE employee SET   
First_name = @first_name, last_name = @last_name, salary = @salary,   
city = @city   
WHERE id = @id   
END   

else IF @StatementType = 'Delete'   
BEGIN   
DELETE FROM employee WHERE id = @id   
END   

end

http://www.c-sharpcorner.com/UploadFile/rohatash/select-insert-update-delete-using-stored-procedure-in-sql/ 

Required field validator in asp.net





Registration form in HTML using JAVASCRIPT validation
 
<html>
    <head>
        <title> Validation using JavaScript </title>
       
    <script language="javascript">
     function validation()
        {
            var x=f.un.value;
            var len=x.length;
            var val=x.charCodeAt(0);
           
            var p=f.pw.value;
            var c=f.cpw.value;
           
            var a=f.email.value;
            var atpos=a.indexOf("@");
            var dotpos=a.lastIndexOf(".");

            if(len<6 || x=='' || x==null)
            {
                alert("check your username ! It must be minimum 6 characters");
                if(val<65 || val>90 && val<97 || val>122)
                alert("username must begin with an alphabet");
                return false;
            }
            else if(val<65 || val>90 && val <97 || val>122)
            {
                alert("username must begin with an alphabet");
                return false;
            }
            else if(p=='' || c=='' || p.length<6 || c.length<6 || p!=c)
            {
                alert("Password and Confirm Password should be same and greater than 6 characters!");
                return false;
            }
            else if(atpos<6 || dotpos<atpos+6 || dotpos+2>=a.length)
            {
                alert("Not a valid email id !!");
                return false;
            }
            else alert("Congragulations! you have successfully registered...");
        }
      
    </script>
</head>

<body>
        <h1 align=center>Welcome!! </h1>
        <h2 align=center>Please register Here...</h2>
        <hr width=50%>

    <table align=center border=3>

        <form name="f" action="home.html" onsubmit="return validation();" method="post">

            <tr>
                <td>User Name:</td>
                <td><input type="text" name="un"/></td>
            </tr>
            <tr>
                <td>Password:</td>
                <td><input type="password" name="pw"/></td>
            </tr>
            <tr>
                <td>Confirm Password:</td>
                <td><input type="password" name="cpw"/></td>
            </tr>
            <tr>
                <td>E-mail ID:</td>
                <td><input type="text" name="email"/></td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" value="Submit"/></td>
            </tr>
        </form>
    </table>
</body>
</html>



Wednesday, 25 February 2015

Java (Characteristics of Java, How Do JSP Pages Work?, Servlets Architecture:)


Java is a programming language originally developed by James Gosling at Sun Microsystems (which is now a subsidiary of Oracle Corporation) and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities. Java applications are typically compiled to bytecode (class file) that can run on any Java Virtual Machine (JVM) regardless of computer architecture. Java is a general-purpose, concurrent, class-based, object-oriented language that is specifically designed to have as few implementation dependencies as possible. It is intended to let application developers "write once, run anywhere". Java is currently one of the most popular programming languages in use, particularly for server-client web applications.



 Characteristics of Java


Platform neutral language – Java programs are platform neutral, this means program written in Java can be used on any computer running any operating system, this is possible by the help of java virtual machine.

Object-orientated programming language – In java programming language all the elements are object except the primitive data types.

Strongly-typed programming language -  Java is strongly-typed programming language, means we must defined the type of the variable before using it and conversion to other objects is relatively strict, it must be done in most cases explicitly by the programmer.

Compiled and Interpreted language – Usually a computer programming language can be either compiled or interpreted, but in case of  Java both of the approach is combined, thus it makes java a two stage system. In first stage source code is transferred into the bytecode format which is platform neutral. These bytecode instructions will be interpreted by the Java Interpreter in second stage.

Automatic memory management -  Java programming language has the in built memory management capabilities, thus allocation and de-allocation for creating new objects is handled by java. Java program does not have direct access to the memory. There is a garbage collector deletes objects automatically which is not active in the program.

JavaServer Pages (JSP) is a server-side programming technology that enables the creation of dynamic, platform-independent method for building Web-based applications. JSP have access to the entire family of Java APIs, including the JDBC API to access enterprise databases.



How Do JSP Pages Work?

 


Servlets provide a component-based, platform-independent method for building Web-based applications, without the performance limitations of CGI programs. Servlets have access to the entire family of Java APIs, including the JDBC API to access enterprise databases.


Servlets Architecture:


Following diagram shows the position of Servelts in a Web Application.



MVC Pattern stands for Model-View-Controller Pattern. This pattern is used to separate application's concerns.

    Model - Model represents an object or JAVA POJO carrying data. It can also have logic to update controller if its data changes.

    View - View represents the visualization of the data that model contains.

    Controller - Controller acts on both Model and view. It controls the data flow into model object and updates the view whenever data changes. It keeps View and Model separate.