Stored Procedure

Sps :

//Show Data SP

//Select Data From CompanyData table
CREATE PROCEDURE [dbo].[SelectData]
AS
BEGIN

SET NOCOUNT ON;
SELECT FirstName,LastName,Department,Salary from dbo.CompanyData
END
GO

//Insert Data Sp
CREATE PROCEDURE InsertData
(
@FirstName nvarchar(20),
@LastName nvarchar(20),
@Department nvarchar(20),
@Salary int
)
AS
BEGIN
SET NOCOUNT ON;
Insert into CompanyData values(@FirstName,@LastName,@Department,@Salary)
END
GO

SqlConnection con = new SqlConnection(@”Data Source=SYNDLF371;Initial Catalog=mydata;Integrated Security=True”);
DataSet ds = new DataSet();
SqlDataAdapter da;
SqlCommand cmd;

//ShowData method

private void ShowData()
{
//con.Open();
//da = new SqlDataAdapter(“select FirstName ,LastName from CompanyData”,con);
//da.Fill(ds);
//GridView1.DataSource = ds;
//GridView1.DataBind();

con.Open();
cmd = new SqlCommand(“SelectData”,con);
da = new SqlDataAdapter(cmd);

da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}

//Insert Data

con.Open();
cmd = new SqlCommand(“InsertData”, con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(“@FirstName”,SqlDbType.NVarChar).Value = TextBox1.Text;
cmd.Parameters.Add(“@LastName”, SqlDbType.NVarChar).Value = TextBox2.Text;
cmd.Parameters.Add(“@Department”, SqlDbType.NVarChar).Value = TextBox3.Text;
cmd.Parameters.Add(“@Salary”, SqlDbType.Int).Value = Convert.ToInt32(TextBox4.Text);
cmd.ExecuteNonQuery();
con.Close();
ShowData();

One thought on “Stored Procedure

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s