Wednesday, 27 June 2012

insert,update and delete in webmatrix

 Page looks like:
 
@{
    var cn=Database.Open("l");
    var m="";
    var nn="";
    var o="";
    if(Request["id"]!=null)
    {
       nn =Request["id"];
       o=Request["name"];
    }
    if(Request["id1"]!=null)
    {
        var qq="delete from emp where eid=@0";
        var q=cn.Execute(qq,Request["id1"]);
    }
   
    if(IsPost)
    {
        if(!Request["c"].IsEmpty())
        {
             var p="insert into emp values(@0,@1)";
        var q=cn.Execute(p,Request["a"],Request["b"]);
        m="One record saved.";
        }
       else if(!Request["cc"].IsEmpty())
       {
           var qq="update emp set name=@0 where eid=@1";
        var q=cn.Execute(qq,Request["b"],Request["a"]);
        m="One record updated.";
       }
 
        
    }
    var d=cn.Query("select * from emp");
}
 
<!DOCTYPE html>
 
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>My Site's Title</title>
    </head>
    <body>
        <form method="post">
        <table align="center">
            <tr><td>&nbsp;</td><th>@m</th></tr>
            <tr><td>EID</td><td><input type="text" name="a" value="@nn" ></td></tr>
            <tr><td>NAME</td><td><input type="text" name="b" value="@o" ></td></tr>
            <tr><td>&nbsp;</td><td><input type="submit" name="c" value="Save">&nbsp;<input type="submit" value="update" name="cc"></td></tr>
            <tr><td>&nbsp;</td><td>
            <table border="1">
                <tr><th>UPDATE</th><th>DELETE</th><th>EID</th><th>NAME</th></tr>
                @{
                    foreach(var n in d)
                    { 
<tr><td><a href="Default.cshtml?id=@n.EID&name=@n.NAME">Edit</a></td><td><a href="Default.cshtml?id1=@n.EID">Delete</a></td><td>@n.EID</td><td>@n.NAME</td></tr> 
                    }
                    
                 }
            </table>
            </td></tr>
        </table>
        </form>
    </body>
</html>

No comments:

Post a Comment