Difference between revisions of "DD Class13"

esse quam videri
Jump to: navigation, search
(Homework)
m (Text replacement - "syntaxhighlight lang="csharp" line="1" " to "syntaxhighlight lang="csharp"")
 
(28 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
[[Category:Data Design]]
 
[[Category:Data Design]]
  
==Master Pages==
 
  
*http://iam.colum.edu/dd/gbrowser.php?file=/classsource/class11/UserControls/TemplateIncludes.aspx
+
==Transactions in ADO==
*http://iam.colum.edu/dd/gbrowser.php?file=/classsource/class11/UserControls/TemplateNoControls.aspx
 
*http://iam.colum.edu/dd/gbrowser.php?file=/classsource/class11/UserControls/TemplateUserControls.aspx
 
  
Master Page with ConentPlaceholders
+
Transactions in ADO.NET http://msdn.microsoft.com/en-us/library/777e5ebh.aspx
 +
<syntaxhighlight lang="csharp">
 +
SqlTransaction tran = null;        //Declare Transaction Object
 +
 +
        try
 +
        {
 +
            conn.Open();
 +
            tran = conn.BeginTransaction(); //init trans object
 +
            command.Transaction = tran;    //use tran object on all commands and adapters
 +
            command = new SqlCommand(strSQL2, conn, tran);
 +
            tran.Commit();      //commmit
  
Master Page
+
        }
 +
        catch (Exception eTran)
 +
        {
 +
            tran.Rollback();
 +
        }
 +
        finally
 +
        {
 +
            conn.Close(); 
 +
        }   
 +
</syntaxhighlight>
 +
and now the ADO way
  
http://iam.colum.edu/dd/gbrowser.php?file=/classsource/class11/MasterPageClass11.master
+
[http://iam.colum.edu/dd/classsource/Logon/trans.aspx trans.aspx] - [http://iam.colum.edu/dd/gbrowser.php?file=/classsource/Logon/trans.aspx source]
  
Real Page with user controls
+
==Transactions in LINQ==
  
http://iam.colum.edu/dd/classsource/class11/UserControls/TemplateMasterPagesUserControls.aspx [http://iam.colum.edu/dd/gbrowser.php?file=/classsource/class11/UserControls/TemplateMasterPagesUserControls.aspx - source]
+
Don't use DTS this a much bigger...
  
==Review Data Diagram for final==
+
DTS transactions http://msdn.microsoft.com/en-us/library/bb387002.aspx#Y0
  
*are they normal?
+
Use DbTransaction http://msdn.microsoft.com/en-us/library/system.data.common.dbtransaction.aspx
*can the design acomplish your goals?
 
*are they over engineered?
 
  
==Final Requirements==
+
<syntaxhighlight lang="csharp">
*Fully normal database
+
DbTransaction trans = db.Connection.BeginTransaction();
*An aspx page must Insert, Update and Delete(or update to set but inactive)
+
</syntaxhighlight>
*ASPX databinding to datacontrols either SQLDatasource or ADO(dal) or both
 
*Proper validation of webforms
 
  
 +
[http://iam.colum.edu/dd/classsource/LINQ/LINQTransaction.aspx LINQTransaction.aspx] - [http://iam.colum.edu/dd/gbrowser.php?file=/classsource/LINQ/LINQTansaction.aspx source]
 +
[http://iam.colum.edu/dd/gbrowser.php?file=/classsource/LINQ/LINQTansaction.aspx.cs LINQTransaction.aspx.cs]
  
 +
==Sql Aggregate Functions==
  
==DataView RowFilter and DataTable Select==
+
They perform a calculation on a set to values and return a single value.
 +
Aggregate functions are determinate. They will return the same value given the same dataset.
  
data in a DataTable can be filtered by using DataTable.Select. DataTable.Select return an array of matching DataRows
+
*AVG
 +
*MIN 
 +
*CHECKSUM
 +
*SUM 
 +
*CHECKSUM_AGG
 +
*STDEV 
 +
*COUNT
 +
*STDEVP 
 +
*COUNT_BIG
 +
*VAR 
 +
*GROUPING
 +
*VARP 
 +
*MAX
  
You can also filter data by using a DataView. DataViews are similar to view in SQL they can be filtered by calling
+
<sql>
 +
SELECT MAX(PricePerPound) as MaxPrice from CheesePrice
  
DataRow.RowFilter.
+
SELECT AVG(PricePerPound) as AvgPrice from CheesePrice
  
In both situation the string expression in the Rowfilter or passed in to the Select funtion should be a WHERE clause.
+
SELECT COUNT(CheeseID) as Count from CheesePrice
 +
</sql>
  
 +
==Sub Query==
  
http://iam.colum.edu/dd/classsource/class13/DataView.aspx [http://iam.colum.edu/dd/gbrowser.php?file=/classsource/class13/DataView.aspx - source]
+
A query may be a part of another query. This allows
  
==Homework==
+
IN vs EXSISTS
  
Post image of your diagram. Be sure it is properly documented. Show your name and the course semester and name.
+
Remember the Many to Many relation ship in the Student Registration System
  
Large format up to and thumbnail that is 175px wide
+
[[Image:ManyToManyStudentCourses.png]]
  
Fill out skeleton files for you final project. All the aspx pages that make up you project should be created. They don't all have to work but they should all be linked, names and have notes on how the page is going to work.
+
Here a student has many course and a course has many student. Imagine in the registration system the student will be presented with a list of classes that they can add. That list of classes '''should not''' include the courses that they are already taking.
 +
Course
 +
{| class="wikitable" cellpadding="5" cellspacing="0"
 +
!CourseID !! CourseName !! CourseNumber
 +
|-
 +
|9 || Science || 66-0002
 +
|-
 +
|10 || Math || 66-0001
 +
|-
 +
|11 || Data Design || 36-2601
 +
|-
 +
|168 || Application Design || 36-4601
 +
|-
 +
|169 || OOP || 36-2001
 +
|}
  
Have questions ready for next week. If there is anything you need help with or somehting you need ot learn to accomplish you project be ready to ask.
+
Student
 +
{| class="wikitable" cellpadding="5" cellspacing="0"
 +
!StudentID !! PeopleID !! OasisID
 +
|-
 +
|1 || 1 || 666666
 +
|-
 +
|2 || 2 || 777777
 +
|}
  
==LINQ==
 
  
c# 3.0 got some new features and key words
+
StudentCourses
 +
{| class="wikitable" cellpadding="5" cellspacing="0"
 +
! StudentID !! CourseID !! SemesterID
 +
|-
 +
|1 || 9 || 1
 +
|-
 +
|2 || 9 || 1
 +
|-
 +
|1 || 10 || 1
 +
|}
  
'''var'''
+
StudentsCourses_vw
:is a implicity typed local variable. it is strong typed you don't need to declare the type when you declare the variable.
+
<sql>
 +
SELECT dbo.Student.PeopleID, dbo.Student.OasisID, dbo.StudentCourses.SemesterID, dbo.Course.CourseName,
 +
  dbo.Course.CourseNumber, dbo.Student.StudentID,
 +
  dbo.Course.CourseID
 +
FROM dbo.StudentCourses INNER JOIN
 +
  dbo.Student ON dbo.StudentCourses.StudentID = dbo.Student.StudentID INNER JOIN
 +
  dbo.Course ON dbo.StudentCourses.CourseID = dbo.Course.CourseID
 +
</sql>
  
''Anonymous Types'''
+
[[Image:StudentsCourses_vw.png]]
:allows creation of structural types without declaring an name first http://msdn.microsoft.com/en-us/library/bb397696.aspx
 
  
'''Lambda Expressions''' =>
+
<sql>
: similar to anonymous methods. don't need to be declared first and don't need to specify return type http://msdn.microsoft.com/en-us/library/bb397687.aspx
+
SELECT CourseID, CourseName, CourseNumber
 +
FROM Course
 +
WHERE
 +
(
 +
  CourseID not in
 +
  (
 +
      SELECT CourseID FROM StudentsCourses_vw WHERE StudentID = @StudentID
 +
  )
 +
)
 +
</sql>
  
new keywords http://msdn.microsoft.com/en-us/library/bb310804.aspx
+
This query has two parts
  
http://iam.colum.edu/dd/classsource/LINQBlog/LINQTest.aspx
+
SELECT CourseID FROM StudentsCourses_vw WHERE StudentID = 1
  
http://msdn.microsoft.com/en-us/library/bb397933.aspx
+
returns
 +
{|
 +
!CourseID
 +
|-
 +
|9
 +
|-
 +
|10
 +
|}
 +
 
 +
then
 +
 
 +
<sql>
 +
SELECT CourseID, CourseName, CourseNumber
 +
FROM Course
 +
WHERE
 +
(
 +
  CourseID NOT IN
 +
  (
 +
      SELECT CourseID FROM StudentsCourses_vw WHERE StudentID = 1
 +
  )
 +
)
 +
</sql>
 +
 
 +
returns
 +
{|
 +
! CourseID !! CousreName !!CourseNumber
 +
|-
 +
|11 || Data Design || 36-2601
 +
|-
 +
|168 || Application Design || 36-4601
 +
|-
 +
|169 || OOP || 36-2001
 +
|}
 +
 
 +
Course adder example
 +
 
 +
http://iam.colum.edu/dd/classsource/class8/student.aspx
 +
 
 +
SQL Subquery
 +
 
 +
=Web services=
 +
 
 +
 
 +
===Page Methods as services===
 +
ServicePage.aspx -source
 +
ServicePage.aspx.cs
 +
 
 +
===AJAX ala Services===
 +
http://asp.net/AJAX/Documentation/Live/tutoria/UsingUpdatePanelUserControls.aspx
 +
 
 +
http://asp.net/ajax/documentation/live/tutorials/ConsumingWebServicesWithAJAXTutorial.aspx broken example on their website
 +
 
 +
fixed on our site
 +
 
 +
CallWebServiceMethods.aspx - source
 +
CallWebServiceMethods.js - source
 +
 
 +
 
 +
 
 +
 
 +
 
 +
 
 +
{{HigherNormalForms}}
 +
 
 +
 
 +
{{ASPX Upload Files}}
 +
 
 +
 
 +
 
 +
{{ASPX Email}}
 +
 
 +
==Mail==
 +
<syntaxhighlight lang="csharp">
 +
string strTo = "jmeyers3@iit.edu";
 +
        string strFrom = "jmeyers3@iit.edu";
 +
        string strSubject = "Hi jeff";
 +
        string strBody = "A real nice body text here";
 +
 +
       
 +
        //Send email
 +
        //SmtpMail.SmtpServer = "mail2.iit.edu";
 +
        //SmtpMail.Send(strFrom, strTo, strSubject, strBody);
 +
 +
        SmtpClient client = new SmtpClient("localhost", 25);
 +
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
 +
        client.Send(strFrom, strTo, strSubject, strBody);
 +
</syntaxhighlight>
 +
 
 +
in web config
 +
<xml>
 +
<system.net>
 +
<mailSettings>
 +
<smtp deliveryMethod="Network" from="jeff@iam.colum.edu">
 +
<network host="localhost" port="25" defaultCredentials="true"/>
 +
</smtp>
 +
</mailSettings>
 +
</system.net>
 +
</xml>
 +
 
 +
<!--
 +
==HomeWork==
 +
 
 +
Final Proposal
 +
 
 +
Final Website proposal. Due 11/29.
 +
Final Proposal
 +
 
 +
Write a short requirements document (website) that explains
 +
 
 +
# Rationale: for website design or introduction of product features
 +
# Audience: Description of target audience and segments for new website areas
 +
# Requirements Summary: Key Areas and Features elaborate on requirements above
 +
# Design Documents: Site Map (hand drawn, word doc, xml visio, paper napkin) and Data Diagram (sql 2008 diagram screen shot)
 +
 
 +
Final Requirements
 +
 
 +
The final website should show off all of your new .NET skills. I'll leave the idea and the content of the website up to you. Good proposals might be... An anonymous cheese rating website (similar) to midterm but using SQL server as the persistence layer. It would allow ratings for cheese from varios regions then show the most popular cheese by region.
 +
 
 +
* Use Web Forms for user interaction
 +
* Use State (sessions, application, viewstate)
 +
* Use Database (I can help you design your db if you can't do this) with either SQLDataSource Direct ADO or LINQDataSource
 +
 
 +
[http://brookfield.rice.iit.edu/jmeyers/463/classsource/CheeseProp.aspx Sample Cheese Poposal]
 +
-->
 +
 
 +
==Final==
 +
 
 +
Create a page the is protected by .NET authentication
 +
 
 +
 
 +
[[Data Design Final 2011]]
 +
 
 +
[[Data Design Final 2012]]

Latest revision as of 03:23, 9 February 2016


Transactions in ADO

Transactions in ADO.NET http://msdn.microsoft.com/en-us/library/777e5ebh.aspx

SqlTransaction tran = null;        //Declare Transaction Object
 
        try
        {
            conn.Open();
            tran = conn.BeginTransaction(); //init trans object
            command.Transaction = tran;     //use tran object on all commands and adapters
            command = new SqlCommand(strSQL2, conn, tran);
            tran.Commit();      //commmit

        }
        catch (Exception eTran)
        {
            tran.Rollback();
        }
        finally
        { 
            conn.Close();  
        }

and now the ADO way

trans.aspx - source

Transactions in LINQ

Don't use DTS this a much bigger...

DTS transactions http://msdn.microsoft.com/en-us/library/bb387002.aspx#Y0

Use DbTransaction http://msdn.microsoft.com/en-us/library/system.data.common.dbtransaction.aspx

DbTransaction trans = db.Connection.BeginTransaction();

LINQTransaction.aspx - source LINQTransaction.aspx.cs

Sql Aggregate Functions

They perform a calculation on a set to values and return a single value. Aggregate functions are determinate. They will return the same value given the same dataset.

  • AVG
  • MIN
  • CHECKSUM
  • SUM
  • CHECKSUM_AGG
  • STDEV
  • COUNT
  • STDEVP
  • COUNT_BIG
  • VAR
  • GROUPING
  • VARP
  • MAX

<sql> SELECT MAX(PricePerPound) as MaxPrice from CheesePrice

SELECT AVG(PricePerPound) as AvgPrice from CheesePrice

SELECT COUNT(CheeseID) as Count from CheesePrice </sql>

Sub Query

A query may be a part of another query. This allows

IN vs EXSISTS

Remember the Many to Many relation ship in the Student Registration System

ManyToManyStudentCourses.png

Here a student has many course and a course has many student. Imagine in the registration system the student will be presented with a list of classes that they can add. That list of classes should not include the courses that they are already taking. Course

CourseID CourseName CourseNumber
9 Science 66-0002
10 Math 66-0001
11 Data Design 36-2601
168 Application Design 36-4601
169 OOP 36-2001

Student

StudentID PeopleID OasisID
1 1 666666
2 2 777777


StudentCourses

StudentID CourseID SemesterID
1 9 1
2 9 1
1 10 1

StudentsCourses_vw <sql> SELECT dbo.Student.PeopleID, dbo.Student.OasisID, dbo.StudentCourses.SemesterID, dbo.Course.CourseName,

 dbo.Course.CourseNumber, dbo.Student.StudentID, 
 dbo.Course.CourseID

FROM dbo.StudentCourses INNER JOIN

 dbo.Student ON dbo.StudentCourses.StudentID = dbo.Student.StudentID INNER JOIN
 dbo.Course ON dbo.StudentCourses.CourseID = dbo.Course.CourseID

</sql>

StudentsCourses vw.png

<sql> SELECT CourseID, CourseName, CourseNumber FROM Course WHERE (

  CourseID not in 
  (
     SELECT CourseID FROM StudentsCourses_vw WHERE StudentID = @StudentID
  )

) </sql>

This query has two parts

SELECT CourseID FROM StudentsCourses_vw WHERE StudentID = 1

returns

CourseID
9
10

then

<sql> SELECT CourseID, CourseName, CourseNumber FROM Course WHERE (

  CourseID NOT IN 
  (
     SELECT CourseID FROM StudentsCourses_vw WHERE StudentID = 1
  )

) </sql>

returns

CourseID CousreName CourseNumber
11 Data Design 36-2601
168 Application Design 36-4601
169 OOP 36-2001

Course adder example

http://iam.colum.edu/dd/classsource/class8/student.aspx

SQL Subquery

Web services

Page Methods as services

ServicePage.aspx -source ServicePage.aspx.cs

AJAX ala Services

http://asp.net/AJAX/Documentation/Live/tutoria/UsingUpdatePanelUserControls.aspx

http://asp.net/ajax/documentation/live/tutorials/ConsumingWebServicesWithAJAXTutorial.aspx broken example on their website

fixed on our site

CallWebServiceMethods.aspx - source CallWebServiceMethods.js - source




Higher Normal Forms

once a db is in 3NF you can go further

Boyce-Codd Normal form

  • needs to be in 3NF

http://en.wikipedia.org/wiki/Boyce-Codd_normal_form

Fourth Normal Form

  • needs to be in BCNF

Fourth Normal Form

separates independent multivalued facts stored in one table into separate tables.

http://en.wikipedia.org/wiki/Fourth_normal_form

Fifth Normal Form

  • needs to be in 4NF

Fifth Normal Form

breaks out data redundancy that is not covered by any of the previous normal forms.

http://en.wikipedia.org/wiki/Fifth_normal_form

More normal forms

http://en.wikipedia.org/wiki/Database_normalization


Mappath

Server.Mappath will return the real physical location of a file that is in a virtual directory.

http://msdn.microsoft.com/en-us/library/ms524632%28v=vs.90%29.aspx


Special permissions need to be set on the folder that you are going to upload to. The anonymous ASP.NET account need to have write permissions to this folder. It is not recommended that folder with write permissions also have execute and should not allow directory listing.

Upload a File

http up-loader

Add executionTimeout and maxRequestLength to the web.config so that large uploads will not fail.

<xml> <configuration>

   <system.web>
       <httpRuntime
           executionTimeout="1200"
           maxRequestLength="65536"
       />
   </system.web>

</configuration> </xml>

multipart form <csharp> <form enctype="multipart/form-data" runat="server"> <tr>

 <td>Select file to upload:</td>
 <td>
 <input id="myfile" type="file" runat="server"></td>

</tr> <tr>

 <td>
 <input type=button id="btnUploadTheFile" value="Upload" 
       OnServerClick="btnUploadTheFile_Click" runat="server" />
 </td>

</tr> </form> </csharp>

Parse the multipart form and save the file msdn library. System.Web.HtmlInputFile.PostedFile Property

<csharp> void btnUploadTheFile_Click(object Source, EventArgs evArgs) {

   //Path to save file
   string strBaseLocation = "";
   
   if (null != myfile.PostedFile) 
   {
       // Get the HTTP posted file instance (to simplify the code) 
       HttpPostedFile postedfile = myfile.PostedFile; 
       
       // Get the filename 
       string filename = new System.IO.FileInfo(postedfile.FileName).Name; 
       string filesize = postedfile.ContentLength.ToString();
       try 
       {
           myfile.PostedFile.SaveAs(strBaseLocation+filename);
           txtOutput.InnerHtml = "File " + strBaseLocation + filename + " uploaded successfully";
       }
       catch (Exception e) 
       {
               txtOutput.InnerHtml = "Error saving " + strBaseLocation + filename + "

"+ e.ToString();

       }
       
       //Do some other stuff 
       //maybe  like send an email to let admin know file was uploaded
   }

} </csharp>

http://iam.colum.edu/oop/classsource/class14/up.aspx [up.aspx - source] [web.config - source]

Modifications

Unique file name

<csharp> // Get the filename

       string filename = new System.IO.FileInfo(postedfile.FileName).Name; 
       string strUniqueDate = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day.ToString() + System.DateTime.Now.Ticks.ToString()  ;
       filename =  strUniqueDate + "_" + filename;

</csharp>

After you upload w file it os often wise to add the file name to a database(especially if you obfuscate the name you might want to save the original name). You may also may want to send an email notifying you that a file has been uploaded.


Template:ASPX Email

Mail

string strTo = "jmeyers3@iit.edu";
        string strFrom = "jmeyers3@iit.edu";
        string strSubject = "Hi jeff";
        string strBody = "A real nice body text here";
 
        
        //Send email
        //SmtpMail.SmtpServer = "mail2.iit.edu";
        //SmtpMail.Send(strFrom, strTo, strSubject, strBody);
 
        SmtpClient client = new SmtpClient("localhost", 25);
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.Send(strFrom, strTo, strSubject, strBody);

in web config <xml> <system.net> <mailSettings> <smtp deliveryMethod="Network" from="jeff@iam.colum.edu"> <network host="localhost" port="25" defaultCredentials="true"/> </smtp> </mailSettings> </system.net> </xml>


Final

Create a page the is protected by .NET authentication


Data Design Final 2011

Data Design Final 2012