Working With Master Pages Themes & Skins Collections & Lists Data Binding Working With XML
Working With Master Pages Themes & Skins Collections & Lists Data Binding Working With XML
Working With Master Pages Themes & Skins Collections & Lists Data Binding Working With XML
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
Softsmith Infotech
Working with Master Pages
Softsmith Infotech
Namespaces
• Compilation units that let you organize and
reuse code
• No relationship between namespaces and file
structure (unlike Java)
• Namespaces provide a way to uniquely identify
a type
• Provides logical organization of types
• Namespaces can span over assemblies
• Namespaces can be nested
• The fully qualified name of a type includes all
namespaces
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
Softsmith Infotech
Collections
• A collection is a specialized class that organizes and exposes a
group of objects
Softsmith Infotech
ArrayList
• Arraylist allows to dynamically add and remove
items from a simple list
• Array List is a zero based collection
• The items in the list are retrieved by accessing
the item index
• Methods
– Add()
– Remove()
– RemoveAt()
– Count()
• Property
– Capacity
Softsmith Infotech
SortedList
• Stores elements in the collection as a key-value pair that are sorted by
the keys
• The elements in the SortedList can be accessed by key as well as by
index
• A key cannot be a null reference whereas a value can be a null
reference
• Any time when an element is added or removed from the collection,
the indexes are adjusted to keep the list in the sorted order. Hence
such operations are slower in this collection
• Methods
– Add()
– Remove()
– RemoveAt()
– Count()
• Property
– Count, Capacity, Item, Keys, Values
Softsmith Infotech
Data Binding
• Bounding values to the controls
• <<Control>>.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
Softsmith Infotech
Single Value Data Binding
• Applicable for server controls that displays
one data at a time
• Controls like
– Textbox
– Label
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
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
Softsmith Infotech
Data Management
• Classes used (for Sql)
– Connection - SqlConnection
– Command - SqlCommand
– DataReader - SqlDataReader
– DataAdapter – SqlDataAdapter
Softsmith Infotech
Connection Methods
• Open – Opens a connection
• Close – Closes a connection
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 stored
procedure or table
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
Softsmith Infotech
DataAdapter
• This is for filling data from more than one
tables
• The data get filled into a DataSet
• OdbcDataAdapter da = new
OdbcDataAdapter(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
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 and
closes a connection. No need of having
explicit open and close of a connection.
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
Softsmith Infotech
Opening an XML
• System.Xml namespace is required
• Opening from an URL
XmlDocument myDoc = new XmlDocument();
myDoc.Load ("http://localhost/sample.xml");
Softsmith Infotech
Example
• To get the name and values of attributes in a xml file
Softsmith Infotech
Writing XML
• Create XML Document object
XmlDocument myDoc = new XmlDocument();
Softsmith Infotech
Writing XML
• Create an attribute
XmlAttribute myAttribute =
myDoc.CreateAttribute(“Trainer");
myAttribute.Value = “Softsmith";
Softsmith Infotech
XmlReader and XmlWriter
• XmlReader – To read an XML file
• XmlWriter – To write Xml to a file (creating
xml)
• XmlReader
string filename=@"books.xml";
XmlTextReader bookXmlReader = new XmlTextReader (filename);
• XmlWriter
string fileName = @"booksnew.xml";
XmlTextWriter bookXmlWriter = new XmlTextWriter(fileName,null);
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 & type
Console.WriteLine(xmlR.NodeType + " " + xmlR.Name);
}
}
catch(XmlException e) {
Console.WriteLine ("Error: " + e.Message);
}
}
Softsmith Infotech
XmlWriter Example
public 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:
<?xml version=“1.0”?>
<Person>
<Phone>111-222-3333</Phone>
<Phone>111-222-4444</Phone>
</Person>
Softsmith Infotech