GridView and SQL DataSource With SQL DataSource Parametrized Queries

GridView and SQL DataSource With SQL DataSource Parametrized Queries.

This post consists of functioning of GridView control by using SQL DataSource control as the datasource for GridView to view data.


You have two options in order to perform this:

1. Either you write complete code .

2. Do smart work, drag the database table and drop it on the design window of your .aspx page, the code will be generated automatically without consuming any time and your coding effort (yes you may require to code a bit in-order to make some changes).


If you are following the second step i.e  using the drag and drop method, you have to follow the following steps

1. Select the table form database and drag it to the design (or source) window of .aspx page and drop it inside the form tag.



img1

 

 

 

 

 

 

 

 

(click on the image to enlarge)
2. Once you have done with the first step, the complete code of GridView and SQL DataSource will be generated automatically.
img2

 

 

 

 

 

 

 

(click on the image to enlarge)

 

Now if you open your source window of .aspx page you will find the following…..

Default.aspx

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default12.aspx.cs" Inherits="Default12" %>

<!DOCTYPE html>

<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" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="SqlDataSource1" EmptyDataText="There are no data records to display.">
            <Columns>
                <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
                <asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True" SortExpression="Id" />
                <asp:BoundField DataField="question" HeaderText="question" SortExpression="question" />
                <asp:BoundField DataField="opt1" HeaderText="opt1" SortExpression="opt1" />
                <asp:BoundField DataField="opt2" HeaderText="opt2" SortExpression="opt2" />
                <asp:BoundField DataField="opt3" HeaderText="opt3" SortExpression="opt3" />
                <asp:BoundField DataField="opt4" HeaderText="opt4" SortExpression="opt4" />
                <asp:BoundField DataField="answer" HeaderText="answer" SortExpression="answer" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString1 %>" DeleteCommand="DELETE FROM [qstn] WHERE [Id] = @Id" InsertCommand="INSERT INTO [qstn] ([question], [opt1], [opt2], [opt3], [opt4], [answer]) VALUES (@question, @opt1, @opt2, @opt3, @opt4, @answer)" ProviderName="<%$ ConnectionStrings:DatabaseConnectionString1.ProviderName %>" SelectCommand="SELECT [Id], [question], [opt1], [opt2], [opt3], [opt4], [answer] FROM [qstn]" UpdateCommand="UPDATE [qstn] SET [question] = @question, [opt1] = @opt1, [opt2] = @opt2, [opt3] = @opt3, [opt4] = @opt4, [answer] = @answer WHERE [Id] = @Id">
            <DeleteParameters>
                <asp:Parameter Name="Id" Type="Int32" />
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="question" Type="String" />
                <asp:Parameter Name="opt1" Type="String" />
                <asp:Parameter Name="opt2" Type="String" />
                <asp:Parameter Name="opt3" Type="String" />
                <asp:Parameter Name="opt4" Type="String" />
                <asp:Parameter Name="answer" Type="String" />
            </InsertParameters>
            <UpdateParameters>
                <asp:Parameter Name="question" Type="String" />
                <asp:Parameter Name="opt1" Type="String" />
                <asp:Parameter Name="opt2" Type="String" />
                <asp:Parameter Name="opt3" Type="String" />
                <asp:Parameter Name="opt4" Type="String" />
                <asp:Parameter Name="answer" Type="String" />
                <asp:Parameter Name="Id" Type="Int32" />
            </UpdateParameters>
        </asp:SqlDataSource>
    
    </div>
    </form>
</body>
</html>

 

Try this one, and you find this amazing, the drag and drop feature is awesome. But if you’re new in programming, I would advise you go with first option, i,e manual coding as this will improve your coding skills and you will be able understand the code in better way.

 

Download Demo Work

 


JavaScript, ASP.Net & PHP Web Developer. Connect with me on Facebook and Twitter.

Share This Post

Related Articles

Powered by Paras Babbar · Designed by Paras Babbar