Anda di halaman 1dari 16

ASP.

NET GridView Samples


HOW TO DO INSERT,UPDATE,DELETE AND SELECT in gridview without writing a
single   piece of code(ASP.NET C#):

STEP 1:
FIRST CREATE TABLES & STORED PROCEDURES IN SQL SERVER:

CREATE TABLE tbl_Student(Stud_ID varchar(20) primary key,Stud_Name VARCHAR(50),


Stud_Addr VARCHAR(MAX))

INSERT INTO tbl_Student(Stud_ID,Stud_Name,Stud_Addr)


Values
('07ca054','Rexmine Rose','Ooty')

INSERT INTO tbl_Student(Stud_ID,Stud_Name,Stud_Addr)


Values
('07ca055','Arvind','Dindugal')

INSERT INTO tbl_Student(Stud_ID,Stud_Name,Stud_Addr)


Values
('07ca056','Annie George','Trissur')

INSERT INTO tbl_Student(Stud_ID,Stud_Name,Stud_Addr)


Values
('07ca057','Jaikumar','Jabalpur')

INSERT INTO tbl_Student(Stud_ID,Stud_Name,Stud_Addr)


Values
('07ca058','Vijay Vignesh','Tanjore')

INSERT INTO tbl_Student(Stud_ID,Stud_Name,Stud_Addr)


Values
('07ca059','Cleetes Praveen','Palladam')

INSERT INTO tbl_Student(Stud_ID,Stud_Name,Stud_Addr)


Values
('07ca060','Sarlin Raj Tilak','Vellore')

SELECT * FROM tbl_Student


-- STORED PROCEDURE FOR SELECT
CREATE PROC sp_Select
AS
SELECT * FROM tbl_Student

--STORED PROCEDUE FOR UPDATE


CREATE PROC sp_Update
@Stud_ID VARCHAR(20),
@Stud_Name Varchar(50),
@Stud_Addr VARCHAR(MAX)
AS
UPDATE tbl_Student SET Stud_Name=@Stud_Name,Stud_Addr=@Stud_Addr WHERE

Stud_ID=@Stud_ID

--STORED PROCEDURE FOR DELETE


CREATE PROC sp_Delete
@Stud_ID VARCHAR(20)
AS
DELETE FROM tbl_Student WHERE Stud_ID=@Stud_ID
SELECT,UPDATE AND DELETE in ASP.NET gridview:
Step 2:
Open Visual Studio--->>file--->>> new --->>>
Website--->>>choose language as c#--->>>ok

Step 3:
drag and drop a "sql datasource" control from the datatab of the tool box
and place it in .aspx page

Step 4:
now click on the smart tag(">") of sqldatasource-->>
configure datasource-->>new connection--->>>and create a connection with your

database--->>>ok--->>next--->>>next--->>>
Step 5:
now select "Specify a custom SQL statement or stored procedure" option--->>
now in the,
SELECT TAB--->>> choose "stored procedure" option--->>
and select "sp_Select"
UPDATE TAB--->>> choose "stored procedure" option--->>
and select "sp_Update"
DELETE TAB--->>> choose "stored procedure" option--->>
and select "sp_Delete"
now give next--->>>finish.
Step 6:
now drag and drop a gridview control into the .aspx page

Step 7:
now click on the smart tag(">") of gridview-->>choose datasource options to

sqldatasource1--->>>
and also enable the check boxes such as
sorting,
paging,
editing,
deleting,
selection
thats it!!! now the run the program and see a gridview with

editing,deleting,selecting options working fine without writing a single code in

asp.net.
In the web.config file under <appSettings> tag declare the constants.
example:
 <appSettings>
      <add key="SqlConnection_local" value="Server=vijay-
PC\SQLEXPRESS;Database=Student;User ID=vijay;password=123;Persist Security
Info=True;"/><!--Ex: For Database Connection String-->
      <add key="clientURl" value="http://localhost:1555/YearBook/"/><!--Ex: For WebSite URI-->
      <add key="PI" value="3.14159265"/><!--Ex: For Mathematical Constant Values-->
 </appSettings>

How to use those variables inside ASP.Net:

string cURL = ConfigurationManager.AppSettings["clientURl"];


//to retrieve URL strings from webconfig global file.
float pi = float.Parse(ConfigurationManager.AppSettings["PI"]);
//to retrieve Constant Values

string conn= ConfigurationManager.AppSettings["SqlConnection_local"];


//to retrieve database connection strings

Anda mungkin juga menyukai