0% found this document useful (0 votes)
139 views

Sample Program On All Operations (Insert, Delete, Update, Reset) in

The document contains code for performing CRUD (create, read, update, delete) operations on a sample database table using stored procedures in SQL Server. Stored procedures are created to insert, update, delete and retrieve data from the table. C# code is provided to call each stored procedure by passing parameters and handle the database operations. A global connection string is also declared to connect to the database without needing to declare a new connection each time.

Uploaded by

sunilbabu29
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
139 views

Sample Program On All Operations (Insert, Delete, Update, Reset) in

The document contains code for performing CRUD (create, read, update, delete) operations on a sample database table using stored procedures in SQL Server. Stored procedures are created to insert, update, delete and retrieve data from the table. C# code is provided to call each stored procedure by passing parameters and handle the database operations. A global connection string is also declared to connect to the database without needing to declare a new connection each time.

Uploaded by

sunilbabu29
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Stored procedure for

insert:

create procedure sample_ins(@empid int,@empname varchar(50),@empmobileno decimal(18,0),@empaddress varchar(max)) as begin insert into sample (empid,empname,empmobileno,empaddress)values(@empid,@empname, @empmobileno,@empaddress) end

Coding for insert button:


try { SqlCommand cmd=new SqlCommand ("sample_ins",con); cmd.CommandType=CommandType.StoredProcedure; cmd.Parameters.Clear(); cmd.Parameters.Add("empid",SqlDbType.Int).Value=TextBox1.Text; cmd.Parameters.Add("empname",SqlDbType.VarChar).Value=TextBox2. Text; cmd.Parameters.Add("empmobileno",SqlDbType.Decimal). Value=TextBox3.Text; cmd.Parameters.Add("empaddress",SqlDbType.VarChar).Value=TextBo x4.Text; cmd.Parameters.Add("password", SqlDbType.VarChar).Value = TextBox5.Text; con.Open(); cmd.ExecuteNonQuery(); //For clearing all the fields TextBox1.Text = ""; TextBox2.Text = ""; TextBox3.Text = ""; TextBox4.Text = ""; TextBox5.Text = ""; Response.Write("Inserted Successfully...!"); } catch(Exception ex) { Response.Write(ex.Message); } finally { con.Close(); }

Stored procedure for Update:


create procedure sample_up(@empid int,@empmobileno decimal(18,0),@empaddress varchar(max),@password varchar(50)) as begin update sample set empmobileno=@empmobileno,empaddress=@empaddress,password= @password where empid=@empid end

Coding for Update button:


try { con.Open(); SqlCommand Cmd = new SqlCommand("sample_up", con); Cmd.CommandType = CommandType.StoredProcedure; Cmd.Parameters.AddWithValue("@empid", SqlDbType.Int).Value = TextBox1.Text; Cmd.Parameters.AddWithValue("@empmobileno", SqlDbType.Decimal).Value = TextBox3.Text; Cmd.Parameters.AddWithValue("@empaddress", SqlDbType.VarChar).Value = TextBox4.Text; Cmd.Parameters.AddWithValue("@password", SqlDbType.VarChar).Value = TextBox5.Text; Cmd.ExecuteNonQuery(); //For clearing all the fields TextBox1.Text TextBox2.Text TextBox3.Text TextBox4.Text TextBox5.Text = = = = = ""; ""; ""; ""; "";

Response.Write("Updated Successfully...!"); } catch (Exception ex) { Response.Write("ex.message"); } finally { con.Close(); }

Stored procedure for Delete:


create procedure sample_del1 (@empid int) as begin delete from sample where empid=@empid end

Coding for Delete button:


con.Open(); SqlCommand Cmd = new SqlCommand("sample_del1", con); Cmd.CommandType = CommandType.StoredProcedure; Cmd.Parameters.AddWithValue("@empid",TextBox1.Text); Cmd.ExecuteNonQuery(); con.Close(); //For clearing all the fields TextBox1.Text TextBox2.Text TextBox3.Text TextBox4.Text TextBox5.Text = = = = = ""; ""; ""; ""; "";

Response.Write("Deleted Successfully...!");

Stored procedure for

Retrieving Data:

create procedure login1(@empid int),@password varchar(20) as begin select * from sample where empid =@empid end

For Retrieving Data:


try { con.Open(); SqlCommand cmd = new SqlCommand("login1", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("@empid", SqlDbType.Int).Value = TextBox1.Text; cmd.Parameters.Add("@password", SqlDbType.VarChar).Value = TextBox2.Text; SqlDataReader rdr = cmd.ExecuteReader(); if (rdr.Read()) { Response.Redirect("welcome.aspx"); } else { Response.Write("Employee id was incorrect"); } } catch (Exception ex) { Response.Write(ex.Message); }

For Connection String:


SqlConnection con=new SqlConnection("Server=SYS18;Initial Catalog=NorthWind;Integrated Security=true;Database='suneel'");

Declare the connection string in the global of the class Then we need not to declare for all the operations NOTE: In the name space import the below string
using System.Data.SqlClient;

You might also like