ho to insert value in database through stored procedure

Posted by Shanky11 under C# on 7/4/2013 | Points: 10 | Views : 978 | Status : [Member] | Replies : 1
i want 2 stored value in databse by sql
4 that i would like to use stored procedure
ho w can i call stored proc in coding pge




Responses

Posted by: Satyapriyanayak on: 7/4/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Stored procedure to insert records


CREATE PROCEDURE insert1
(@sid varchar(50),@sname varchar(50),@smarks int,@saddress varchar (50),@year varchar(50))
AS
insert student(sid,sname,smarks,saddress,year) values (@sid,@sname,@smarks,@saddress,@year)




<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Stored_procedure_to_insert._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Id" Width="100px"></asp:Label>
<asp:TextBox ID="txtid" runat="server"></asp:TextBox><br />

<asp:Label ID="Label2" runat="server" Text="Name" Width="100px"></asp:Label>
<asp:TextBox ID="txtname" runat="server"></asp:TextBox><br />

<asp:Label ID="Label3" runat="server" Text="Marks" Width="100px"></asp:Label>
<asp:TextBox ID="txtmarks" runat="server"></asp:TextBox><br />


<asp:Label ID="Label4" runat="server" Text="Address" Width="100px"></asp:Label>
<asp:TextBox ID="txtaddress" runat="server"></asp:TextBox><br />


<asp:Label ID="Label5" runat="server" Text="Year" Width="100px"></asp:Label>
<asp:TextBox ID="txtyear" runat="server"></asp:TextBox><br />

<asp:Button ID="btn_insert" runat="server" Text="Insert" onclick="btn_insert_Click"
/>
<asp:Label ID="lblmsg" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>



using System;
using System.Collections;
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;
namespace Stored_procedure_to_insert
{
public partial class _Default : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand com;
SqlDataAdapter sqlda;
DataSet ds;



protected void btn_insert_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
com = new SqlCommand("insert1", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@sid", txtid.Text);
com.Parameters.AddWithValue("@sname", txtname.Text);
com.Parameters.AddWithValue("@smarks", int.Parse(txtmarks.Text));
com.Parameters.AddWithValue("@saddress", txtaddress.Text);
com.Parameters.AddWithValue("@year", txtyear.Text);
com.ExecuteNonQuery();
con.Close();
lblmsg.Text = "Data entered successfully!!!";
}




}
}




If this post helps you mark it as answer
Thanks

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response