objects and web scripting

scripting typically involves creating objects in order to execute certain tasks to achieve a desired outcome. and so … how do i create an object? it sounds silly, really. but, honest … its abstract … a tough concept around which to wrap one’s head. its the core of modern-day programming, though. so i had better start getting it.

object creation involves 2 steps.

(1). create an object definition ~ the code containing methods and properties of the object. methods ~ directions for making the object. properties ~ characteristics that typify the object.

create a constructor function ~ a named group of 1 or more programming statements that can create new objects. note – by convention, capitalize the first letter of a constructor function.

add properties ~ identified by object name “dot” property name. refer to the newly created, not yet instantiated objects as “this”

objectName.propertyName = value

add methods ~ (methods = functions called from within the method.) name the method, assign a function. use action words when naming methods.  identified using the syntax below. (argument = input to subroutine … used to assign a value to a property of the new object)

objectName.methodName(arguement)

(2) instantiation ~ create an  instance of the newly defined object. specify a unique name for the new object. assign the object to the recently created object definition using the “new” keyword.

steps in object instantion ~ variable assigned a value ~ variable value passed as a parameter (parameters act as local copies of the argument) to the new object ~ parameter used to assign variable value to an object property ~ object property used by the object’s show method ~ show calls the object’s display method, which writs to the browser.

a primer on scripting objects

client-side scripting ~ runs in a browser: interpreted when browser loads page. to create ~ must specify type/language attribute in opening script tag. the document object methods document.write ( ) and document.writeln ( ) return output to the web page. document.writeln inserts a carriage return at the end of each line. to call external client-side script files ~ use *.js format/file extension to denote the file with the script tag’s source (src) attribute. when creating an external script file, just begin writing your script ~ no special tags required.

<script> src=’filenamehere.js’ type = ‘text/JavaScript’… </script>

server-side scripting (windows/MS) ~ runs on a web server: interepreted by the web server’s script engine. to create ~ can use (i) script tag pair with script’s runat attribute set to server, or (ii) inline tags.

(i) <script> type = ‘text/JavaScript’ runat=’server’… </script>
(ii) <% … %>

the asp (active server page) model comprises 6 objects: response, request, application, session, server, objectContext. to return output to the web page, use the response object’s write method: response.write( ). to reference an external server-script, call the *.inc file. all server-side code gets stripped from the html code before browser/client receives it ~ cannot be viewed via view source code.

html and beyond ~ xml

phase 2 has come and gone. the integration project ~ done. not quit exactly as set out in specs … but i produced a functioning application with a back end connection to sql server. i eliminated the class library in favour of coding the user interface. to ensure meeting the tight deadline for the project. and then i moved onto HTML. cool ~ me loves HTML. i had a blast making a website ~ for marks! haha. it felt like playing and getting credit for it. i could do this for a living. and now … now i’m into XML. it should have been scripting, but, a matter of book availability meant i had to take the XML course first. whatever. i had the example of blogger in my head wrt to xml. its amazing how much one learns simply from curious exposure to something.

the design stage never ends

not really. in a way. i mean, don’t we forge ahead with a view to how we could enhance or improve any little thing along the way? the art of design lies in working out for oneself the details that one requires in order to begin a project. i mean, some details, once worked out and implemented, we cannot change. and so, we should consider carefully.

its sort of an iterative process. its sort of a process that involves all phases engaged in the brain at once. it cannot distill itself into phase components. not in my brain, anyhow. it all just ferments in there all at once. iteratively.

whew. well, it looks like i have just about survived another quarter of this programming/web development course of study. now its onto the fun stuff. yeehaw! now i think i wanna slow down. and enjoy the ride. more about the project for my next post.

so, what about the design phase?

do we need it? i mean, does anyone seriously sit down and spend hours creating all those wacky scenario diagrams, flow charts, and shit? ok. i wonder. coz, on one hand, i think its a load of BS. and on the other hand, i think the genius of any program lies in its design. how many times have you gotten to the tail end of coding your programme, and arrived at some glitch. a glitch you could have caught earlier in the process, had you only just attended to, followed through with and contemplated your programme’s design more thoroughly?

and ask yourself. does a house builder go out and get all the construction equipment as soon as he decides he’s building a house? what about a blue print? some sort of building plan? electrical? plumbing? it would seem an expensive and wasteful activity to approach house building in such a way. yet … so many of us approach program design that way. i wonder why?

and i wonder. why do we think we have to fit our design ideas into some pre-fab diagram? what if my neural pathways don’t produce cognitive output that fits neatly into those uml charts and diagrams? what if … my brain conceives a programming situation differently? its still design right? even if its not some pretty, prefab chart, like they show in the textbooks?

the project, part 2

ok. so i figured it out. my previous post set me on the right track. here’s what i arrived at, finally. just using the adapter that i created on the form and adding the database tables i need. and then just coding the on-click event of each button to perform the function – i.e. add/update a row. no need to create the connection to the database manually for each and every button. simple. not sure why the ’sample’ had such complicated coding.

as for loading the data … i simply attached the loading of the dataset to each tab page. only one page has data actually bound to the text/list box controls. the other forms are form entering data to add to the database. and there’s the report viewer. that’s it. simple.
then i figured out the audit thing. even handler, indeed. i changed the system to record  payment voids – the amount, date, ID. but i see from testing that information can be changed on my updates form. and so i need a second audits table to record changes made to information made on that form.

i question having an add lease form and a separate update lease form. once again, i followed the lead of the sample. but … now i wonder what the point of that separate form is, really. i suppose, if i’m still convinced its superfluous by monday a.m. i can just delete the controls and make it a welcome/instruction tab page.

project: design a program~c-sharp front-end; sql server back-end

ok. so i have a windows form with 4 tab-controlled pages. each tab page related to a different function of the program. tab 1 ~ add a lease ~ 2 buttons and several text boxes. tab 2 ~ crystal report viewer. tab 3 ~ update lease ~ 3 buttons and several text boxes. tab 4 ~ make payment ~ 1 button, 1 check box and several text boxes. at the back end, a SQL Server 2000 database. no class libraries for this project. just windows controls and event handlers and ADO.NET controls.

ok. so. i am looking at the source code of a ’sample’ … something given to me to inspire my own creation. its deficient. lacking in some of the basic, stated requirements. and coded weirdly. like … each windows control – i.e. button – click event has the ADO.NET objects coded within it. i wonder, why not just attach the connection to the tab-click of the tab page to which it pertains? why repeat the same code for so many buttons? i don’t get it.

and what of an event handler that considers the whole audit thing? the audit thing being that, an audit table must get updated in the event of any changes made to the lease table can i attach an audit-related event handler to ‘add lease’ (submit) and ‘update lease’ (update) buttons? is that what i do to implement the auditing system?

and what about the whole void payments thing? the ‘make payment’ tab page has a check box for users to indicate whether they are voiding the payment. does void now become a new field in my payments table? this void field would take on a boolean/binary type – 0 for false or not voided and 1 for true or voided, right? that seems to make sense.

ADO.NET ~ classes and objects

ADO.NET ~ the name for a set of classes used with C# and the .NET framework to access data in a relational, table-oriented format. this includes relational databases such as Acess, SQL as well as other databases and even non-relational data sources. located within the System.Data.dll assembly. developed for the purpose of providing a set of easy-to-use classes with which to access data … extensibility: supports more data sources that predecessors … support for multi-tiered internet applications.

classes and objects detailed in a previous post.

DataReader ~ five steps to retrieving data from a dataTable in SQL~ 1.connect to the data source … 2. open the connection … 3. issue an SQL query … 4. read and display the data with the data reader … 5. close the data reader and connection.

DataSet ~ 1. create connection … 2. create a dataAdapter object … 3. create a dataSet … 4. fill dataSet with data … 5. use a foreach statement to write data from the dataSet to the console … no need to open or close connection – data adapter manages that.

*data updated through the dataSet object and its objects, i.e. dataRow.

SqlDataAdpater.Update () ~ this method goes thru the rows in a DataTable to check for data marked for changes. each DataRow object in the Rows collection has a property, RowState, tracking whether the current row marked for deletion, addition, modification, or whether unchanged. any changes made get reflected in the database.

Find () ~ a method in the DataTable Rows collection that ~ goes through rows in a DataTable finds data in rows by using the PK.

Delete () ~ a method in the DataRow object that deletes the current row.

*use Remove () to remove rows from dataSet, while leaving underlying database unchanged.

**reproduce graphics p. 619 … p. 634 … **

DataRelation Object ~ used to describe the relation betweeen multiple DataTable objects. Each DataSet has a Relations collection of DataRelations that enables you to find and manipulate related tables. ~objects to represent relationships between fields, data~ creating objects that represent relationships enable navigation among the data of related tables.

Add () method of Relations accepts a string name for the relationship and two DataColumns: the parent column, followed by the child column.

GetChildRows () method resides in DataRow object ~ loops thru each DataRow in the Rows collection and returns just the related rows. (GetParentRows() works analogously)

to access data with multiple relations, initialize a connection to the data source, create a DataSet and then a data adapter for each table required …. then build a data relations object for each of the relationships between the tables … then use foreach loops to process data using child-parent hierarchy.

DataSet has several methods that process XML ~ WriteXML()ReadXML()

DataAdapter also has properties to access SQL commands used in data update more directly than with command builder.

Stored Procedures require passage to the comman objects constructor of the stored procedure’s name and the connection used. items to remember:

1. set the CommandType of the command object to StoredProcedure

2. with parameters collection its possible to create and add input, output and return parameters for use with stored procedures

3. stored procedure also make it possible to retrieve information about the DataTable, DataRow, and DataColumns using the Get and Set methods

4. it’s possible to derive parameters from the parameters collection using the CommandBuilder’s DeriveParameters method.

XML and data tranfer

XML ~ eXtensible markup language ~ an open standard for defining data and its elements, making it readable in a variety of platforms, by a variety of applications. XML facilitates data definition thru its use of tags, similarly to HTML. however, XML defines the structure of the data, whereas HTML defines the appearance of the data. a complete set of XML elements make up an XML document.

two levels of ‘correctness’ of XML exist: 1. well formed ~document obeys the rules required by the XML standard, and, 2. valid ~ document conforms to specific rules of the specific XML application.

DTDs and schemas provide ways of validating XML documents. DTDs ~ document type definitions ~ inflexible; do not allow for specification of attributes and element types. Schemas ~ more flexible; more complex; XSD and XDR. 1. XSD ~ XML Schema Definition language ~ defines a type of XML document in terms of constraints upon what elements and attributes may appear, their relationship to each other, what types of data may be in them, and other things. It can be used with validation software in order to ascertain whether a particular XML document is of that type; more powerful, more complex. 2. XML-Data Reduced schemas ~ similar to XSD, but its element defined within the top-level element of the schema; no premable for declarations, instead using namespace declarations.

linking application to data

so … we have an application, written in c#. we have a rockin’ sql database. how do we get it together? how do we get sql and c# to talk to each other? that’s what this post will discuss. we need an API – an application programming interface – that will facilitate our program’s access to the data to carry out specified functions. a number of alternatives exist.

1. ODBC ~ open database connectivity ~ a procedural API (common set of functions/methods) for using SQL queries to access data. limitations: unsuitable for accessing non-relational data such as spreadsheets, text files and xml files. An implementation of ODBC will contain one or more applications, a core ODBC library, and one or more “database drivers”. The core library, independent of the applications and DBMS, acts as an “interpreter” between the applications and the database drivers, whereas the database drivers contain the DBMS-specific details. Thus a programmer can write applications that use standard types and features without concern for the specifics of each DBMS that the applications may encounter. Likewise, database driver implementors need only know how to attach to the core library. This makes ODBC modular.

2. OLE DB ~ object linking and embedding, database ~ a procedural API developed by MS as a successor to ODBC, to extend its feature set to support a wide variety of non-relational databases, such as object databases, and spreadsheets. OLE DB separates the data store from the application that needs access to it through a set of abstractions that include the datasource, session, command and rowsets. This was done because different applications need access to different types and sources of data and do not necessarily want to know how to access functionality with technology-specific methods. OLE DB is conceptually divided into consumers and providers. The consumers are the applications that need access to the data, and the provider is the software component that implements the interface and therefore provides the data to the consumer.

3. ADO.NET ~ activeX data object .NET ~ an API that’s a base class in the MS .NET framework. ADO.NET consists of a set of objects in the Systems.Data namespace that communicate with the database via .NET providers. these objects allow connectivity to the database to retrieve, edit, delete and insert data in the database and manipulate the data within the program. ADO.NET consists of two parts – (i). the DataSet and (ii). the .NET provider.

(i). DataSet ~ does not provide or maintain connectivity to the database. provides a copy of the data source – in memory – with which to work. consists of a nested collection of objects that allow for manipulation of data within the dataset. see figure below.

(ii).NET data provider objects ~ consists of objects that faciliate connectivity to individual data sources. each provider resides in its own namespace within the System.Data namespace. @ present, 4 main components. see figure below.

to access an SQL Server database requires an OLE DB connection provider.

Next Page »