Saturday 24 November 2012

Insert , Update and Delete without writing any code in GridView in Asp.Net.


Introduction:-

It is possible to insert , update and delete operation performed in GridView in asp.net  without writing any code in .cs page. If you use SqlDataSoucreId , then it is possible.
When you drag and drop a GridView on web page , click on gridview  , a rectangular box will appear at the top right corner . Click on this , select new  datasource. I have Already explain about sqlDatasourceid. Now go to property of gridview set true to AutoGenerateEditButton and AutoGenerateDeleteButton. Write the update and delete code inside the sqldatasourceid control . Follow the code as given Below.

GridView.aspx:-
<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            AutoGenerateEditButton="True" DataSourceID="SqlDataSource1"
            AllowSorting="True" AutoGenerateDeleteButton="True">
            <Columns>
                <asp:BoundField DataField="user_name" HeaderText="user_name"
                    SortExpression="user_name" ReadOnly="true" />
                <asp:BoundField DataField="password" HeaderText="password"
                    SortExpression="password" ReadOnly="true" />
                <asp:BoundField DataField="country" HeaderText="country"
                    SortExpression="country" />
                <asp:BoundField DataField="email" HeaderText="email" SortExpression="email" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:modiConnectionString %>"
            SelectCommand="SELECT [user_name], [password], [country], [email] FROM [user_registration]"
              UpdateCommand="Update user_registration set country=@country, email=@email where user_name=@user_name"
               DeleteCommand="Delete user_registration where user_name=@user_name">
        </asp:SqlDataSource>
    </div>
    </form>
</body>
</html>

Run the code and test the code.
If any problem regarding this post feel free to contact me or comment the post.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.