Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
Hi,
 

Can anyone know?
 
what is the asp.net code for insert,update,delete using stored procedure?
Posted 19-Dec-11 17:42pm
Comments
Shobana16 at 20-Dec-11 0:10am
   
Do you want in vb.net or c#?
sathiyak at 20-Dec-11 0:12am
   
c#
Prince Antony G at 20-Dec-11 4:10am
   
google?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

see the below link....sure you can implement this...
 
www.dotnetfunda.com/articles/article29.aspx
 
and also you can do this by 3 tier method
 

ex:
 
BL:
Public dataset function name(parameters)
{
return obj.function name(parasmeters)
}
DL:
 
call the same function here
and open sql connection,call parameters
then call store procedure name
  Permalink  
v4
Comments
munirajan at 19-Dec-11 23:52pm
   
I think its enough for you....
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Try this,
 
Asp.net code:
 
<div>
       Name <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
       Mobile  <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
        <asp:Button ID="Button1" runat="server" Text="Insert" OnClick="insert_Click" />
        <asp:Button ID="Button2" runat="server" Text="Update" OnClick="update_Click" />
        <asp:Button ID="Button3" runat="server" Text="Delete" OnClick="delete_Click" />
    </div>
 

C# code
 

SqlConnection conn = new SqlConnection();
    SqlCommand cmd = new SqlCommand();
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    public void connection()
    {
        string sqlcon = System.Configuration.ConfigurationManager.AppSettings["conn"];
        conn=new SqlConnection(sqlcon);
        conn.Open();
    }
    protected void insert_Click(object sender, EventArgs e)
    {
        connection();
      
        cmd = new SqlCommand("spInsert",conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = TextBox1.Text.ToString();
        cmd.Parameters.Add("@mobile", SqlDbType.Int).Value = Convert.ToInt32(TextBox2.Text.ToString());
        cmd.ExecuteNonQuery();
        conn.Close();
    }
    protected void update_Click(object sender, EventArgs e)
    {
        connection();
        cmd = new SqlCommand("spUpdate", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = TextBox1.Text.ToString();
        cmd.Parameters.Add("@mobile", SqlDbType.Int).Value = Convert.ToInt64(TextBox2.Text.ToString());
        cmd.ExecuteNonQuery();
        conn.Close();
    }
    protected void delete_Click(object sender, EventArgs e)
    {
        connection();
        cmd = new SqlCommand("spDelete", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = TextBox1.Text.ToString();
        cmd.ExecuteNonQuery();
        conn.Close();
    }
 

sql table
 
create table table1
(
Name varchar(25),
Mobile int
)
 
SQL procedure
 

 
create procedure spInsert
(
@name varchar(25),
@mobile int
)
as
begin
insert into table1 values(@name,@mobile)
end
create procedure spUpdate
(
@name varchar(25),
@mobile int
)
as
begin
update table1 set Mobile=@mobile where Name=@name
end
create procedure spDelete
(
@name varchar(25)
)
as
begin 
delete from table1 where Name=@name
end
 
Hope this helps....
 
Thanks
  Permalink  
Comments
Shobana16 at 20-Dec-11 1:20am
   
Can anyone tell me, what is wrong in above code?
Shobana16 at 20-Dec-11 5:03am
   
Tell me what is error in it?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

go through this link
 

3-tier architecture in C#[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

hi,
please try this code
 
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
    SqlConnection con;
    DataSet ds;
    SqlDataAdapter da;
    SqlCommand cmd;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
            BindData();
        }
    }
    public void BindData()
    {
        con = new SqlConnection("server=.\\SQLEXPRESS;database=master;integrated security=true");
        ds = new DataSet();
        da = new SqlDataAdapter("SELECT * FROM users", con);
        da.Fill(ds, "users");
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BindData();
    }
    protected void BtnDelete_click(object sender, EventArgs e)
    {
        Button btn = sender as Button;
        GridViewRow row = (GridViewRow)btn.NamingContainer;
        string username = ((Label)row.FindControl("LblSId")).Text;
        string query = "DELETE users WHERE username = '" + username + "'";
        con = new SqlConnection("server=.\\SQLEXPRESS;database=master;integrated security=true");
        cmd = new SqlCommand(query, con);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        BindData();
    }
    protected void BtnUpdate_Click(object sender, EventArgs e)
    {
        Button btn = sender as Button;
        GridViewRow row = (GridViewRow)btn.NamingContainer;
        string username = ((Label)row.FindControl("LblEditSId")).Text;
        string password = ((TextBox)row.FindControl("TxtEditSName")).Text;
        //string sadd = ((TextBox)row.FindControl("TxtEditSAdd")).Text;
        //string smarks = ((TextBox)row.FindControl("TxtEditSmarks")).Text;
        string query = "UPDATE student SET password ='" + password + "', WHERE username = '" + username + "'";
        con = new SqlConnection("server=.\\SQLEXPRESS;database=master;integrated security=true");
        cmd = new SqlCommand(query, con);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        GridView1.EditIndex = -1;
        BindData();
    }
    protected void BtnCancel_Click(object sender, EventArgs e)
    {
        GridView1.EditIndex = -1;
        BindData();
    }
}
may be it helps.Smile | :)
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 446
1 OriginalGriff 325
2 Yogesh Kumar Tyagi 304
3 Maciej Los 235
4 Prakriti Goyal 200
0 OriginalGriff 6,417
1 Sergey Alexandrovich Kryukov 5,344
2 Maciej Los 3,434
3 Peter Leow 3,259
4 DamithSL 2,490


Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 20 Dec 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100