implementing insert,update , delete using grid view in SP

Tuesday 8 March 2011

Gridview insert code:



using System.Data.SqlClient;


SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["conn"]);
-----------------

public void show()
    {
        SqlCommand cmd = new SqlCommand("showsample", con);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter ada = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        ada.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();

      
    }

------------------


protected void Button1_Click(object sender, EventArgs e)
    {
        int id;
        string name;
        id = Convert.ToInt32(txtid.Text);
        name = txtname.Text;
        con.Open();
        SqlCommand cmd = new SqlCommand("insertsample", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@mode", 1);
        cmd.Parameters.AddWithValue("@id", id);
        cmd.Parameters.AddWithValue("@name", name);
        cmd.ExecuteNonQuery();
        con.Close();
        show();


    }


---------------


protected void selectindex(object sender, EventArgs e)
    {
        txtid.Text = GridView1.SelectedRow.Cells[1].Text;
        txtname.Text = GridView1.SelectedRow.Cells[2].Text;
    }
------------


protected void btnupdate_Click(object sender, EventArgs e)
    {

        int id;
        string name;
        id = Convert.ToInt32(txtid.Text);
        name = txtname.Text;
        con.Open();
        SqlCommand cmd = new SqlCommand("insertsample", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@mode", 0);
        cmd.Parameters.AddWithValue("@id", id);
        cmd.Parameters.AddWithValue("@name", name);
        cmd.ExecuteNonQuery();
        con.Close();
       show();
      
    }

-----------------

protected void btndelete_Click(object sender, EventArgs e)
    {
        int id;
        id = Convert.ToInt32(txtid.Text);
        con.Open();
        SqlCommand cmd = new SqlCommand("delsample", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@id", id);
        cmd.ExecuteNonQuery();
        con.Close();
       // show();

    }
-------------------------------
<configuration>
    <appSettings>
    <add key="conn" value="Data Source=SURESH-F51B96E2\SQLEXPRESS;Initial Catalog=master;Integrated Security=True"/>
    </appSettings>
  
    <connectionStrings>



 Store procedure

create proc select_sample
as
begin
select * from test1
end

--------------------------------------

create proc insert_sample
(@id int,
@mode bit,
@name nchar(20),
@city nchar(10),
@addr nchar(50))
as
begin
if (@mode=1)
insert into test1 values(@id,@name,@city,@addr)
else
update test1 set name=@name,city=@city,addr=@addr where id=@id
end

------------------------------------------------
create proc delete_sample
@id int
as
begin
delete from test1 where id=@id
end

----------------------------------------------------



link to learn c#  techotopia.com

0 comments:

Post a Comment