aspdotnet presentation part2
TRANSCRIPT
-
8/14/2019 ASPdotNET Presentation Part2
1/30
Softsmith Infotech
.Net
Table of contents Introduction to VS 2005 Application and Page Frameworks GUI Controls Validation Server Controls Working with Master Pages Themes & Skins Collections & Lists
Data Binding Data Management with ADO.Net Working with XML Site Navigation Security State Management Caching Debugging & Error Handling File I/O & Streams Configurations
-
8/14/2019 ASPdotNET Presentation Part2
2/30
Softsmith Infotech
Working with Master Pages
All web application will have a master page or a
home page If we hit a website the default page that comes is
the home page
It will have the name index.html or default.aspxor home.html or some thing like that.
From this master page, we navigate to the restof the pages through buttons or links
To set a page as the master page all we need todo is, just right click on the aspx page in thesolution explorer and select Set as Start Pagefrom the pop up menu.
-
8/14/2019 ASPdotNET Presentation Part2
3/30
Softsmith Infotech
Themes and Skins
Themes and Skins provide a good look
and feel to our site or web application.
We can set the theme using CSS(Cascading Style Sheet)
In the portion of our aspx page if
we give
then the particular style specified in the
CSS will be used for the page
-
8/14/2019 ASPdotNET Presentation Part2
4/30
Softsmith Infotech
Themes and Skins
We can also allow the user to change the
skin (color of controls on the application)
and dynamically deliver the theme and
skin on his browser.
This is achieved using session variables.
Once the user selects a skin,the value is
stored in a session variable and it will be
used for the session.
-
8/14/2019 ASPdotNET Presentation Part2
5/30
Softsmith Infotech
Themes and Skins
If the user want the same skin to beapplied whenever he logs in, there are twoways to achieve that.
Cookies The information will be stored onthe clients system and will be taken when helogs in from the system again. But this willwork only in a particular system
To make this global, the information need tobe stored on the server, in a table against theuser name.
-
8/14/2019 ASPdotNET Presentation Part2
6/30
Softsmith Infotech
Namespaces
Compilation units that let you organize andreuse code
No relationship between namespaces and filestructure (unlike Java)
Namespaces provide a way to uniquely identifya type
Provides logical organization of types
Namespaces can span over assemblies
Namespaces can be nested
The fully qualified name of a type includes allnamespaces
-
8/14/2019 ASPdotNET Presentation Part2
7/30
Softsmith Infotech
Namespaces
The fully qualified name of a type includes all namespaces
Namespaces are mainly used to distinguish between the objects
having same names
With-in a namespace, the names of all the objects should be unique
namespace N1 { // is referred to as N1
class C1 { // is referred to as N1.C1
class C2 { // is referred to as N1.C1.C2
} //End of N1.C1.C2
} //End of N1.C1
namespace N2 { // is referred to as N1.N2
class C2 { // is referred to as N1.N2.C2
} // End of N1.N2.C2
} //End of N1.N2
} //End of N1
-
8/14/2019 ASPdotNET Presentation Part2
8/30
Softsmith Infotech
Collections
A collection is a specialized class that organizes and exposes a
group of objects
Various collection classes are ArrayList, SortedList, BitArray,
HashTable, Queue and Stack
They are all included in System.Collections namespace
Like arrays, members of collections can be accessed by an index
Unlike arrays, collections can be resized dynamically
-
8/14/2019 ASPdotNET Presentation Part2
9/30
Softsmith Infotech
ArrayList
Arraylist allows to dynamically add and removeitems from a simple list
Array List is a zero based collection
The items in the list are retrieved by accessingthe item index
Methods Add()
Remove()
RemoveAt()
Count()
Property Capacity
-
8/14/2019 ASPdotNET Presentation Part2
10/30
Softsmith Infotech
SortedList
Stores elements in the collection as a key-value pair that are sorted bythe keys
The elements in the SortedList can be accessed by key as well as byindex
A key cannot be a null reference whereas a value can be a nullreference
Any time when an element is added or removed from the collection,the indexes are adjusted to keep the list in the sorted order. Hencesuch operations are slower in this collection
Methods Add()
Remove() RemoveAt() Count()
Property Count, Capacity, Item, Keys, Values
-
8/14/2019 ASPdotNET Presentation Part2
11/30
Softsmith Infotech
Data Binding
Bounding values to the controls
.DataBind() method binds
values for the control
Page.DataBind() binds all the controls on
the page
Types Single value data binding
Multi value data binding
-
8/14/2019 ASPdotNET Presentation Part2
12/30
Softsmith Infotech
Single Value Data Binding
Applicable for server controls that displays
one data at a time
Controls like
Textbox
Label
-
8/14/2019 ASPdotNET Presentation Part2
13/30
Softsmith Infotech
Multi Value Data Binding
Involves binding server controls to
ArrayList or SortedList or any collection
object
Example
Populating a drop down list with a collection
object or a data set
Populating a Data grid with a data set
-
8/14/2019 ASPdotNET Presentation Part2
14/30
Softsmith Infotech
Data Management with
ADO.Net ADO ActiveX Data Objects
Namespace
System.Data Types
Odbc For working with MySQL etc
OleDb For working with OLEDB (Excel etc)
Sql For working with MS SQL data bases
Oracle For working with Oracle databases
-
8/14/2019 ASPdotNET Presentation Part2
15/30
Softsmith Infotech
Data Management
Classes used (for Sql)
Connection - SqlConnection
Command - SqlCommand
DataReader - SqlDataReader
DataAdapter SqlDataAdapter
For other database types, we need to putthe appropriate prefix.(like
OdbcConnection and so on)
-
8/14/2019 ASPdotNET Presentation Part2
16/30
Softsmith Infotech
Connection
OdbcConnection con = new
OdbcConnection(Connection string)
Connection string has details about which
database is to be used and what is the
user name and password are.
Example Driver={MySQL ODBC 3.51 Driver};SERVER=localhost;DATABASE=test;
password=sa;user id=root;
-
8/14/2019 ASPdotNET Presentation Part2
17/30
Softsmith Infotech
Connection Methods
Open Opens a connection
Close Closes a connection
It is recommended to open a connection
only if it is needed and close it when it is
no longer needed. This would avoidwastage of system resources
-
8/14/2019 ASPdotNET Presentation Part2
18/30
Softsmith Infotech
Command
OdbcCommand cmd = new
OdbcCommand(Query, con);
Query SQL query like select * from
table1
con is the connection object created
Command object property CommandType This can be text or storedprocedure or table
-
8/14/2019 ASPdotNET Presentation Part2
19/30
Softsmith Infotech
DataReader
OdbcDataReader dr = new OdbcDataReader();
Usage - dr.Method
DataReader Methods:
ExecuteReader For reading one or more rows (for
select * from)
ExecuteScalar For reading a scalar value like select
count(*) from
ExecuteNonQuery for inserting or updating or
deleting or executing a stored procedure or function
-
8/14/2019 ASPdotNET Presentation Part2
20/30
Softsmith Infotech
DataAdapter
This is for filling data from more than onetables
The data get filled into a DataSet
OdbcDataAdapter da = newOdbcDataAdapter(cmd)
cmd command object created using the
connection and SQL statement This will fetch the result of the command
and stores it in the adapter
-
8/14/2019 ASPdotNET Presentation Part2
21/30
Softsmith Infotech
DataAdapter
To put the data in a dataset, use the Fill
method
da.Fill(ds) DataSet ds = new DataSet()
We can also have DataTable or DataRow
or DataView instead of DataSet
Data adapter automatically opens andcloses a connection. No need of having
explicit open and close of a connection.
-
8/14/2019 ASPdotNET Presentation Part2
22/30
Softsmith Infotech
Working with XML
XML eXtensible Markup Language
Uses
XML can be used to Store Data XML is used to create configuration files
for different applications
XML is used to Exchange Data in cross-platform applications
Used in Web applications
-
8/14/2019 ASPdotNET Presentation Part2
23/30
Softsmith Infotech
Opening an XML
System.Xml namespace is required
Opening from an URLXmlDocument myDoc = new XmlDocument();
myDoc.Load ("http://localhost/sample.xml");
Opening from a location in the local
systemXmlDocument myDoc = new XmlDocument();
FileStream myFile = new
FileStream("myDoc.xml",FileMode.Open);
myDoc.Load(myFile);
myFile.Close();
-
8/14/2019 ASPdotNET Presentation Part2
24/30
Softsmith Infotech
Reading the XML document
XmlElement Class for accessing
individual elements in a xml file
Attributes, FirstChild, LastChild, InnerText,
InnerXml, Name
XmlAttribute Class for accessing
attributes of the individual xml elements
Name
Value
-
8/14/2019 ASPdotNET Presentation Part2
25/30
Softsmith Infotech
Example
To get the name and values of attributes in a xml file
/* Get the Attribute Collection */
XmlAttributeCollection attrs = myElement.Attributes;
/* Get the number of Attributes */
int aCount = attrs.Count;
for (i=0; i< aCount; i++)
{
Console.WriteLine (attrs[i].Name);Console.WriteLine (attrs[i].Value);
}
-
8/14/2019 ASPdotNET Presentation Part2
26/30
Softsmith Infotech
Writing XML
Create XML Document objectXmlDocument myDoc = new XmlDocument();
Load the root element
myDoc.LoadXml ("");
Create an element and add it to the parent
elementXmlElement myChildEle =
myDoc.CreateElement(Topic");
myChildEle.InnerText = Dot Net";
ParentElement.AppendChild (myChildEle);
-
8/14/2019 ASPdotNET Presentation Part2
27/30
Softsmith Infotech
Writing XML
Create an attributeXmlAttribute myAttribute =
myDoc.CreateAttribute(Trainer");
myAttribute.Value = Softsmith";
Add it to Parent elementParentElement.SetAttributeNode (myAttribute);
XML file will be
Dot Net
-
8/14/2019 ASPdotNET Presentation Part2
28/30
Softsmith Infotech
XmlReader and XmlWriter
XmlReader To read an XML file
XmlWriter To write Xml to a file (creating
xml)
XmlReaderstring filename=@"books.xml";
XmlTextReader bookXmlReader = new XmlTextReader (filename);
XmlWriterstring fileName = @"booksnew.xml";
XmlTextWriter bookXmlWriter = new XmlTextWriter(fileName,null);
-
8/14/2019 ASPdotNET Presentation Part2
29/30
Softsmith Infotech
XmlReader Example
public void ReadDocument (XmlReader xmlR)
{
try {
// read (pull) the next node in document order
while (xmlR.Read()) {
// print the current node's name & typeConsole.WriteLine(xmlR.NodeType + " " + xmlR.Name);
}
}
catch(XmlException e) {
Console.WriteLine ("Error: " + e.Message);}
}
-
8/14/2019 ASPdotNET Presentation Part2
30/30
Softsmith Infotech
XmlWriter Examplepublic void WriteDocument(XmlWriter writer)
{
writer.WriteStartDocument();
writer.WriteStartElement ("Person");
writer.WriteAttributeString ("Gender", "Male");
writer.WriteAttributeString ("Name", "Abc");
writer.WriteElementString ("Phone", "111-222-3333");
writer.WriteElementString ("Phone", "111-222-4444");
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Close();
}
Output:
111-222-3333111-222-4444