In this blog we will know if data is present in the database then update the data, or if the data does not exits in database then insert it to the database.
Create a table employee as below.
Create table employee (eid varchar(50)primary key,ename varchar(50))

Create a stored procedure display as below.

ALTER
PROCEDURE dbo.display
@eid varchar(50),
@ename varchar(50)
AS
IF EXISTS (SELECT * FROM employee WHERE eid = @eid)
BEGIN
update employee set ename=@ename where eid=@eid
END
ELSE
BEGIN
insert employee(eid,ename) values (@eid,@ename)
END
Default.aspx code
<%@
Page Language="VB"
AutoEventWireup="false"
CodeFile="Default.aspx.vb"
Inherits="_Default"
%>
<!DOCTYPE
html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml
/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="Eid"
Width="50px"></asp:Label>
<asp:TextBox
ID="txtUserId"
runat="server"
Width="197px"></asp:TextBox><br
/>
<asp:Label
ID="Label2"
runat="server"
Text="Ename"
Width="50px"></asp:Label>
<asp:TextBox
ID="txtUserName"
runat="server"
Width="197px"></asp:TextBox>
<asp:GridView
ID="GridView1"
runat="server">
</asp:GridView>
<br
/>
<asp:Button
ID="btn1"
runat="server"
Text="Insert"
onclick="btn1_Click"
/>
</div>
</form>
</body>
</html>



Join the conversation! Your thoughts help the community grow.