0% found this document useful (0 votes)
21 views32 pages

Awp Unit 4 - 23603239 - 2023 - 11 - 22 - 12 - 23

Uploaded by

Yash Palwankar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views32 pages

Awp Unit 4 - 23603239 - 2023 - 11 - 22 - 12 - 23

Uploaded by

Yash Palwankar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

www.moreacademy.

online

More Academy

ADVANCED WEB
PROGRAMMING
SEM : V
SEM V: UNIT 4

607A, 6th floor, Ecstasy business park, city of joy, JSD


road, mulund (W) | 8591065589/022-25600622

Abhay More abhay_more


MORE ACADEMY BSC IT: SEM – V AWP: U4

Page 1 of 31
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
MORE ACADEMY BSC IT: SEM – V AWP: U4

Q1. What is ADO.Net? Explain its architecture.


Or
Draw and explain ADO.NET data provider model with diagram (Apr 2023)

ADO.NET provides a bridge between the front-end controls and the back-end database. The
ADO.NET objects encapsulate all the data access operations and the controls interact with
theseobjects todisplay data, thus hiding the details of movement of data.
ADO.NET consist of a set of Objects that expose data access services to the .NET
environment.It is a data access technology from Microsoft .Net Framework, which provides
communication betweenrelational and non-relational systems through a common set of
components.
System.Data namespace is the core of ADO.NET and it contains classes used by all data
providers.ADO.NET is designed to be easy to use, and Visual Studio provides several wizards andother
features that we can use to generate ADO.NET data access code.

The two key components of ADO.NET are Data Providers and DataSet. The Data Provider
classes aremeant to work with different kinds of data sources. They are used to perform all
data-management operations on specific databases. DataSet class provides mechanisms for
managing data when it isdisconnected from the data source.
Data Providers
The .Net Framework includes mainly three Data Providers for ADO.NET. They are the
MicrosoftSQL Server Data Provider, OLEDB Data Provider and ODBC Data Provider. SQL
Server uses the SqlConnection object , OLEDB uses the OleDbConnection Object and ODBC
uses OdbcConnectionObject respectively
A data provider contains Connection, Command, DataAdapter, and DataReader
objects.These fourobjects provide the functionality of Data Providers in the ADO.NET.
DataSet
DataSet provides a disconnected representation of result sets from the Data Source, and it is
completely independent from the Data Source. DataSet provides much greater flexibility when dealing
with related Result Sets.

Page 2 of 31
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
MORE ACADEMY BSC IT: SEM – V AWP: U4

Q2. List and Explain ADO .NET objects. (Nov 2018)

ADO.NET includes many objects we can use to work with data. Some important objects of
ADO.NETare:
Connection
To interact with a database, we must have a connection to it. The connection helps identify the
database server, the database name, user name, password, and other parameters that are
required
for connecting to the data base.
A connection object is used by command objects so they will know which database to execute
thecommand on.
Command
The command object is one of the basic components of ADO .NET. The Command Object uses
theconnection object to execute SQL queries.
The queries can be in the Form of Inline text, Stored Procedures or direct Table access. An
important feature of Command object is that it can be used to execute queries and Stored
Procedures withParameters If a select query is issued, the result set it returns is usually stored in
either a DataSet or a DataReader object.
DataReader
Many data operations require that we only get a stream of data for reading. The data reader object
allows us to obtain the results of a SELECT statement from a command object. For
performancereasons, the data returned from a data reader is a fast forward-only stream of data.
This means that we can only pull the data from the stream in a sequential manner this is good for
speed, but if we need to manipulate data, then a DataSet is a better object to work with.
DataSet
DataSet objects are in-memory representations of data. They contain multiple Datatable objects,
which contain columns and rows, just like normal database tables. We can even define
relationsbetween tables to create parent-child relationships.
The DataSet is specifically designed to help manage data in memory and to support
disconnectedoperations on data, when such a scenario make sense.
DataAdapter
The data adapter fills a DataSet object when reading the data and writes in a single batch when
persisting changes back to the database. A data adapter contains a reference to the connection
object and opens and closes the connection automatically when reading from or writing to the
database.

Page 3 of 31
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
MORE ACADEMY BSC IT: SEM – V AWP: U4

Q3. Explain Command Class and DataAdapter class with properties and methods

Command class properties and methods

Page 4 of 31
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
MORE ACADEMY BSC IT: SEM – V AWP: U4

Differentiate between DataSet and DataReader. (Nov 2022)

Q5. Explain the difference between DataReader and DataAdapter (Apr 2023)

DataAdapterin ADO.NET.DataReader
 DataReader is connection oriented architecture.
 DataReader is like a forward only recordset.
 It fetches one row at a time so very less network cost compare to DataSet (Fetches all
the rowsat atime).
 DataReader is readonly so we can't do any update or transaction on them.
 DataReader will be the best choice where we need to show the data to the
user whichrequires notransaction.
 As DataReader is forward only so we can't fetch data randomly.
 .NET Data Providers optimizes the DataReader to handle huge amount of data.
 Performance is good.
DataAdapter
 DataAdapter acts as a bridge between DataSet and database.
 DataAdapter object is used to read the data from the database and bind that data to dataset.
 DataAdapter is a disconnected oriented architecture.
 DataAdapter resolves the changes made to the DataSet back to the database.

Page 5 of 31
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
MORE ACADEMY BSC IT: SEM – V AWP: U4

Q6. What is DataReader in ADO.NET? Explain with example. (Nov 2018)(Apr 2019)

DataReader provides an easy way for the programmer to read data from a database as if it
werecoming from a stream. The DataReader is the solution for forward streaming data through
ADO.NET.
dataDataReader is also called a firehose cursor or forward read-only cursor because it moves
forwardthrough the data. The DataReader not only allows us to move forward through each record
of database, but it also enables us to parse the data from each column. The DataReaderclass
represents
a data reader in ADO.NET.
Example:
In below example we read the all records from customer table using DataReader class.
string SQL = "SELECT * FROM Customers";
SqlConnection conn = new SqlConnection(ConnectionString);
// create a command object
SqlCommand cmd = new SqlCommand(SQL, conn); conn.Open();
// Call ExecuteReader to return a DataReader SqlDataReader reader = cmd.ExecuteReader();
Console.WriteLine("customer ID, Contact Name, " + "Contact Title, Address ");
Console.WriteLine("=============================");
while (reader.Read())
{
Console.Write(reader["CustomerID"].ToString() + ", ");
Console.Write(reader["ContactName"].ToString() + ", ");
Console.Write(reader["ContactTitle"].ToString() + ", ");
Console.WriteLine(reader["Address"].ToString() + ", ");
}
//Release resourcesreader.Close();
conn.Close();
//Release resourcesreader.Close();
conn.Close();

Page 6 of 31
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
MORE ACADEMY BSC IT: SEM – V AWP: U4

Q7. What is data binding? Its types. (Nov 2019)(Apr 2019)(Nov 2019) (Apr 2023)
or
What is data binding? Explain repeated value data binding with example
or
Write short note on data binding?
Data binding in ASP.NET is superficially similar to data binding in the world of
desktop or client/serverapplications, but in truth, it's fundamentally different. In those
environments, data bindinginvolves creating a direct connection between a data source and
a control in an application window.
If the user modifies the data in a data-bound control, our program can update the
correspondingrecord in the database, but nothing happens automatically.
ASP.NET data binding is much more flexible than old-style data binding. Many of the most
powerful data binding controls, such as the GridView and DetailsView, give us
unprecedented control over thepresentation of our data, allowing us to format it, change its
layout, embed it in other ASP.NET controls, and so on.
Types of ASP.NET DataBinding
Two types of ASP.NET data binding exist: single-value binding and repeated-value binding.
Single- value data binding is by far the simpler of the two, whereas repeated-value binding
providesthefoundation for the most advanced ASP.NET data controls.
Single-Value, or "Simple," Data Binding
We can use single-value data binding to add information anywhere on an ASP.NET page. We
canevenplace information into a control property or as plain text inside an HTML tag.
Single-value data binding doesn't necessarily have anything to do with ADO.NET. Instead,
single- valuedata binding allows us to take a variable, a property, or an expression and insert it
dynamicallyinto a page.
Repeated-Value, or "List," Binding
Repeated-value data binding allows us to display an entire table (or just a single field
from a table).Unlike single-value data binding, this type of data binding requires a special
control that supports it.
Typically, this will be a list control such as CheckBoxList or ListBox, but it can also be a much
moresophisticated control such as the GridView.

Page 7 of 31
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
MORE ACADEMY BSC IT: SEM – V AWP: U4

Q8. What is a Data source? Explain various types of data sources in ASP.NET.

The Data source control connects to and retrieves data from a data source and makes it
available forother controls to bind to, without requiring code. ASP.NET allows a variety of
data sources suchas a database, an XML file, or a middle-tier business object.
The common data source controls are:
 AccessDataSource – Enables you to work with a Microsoft Access database.
 XmlDataSource – Enables you to work with an XML file.
 SqlDataSource – Enables you to work with Microsoft SQL Server, OLE DB, ODBC, or
Oracledatabases.
 ObjectDataSource – Enables you to work with a business object or other class
 SiteMapDataSource – Used for ASP.NET site navigation.
 EntityDataSource - Enables you to bind to data that is based on the Entity Data Model.
 LinqDataSource – Enables you to use Language-Integrated Query (LINQ) in an ASP.NET Web
page.

Page 8 of 31
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
MORE ACADEMY BSC IT: SEM – V AWP: U4

Q9. Explain SqlDataSource in ADO.NET. (Nov 2018)(Apr 2019)(Apr 2023)


The SqlDataSource control represents a connection to a relational database such as SQL Server or
Oracle database, or data accessible through OLEDB or Open Database Connectivity (ODBC).
Connection to data is made through two important properties ConnectionString and ProviderName.
The following code snippet provides the basic syntax of the control:
<asp:SqlDataSource runat="server" ID="MySqlSource"
ConnectionString='<%$ ConnectionStrings:Empconstr %>'
SelectionCommand= "SELECT * FROM EMPLOYEES" />

The following table provides the related sets of properties of the SqlDataSource control, whichprovides
the programming interface of the control:

The following code snippet shows a data source control enabled for data manipulation:

<asp:SqlDataSource runat="server" ID= "MySqlSource" ConnectionString=' <%$


ConnectionStrings:Empconstr %>' SelectCommand= "SELECT * FROM EMPLOYEES"
UpdateCommand= "UPDATE EMPLOYEES SET LASTNAME=@lame"
DeleteCommand= "DELETE FROM EMPLOYEES WHERE EMPLOYEEID=@eid"
FilterExpression= "EMPLOYEEID > 10">
.....
.....
</asp:SqlDataSource>

Page 9 of 31
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
MORE ACADEMY BSC IT: SEM – V AWP: U4

Q10. What is a GridView control? Explain how to enable row selection, paging and
sortingfeatures ofGridView (Nov 2018)
The
GridView control displays the values of a data source in a table. Each column represents a
field,while each row represents a record. The GridView control supports the following features:
• Binding to data source controls, such as SqlDataSource.
• Built-in sort capabilities.
• Built-in update and delete capabilities.
• Built-in paging capabilities.
• Built-in row selection capabilities.
• Multiple key fields.
• Multiple data fields for the hyperlink columns.
• Customizable appearance through themes and styles
Sorting allows the user to sort the items in the GridView control with respect to a specific column
byclicking on the column's header. To enable sorting, set the AllowSorting property totrue.

AllowSorting="True"
Instead of displaying all the records in the data source at the same time, the GridView control
canautomatically break the records up into pages. To enable paging, set the AllowPaging property to
true.
AllowPaging="True"
Also we can set how many rows we want to see in a page.
PageSize="4"
Example:
<asp:gridview AllowSorting="true" AllowPaging="true" PageSize="5" ID="Gridview1"
runat="server
DataKeyNames="pid" DataSourceID="SqlDS" >
<Columns>
<asp:BoundField DataField="pname" HeaderText="PRODUCT
NAME"
SortExpression="pname"></asp:BoundField>
</Columns>
</asp:gridview>

Page 10 of 31
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
MORE ACADEMY BSC IT: SEM – V AWP: U4

Q11. Write a program which display all records from table to GridView
Or
Write a C# code to insert data in database table. Write a comment where ever required
(Nov 2022)
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;using
(SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers"))
{
cmd.Connection = con;
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();sda.Fill(dt);

GridView1.DataSource = dt; GridView1.DataBind();


}
}
}

Page 11 of 31
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
MORE ACADEMY BSC IT: SEM – V AWP: U4

Q12. Explain DetailsView Control.


 DetailsView control is a data-bound control that renders a single record at a time. It
canprovidenavigation option also. DetailsView control supports the edit, insert, delete and
paging functionality.
 It can insert, update and delete the record also. When it is rendered on the page, generally
it isimplemented through <table> HTML tag.
 The DetailsView control supports exactly the same fields as the GridView control: BoundField:
Enables us to display the value of a data item as text. CheckBoxField: Enables us to display
the value of a data item as a check box. CommandField: Enables us to display links for
editing, deleting, and selecting rows.ButtonField: Enables us to display the value of a data
item as a button. HyperLinkField: Enables us to display the value of a data item as a link.
ImageField: Enables us to display the value of a data item as an image. TemplateField:
Enables us to customize the appearance of a data item.
Properties of DetailsView:

Properties Description
AllowPaging Gets or sets a value indicating whether the paging feature
is enabled.
DataSource Gets or sets the object from which the data-bound control
retrieves itslist of data items.
DataSourceID Gets or sets the ID of the control from which the data-
bound controlretrieves its list of data items.
AutoGenerateEditButton Gets or sets a value indicating whether the built-in controls
to edit thecurrent record are displayed in a DetailsView
control.
AutoGenerateDeleteButt Gets or sets a value indicating whether the built-in
on control to deletethe current record is displayed in a
DetailsView control.
AutoGenerateRows Gets or sets a value indicating whether row fields for
each field in thedata source are automatically generated
and displayed in
a DetailsView control.
DefaultMode Get or sets the default data-entry mode of the DetailsView
control.

SqlConnection conn; SqlDataAdapter adapter;


DataSet ds; SqlCommand cmd;
string cs = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;

protected void PopulateDetailView()


{
try
{
conn = new SqlConnection(cs);
adapter = new SqlDataAdapter("select * from tblEmps", conn);
ds = new DataSet();
adapter.Fill(ds);

Page 12 of 31
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
MORE ACADEMY BSC IT: SEM – V AWP: U4

DetailsView1.DataSource = ds;
DetailsView1.DataBind();
}
catch (Exception ex)
{
Label1.Text = "ERROR :: " + ex.Message;
}
}
protected void DetailsView1_PageIndexChanging(object sender, DetailsViewPageEventArgs
e)
{
DetailsView1.PageIndex = e.NewPageIndex;
PopulateDetailView();
}
protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
{
DetailsView1.ChangeMode(e.NewMode);
PopulateDetailView();
}

Page 13 of 31
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
MORE ACADEMY BSC IT: SEM – V AWP: U4

Q13. Briefly explain FormView control. How is it different from DetailsView?


Or
Difference between Form view and details view in ASP.NET (Nov 2018)(Apr
2023)

 Like DetailsView, FormView also displays a single record from the data source at a time.
Bothcontrols can be used to display, edit, insert and delete database records but one at a time.
Both have paging feature and hence support backward and forward traversal.
 FormView is a new data-bound control that is nothing but a templated version of
DetailsViewcontrol. The major difference between DetailsView and FormView is, here user
need to define the rendering template for each item.
 The FormView control provides more formatting and layout options than DetailsView.
 The DetailsView control uses <BoundField> elements or <TemplateField> elements to
display bound data whereas FormView can use only templates to display bound data.
 The FormView control renders all fields in a single table row whereas the DetailsViewcontrol
display seach field as a table row.
 When compare to DetailsView, the FormView control provides more control over the layout.
 Following are some important properties that are very useful.

Page 14 of 31
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
MORE ACADEMY BSC IT: SEM – V AWP: U4

Q14. What is the difference between ListView and Gridview control? Explain the
ListView control.
ListView presents the data in rows and columns just like a GridView control. The main
differencebetween the ListView control and Gridview control is that the ListView control includes
an additional row for inserting a new row into the table.
ListView Control:
The ListView control displays columns and rows of data and allows sorting and paging. It is byfar
themost popular data display control and is ideal for understanding how data display controls
interact with data retrieval controls and code.
ListView provides various templates which we can use to display the data. The templates are:

o LayoutTemplate
o ItemTemplate
o ItemSeparatorTemplate
o GroupTemplate
o GroupSeparatorTemplate
o EmptyItemTemplate
o EmptyDataTemplate
o SelectedItemTemplate
o AlternatingItemTemplate
o EditItemTemplate
o InsertItemTemplate

Example:
<form id="form1" runat="server">
<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1">
</asp:ListView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$
ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM
[Customer]"></asp:SqlDataSource>
</form>

Page 15 of 31
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
MORE ACADEMY BSC IT: SEM – V AWP: U4

Q15. What is the application services provided in ASP.NET? Explain. (Nov 2018)
Or
Describe asp.net provider model and direct data access method. (Nov 2019)
ASP.NET 4 ships with a number of application services, of which the most important ones are:
Membership: Enables us to manage and work with user accounts in our system.
Roles: Enables us to manage the roles that your users can be assigned to.
Profile: Enables us to store user-specific data in a back-end database.

Figure below gives an overview of these services and shows how they are related to our website
andthe underlying data stores that the services may use.

A provider is software that provides a standardized interface between a service and a datasource.
ASP.NET providers are as follows:
 Membership
 Role management

 Site map
 Profile
 Session state etc.

At the top of the diagram you see the ASP.NET 4 web sites and web applications that
represent theweb sites that you build. These web sites can contain controls like the login
controls that in turn cantalk to the ASP.NET application services such as membership and
profile. To create a flexible solution, these services don’t talk to an underlying data source
directly, but instead talk to a configuredprovider.
A provider is an interchangeable piece of software that is designed for a specific task. For example,
in the case of the membership services, the membership provider is designed to work with users in
the underlying data store. You can configure different providers for the same application service
dependingon your needs.

Page 16 of 31
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
MORE ACADEMY BSC IT: SEM – V AWP: U4

Q16. Write short notes on data source controls. (Apr 2019) (Nov 2022)
Or
What is the use of data source control? Explain various type of data source
control in ASP.NET

A data source control interacts with the data-bound controls and hides the complex data binding
processes.
These are the tools that provide data to the data bound controls and support execution of
operations like insertions, deletions, sorting, and updates.
Each data source control wraps a particular data provider-relational databases, XML documents,
or custom classes and helps in:
• Managing connection
• Selecting data
• Managing presentation aspects like paging, caching, etc.
• Manipulating data
There are many data source controls available in ASP.NET for accessing data from SQL Server,
from ODBC or OLE DB servers, from XML files, and from business objects.
Based on type of data, these controls could be divided into two categories:
• Hierarchical data source controls
• Table-based data source controls
The data source controls used for hierarchical data are:
• XML Data Source - It allows binding to XML files and strings with or without schema
information.
• Site Map Data Source - It allows binding to a provider that supplies site map information.

The data source controls used for tabular data are:

Data source Control Description


Sql Data Source It represents a connection to an ADO.NET data
provider that returns SQL data, including data
sources accessible via OLEDB and ODBC.
ObjectDataSource It allows binding to a custom .Net business
object that returns data.
LinqDataSource It allows binding to the results of a Linq-to-SQL
query (supported by ASP.NET 3.5 only).
AccessDataSource It represents connection to a Microsoft Access
database.

Page 17 of 31
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
MORE ACADEMY BSC IT: SEM – V AWP: U4

Q17. Explain the ways of formatting the grid view data for display (Apr 2019)

• DataFormatString property of BoundField column can be used to configure the appearance of


numbers and dates using a format string.
• A format string takes the following form
• {0:C} ,where 0 represents the value that will be formatted, and the letter ‘C’ indicates a
predetermined format style.
<asp:BoundField DataField = "Price" HeaderText = "Price" DataFormatString = "{0:C}" />
Formatting the grid view using the style:

<asp:GridView ID = "GridView1" runat = "server" DataSourceID = "sourceProducts"


AutoGenerateColumns = "False">
<RowStyle BackColor = "#E7E7FF" ForeColor = "#4A3C8C" />
<HeaderStyle BackColor = "#4A3C8C" Font-Bold = "True" ForeColor = "#F7F7F7" />
<Columns>
<asp:BoundField DataField = "ProductID" HeaderText = "ID" />
<asp:BoundField DataField = "ProductName" HeaderText = "Product Name" />
<asp:BoundField DataField = "UnitPrice" HeaderText = "Price" />
</Columns>
</asp:GridView>

Page 18 of 31
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
MORE ACADEMY BSC IT: SEM – V AWP: U4

Q18. Write a short note on selecting a Grid view row. (Apr 2019)

• Selecting an item refers to the ability to click a row and have it change color (or become
highlighted) to indicate that the user is currently working with this record.
• Adding a Select Button for each row of a GridView

• Method 1
• By setting AutoGenerateSelectButton attribute of GridView to True.
• This SelectedIndexChanged event of the GridView is triggered when a row's Select button is
clicked.

• Method 2
• This can aloso be achieved by adding a CommandField column with
• the ShowSelectButton property set to true.
• <asp:CommandField ShowSelectButton="true" SelectText="Select College" ButtonType="Link"
/>

• Method 3
• This can aloso be achieved by adding a ButtonField column with
• the CommandName property set to “Select”.
<asp:ButtonField CommandName="Select" Text="Select College" ButtonType="Button" />

Page 19 of 31
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
MORE ACADEMY BSC IT: SEM – V AWP: U4

Q19. Explain data binding with a dictionary collection. (Nov 2019)

Databound controls are designed for data binding.


These controls represents data in either a tabular form or in a detailed form depending on the layouts
each control follows
These data controls not only presents the data but allows us to work on it.

For example we can


Select the data
Sort the data
Edit the data

As discussed, gridview gives allows to sort, to select using following dialog box

 To work on the columns of Gridview ,following dialog box allows to perform the actions such as
 Edit the column Name,
 Add any new column in existing
 Change location of column
 Remove column.

Page 20 of 31
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
MORE ACADEMY BSC IT: SEM – V AWP: U4

Describe the SqlConnection class with example (Nov 2022)

This class is used to read data from SQL Server database. It reads data in forward-only stream of rows
from a SQL Server database. it is sealed class
so that cannot be inherited. It inherits DbDataReader class and implements IDisposable interface.

SqlDataReader Properties:

Property Description
Connection It is used to get the SqlConnection
associated with the SqlDataReader.

Example: Selection of record from student table:

Page 21 of 31
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
MORE ACADEMY BSC IT: SEM – V AWP: U4

Add rows to table using server explorer

using System;
using System.Data;
using System.Data.SqlClient;
class ViewStudent
{
static void Main()
{
// Instantiate the connection
SqlConnection conn = new SqlConnection(@"Data Source=.\sqlexpress;Initial
Catalog=college;Integrated Security=True;Pooling=False");
SqlDataReader dr = null;
try
{
//Open the connection
conn.Open();
// Pass the connection to a command object
SqlCommand cmd = new SqlCommand("select * from student", conn);
// get query results
dr = cmd.ExecuteReader();
// print the Id of each record
while (dr.Read())
{
Console.WriteLine(dr[0]);
}
}
catch(Exception ex)
{
Console.WriteLine(“error”);
}
Finally
{
dr.Close();
conn.Close();
}}}

Page 22 of 31
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
MORE ACADEMY BSC IT: SEM – V AWP: U4

Q21. Describe (i)ExecuteNonQuery , (ii)Execute Scalar and (iii) Execute Reader.


(Nov 2022) (Apr 2023)

(i) ExecuteNonQuery:

ExecuteNonQuery is a method used to execute SQL statements that do not return data, typically used for
operations like INSERT, UPDATE, DELETE, or any other SQL statement that doesn't return a result set.
It is commonly used to make changes to the database, such as adding new records, updating existing
records, or deleting records.
The method returns the number of rows affected by the executed SQL statement, which can be useful to
confirm the success of the operation.
Example:

string connectionString = "YourConnectionStringHere";


string sqlQuery = "UPDATE Customers SET FirstName = 'John' WHERE CustomerID = 1";

using (SqlConnection connection = new SqlConnection(connectionString))


{
connection.Open();
SqlCommand command = new SqlCommand(sqlQuery, connection);
int rowsAffected = command.ExecuteNonQuery();
Console.WriteLine($"Rows affected: {rowsAffected}");
}

(ii) ExecuteScalar:

ExecuteScalar is used to execute an SQL query that returns a single value (usually the result of an
aggregation function like COUNT, SUM, AVG, etc.) or the first column of the first row in a result set.
It is often used when you expect a single value as a result, and it simplifies the process of retrieving that
value.
Example:
string connectionString = "YourConnectionStringHere";
string sqlQuery = "SELECT COUNT(*) FROM Customers";

using (SqlConnection connection = new SqlConnection(connectionString))


{
connection.Open();
SqlCommand command = new SqlCommand(sqlQuery, connection);
object result = command.ExecuteScalar();
int count = Convert.ToInt32(result); // Convert the result to the appropriate data type
Console.WriteLine($"Total customers: {count}");
}

Page 23 of 31
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
MORE ACADEMY BSC IT: SEM – V AWP: U4

(iii) ExecuteReader:

ExecuteReader is used to execute an SQL query that returns a result set (multiple rows and columns).
It is used when you need to retrieve and process multiple rows of data from the database.
The method returns a SqlDataReader object, which you can use to iterate through the result set row by
row.

Example

string connectionString = "YourConnectionStringHere";


string sqlQuery = "SELECT FirstName, LastName FROM Customers";

using (SqlConnection connection = new SqlConnection(connectionString))


{
connection.Open();
SqlCommand command = new SqlCommand(sqlQuery, connection);
SqlDataReader reader = command.ExecuteReader();

while (reader.Read())
{
string firstName = reader["FirstName"].ToString();
string lastName = reader["LastName"].ToString();
Console.WriteLine($"Name: {firstName} {lastName}");
}
reader.Close();
}

Page 24 of 31
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
MORE ACADEMY BSC IT: SEM – V AWP: U4

22. Write any three similarities between FormView and DetailsView controls. Explain
about item templates of form view (Nov 2019)

FormView and DetailsView are two ASP.NET Web Forms controls used for displaying and editing
data. Here are three similarities between FormView and DetailsView controls:

1. Data Presentation:
- Both FormView and DetailsView controls are used to display and edit data from a data source,
such as a database.
- They provide a way to view and interact with a single record at a time.

2. Template-Based Rendering:
- Both controls use templates for rendering their content. Templates allow you to customize the
layout and appearance of the control.
- You can define templates for various modes, such as ItemTemplate, EditItemTemplate, and
InsertItemTemplate, to control how data is presented in different states.

3. Data Binding:
- FormView and DetailsView controls support data binding to bind data source fields to various
controls within their templates.
- They provide a flexible way to bind data to controls like labels, textboxes, and buttons within the
templates.

Now, let's discuss the "Item Templates" of the FormView control in more detail:

The FormView control allows you to define templates for different modes, including ItemTemplate. The
ItemTemplate is used to specify how a single record should be displayed in the read-only (non-
editable) mode. Here's how you can use it in C#:

Html:

<asp:FormView ID="MyFormView" runat="server" DataSourceID="MyDataSource"


DefaultMode="ReadOnly">
<ItemTemplate>
<!-- Place data-bound controls or HTML elements here -->
<asp:Label ID="lblEmployeeName" runat="server" Text='<%# Eval("EmployeeName") %>' />
<asp:Label ID="lblDepartment" runat="server" Text='<%# Eval("Department") %>' />
<!-- Add more controls as needed -->
</ItemTemplate>
</asp:FormView>

In this example, we have a FormView control with an ItemTemplate. Inside the ItemTemplate, we use
data-bound controls like <asp:Label> to display the values of fields from the data source. The Eval
function is used to bind specific data source fields to these controls.

When the FormView control is in the read-only mode, it uses the ItemTemplate to render the data. You
can customize the appearance and layout of the data as needed within the ItemTemplate.

Remember to specify the DataSourceID property of the FormView control to bind it to a data source,
and set the DefaultMode property to specify the initial mode (e.g., ReadOnly, Edit, Insert) of the
control.

Overall, the ItemTemplate of the FormView control allows you to define the presentation of data when
the control is in the read-only mode, giving you full control over the layout and styling.
Page 25 of 31
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
MORE ACADEMY BSC IT: SEM – V AWP: U4

23. Explain various style property of grid view control (Nov 2019)

The GridView control in ASP.NET provides various style-related properties that allow you to customize
the appearance of the grid and its elements. These properties can be set in the markup or
programmatically in C#. Here are some of the commonly used style properties of the GridView control
in C#:

1. HeaderStyle: This property controls the style of the header row of the GridView.
csharp
myGridView.HeaderStyle.CssClass = "header-style";

2. RowStyle: This property allows you to define the style for data rows in the GridView.
csharp
myGridView.RowStyle.CssClass = "row-style";

3. AlternatingRowStyle: Similar to RowStyle, this property defines the style for alternating data rows for
better readability.
csharp
myGridView.AlternatingRowStyle.CssClass = "alt-row-style";

4. SelectedRowStyle: This property sets the style for the selected row when a row is selected.
csharp
myGridView.SelectedRowStyle.CssClass = "selected-row-style";

5. EditRowStyle: It specifies the style for the row when it's in edit mode.
csharp
myGridView.EditRowStyle.CssClass = "edit-row-style";

6. PagerStyle: This property controls the style of the pager row, which includes navigation controls like
Next, Previous, Page Numbers, etc.
csharp
myGridView.PagerStyle.CssClass = "pager-style";

7. EmptyDataRowStyle: Sets the style for the row that is displayed when the GridView has no data to
show.

csharp
myGridView.EmptyDataRowStyle.CssClass = "empty-row-style";

8. FooterStyle: Defines the style for the footer row of the GridView.

csharp
myGridView.FooterStyle.CssClass = "footer-style";

Page 26 of 31
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
MORE ACADEMY BSC IT: SEM – V AWP: U4

9. ControlStyle: Controls the overall style of the GridView, affecting its border, padding, and other
general appearance settings.

csharp
myGridView.ControlStyle.CssClass = "gridview-style";

10. BorderStyle: Specifies the border style of the GridView.

csharp
myGridView.BorderStyle = BorderStyle.Solid;

These style properties allow you to control the appearance of different parts of the GridView control,
including header, data rows, footer, pager, and more. You can apply CSS classes to these properties
to define the styles, or you can use inline style attributes to set specific styles for each property.
Additionally, you can use other properties like ForeColor, BackColor, BorderColor, BorderWidth, and
Font to further customize the appearance of the GridView and its elements.

Page 27 of 31
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
MORE ACADEMY BSC IT: SEM – V AWP: U4

24. Write a code to display data from a table named students (Rollno, Name, Marks) and
display grid view control when page is loaded. (Nov 2022)

To display data from a table named "students" with columns "Rollno," "Name," and "Marks" in a GridView
control when a page is loaded, you can follow these steps in C#. Assuming you have a database
connection already established, here's a sample code snippet:

csharp
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class YourPageName : System.Web.UI.Page


{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Call a method to fetch data from the database and bind it to the GridView
BindStudentData();
}
}

protected void BindStudentData()


{
// Connection string for your database
string connectionString = "YourConnectionStringHere";

// SQL query to fetch data from the "students" table


string query = "SELECT Rollno, Name, Marks FROM students";

using (SqlConnection connection = new SqlConnection(connectionString))


{
using (SqlCommand command = new SqlCommand(query, connection))
{
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataTable dataTable = new DataTable();

// Open the database connection and fill the DataTable with data
connection.Open();
adapter.Fill(dataTable);
connection.Close();

// Bind the DataTable to the GridView


GridView1.DataSource = dataTable;
GridView1.DataBind();
}
}
}
}

Page 28 of 31
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
MORE ACADEMY BSC IT: SEM – V AWP: U4

1. Replace "YourPageName" with the name of your web page.

2. Set the connectionString variable to your actual database connection string.

3. Modify the query variable to match the SQL query you need to retrieve data from the "students" table.

4. The BindStudentData() method is called in the Page_Load event handler. It checks whether the page is
being loaded for the first time (not a postback) and then fetches data from the database and binds it to the
GridView control.

5. Make sure to have a GridView control with the ID "GridView1" in your web page's markup.

Page 29 of 31
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
MORE ACADEMY BSC IT: SEM – V AWP: U4

Page 30 of 31
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622
MORE ACADEMY BSC IT: SEM – V AWP: U4

Page 31 of 31
YouTube - Abhay More | Telegram - abhay_more
607A, 6th floor, Ecstasy business park, city of joy, JSD road, mulund (W) | 8591065589/022-25600622

You might also like