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>