You are on page 1of 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