@{ if(IsPost) }cis.csuohio.edu/.../CIS611_Lab1_Guide_ASPNET_Setup.pdf · 3. Create the Stored...

6
Guide for Part 1 of Lab Assignment 1 on ASP.NET with SQL Server CIS611 Object: Automatic Creation and Maintenance of Database from Web Interface Part 1 1. Create a company webpage where a new employee can add his/her information into the Employee table by taking a new employee information with 9 Input Boxes for each column Fname, MInit, Lname, …, Dno as in Employee table schema With ADD button. 2. Write an Web Application Server that takes the new employee’s input from the webpage and call a Stored Procedure SP_Insert_NewEmployee with the 9 input parameters. I used the ASP.NET Web Pages platform to create a Single Page Application, combining the HTML input code with the ASP.NET server output code into one file called Default.cshtml. When Default.cshtml is accessed through a “get” request, the current Employee table and a blank input form are displayed to the user. Once the user enters their information into the form and clicks the “ADD” button, the Default.cshtml page is accessed again, but this time through a “post” request. Razor code is used to detect this access method ( @{ if(IsPost) } ) and use the text entered into the form to call a stored procedure in the Company database with the form values as parameters. The Company database connection is defined in the web.config file: <connectionStrings> <add name="CompanyDBConnection" connectionString= "Data Source=DESKTOP-NBIUEU3;Initial Catalog=COMPANY;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings> opened in the Razor code: @{ var db = Database.Open("CompanyDBConnection"); } and used to execute two queries in the Razor code: //Calls the stored procedure SP_Insert_NewEmployee that is defined in SQL Server Company DB @{ db.Execute("EXEC SP_Insert_NewEmployee @0, @1, @2, @3, @4, @5, @6, @7, @8, @9", fname, minit, lname, ssn, bdate, address, sex, salary, superssn, dno); } //Displays the contents of the Employee table in the Company DB @{ var selectQueryString = "SELECT * FROM EMPLOYEE"; db.Query(selectQueryString); } The Employee table is displayed in both the “get” and “post” accesses to show the original list and the new list after clicking “ADD”. ASP.NET Web Pages Code (Default.cshtml): @{ var db = Database.Open("CompanyDBConnection"); var selectQueryString = "SELECT * FROM EMPLOYEE";

Transcript of @{ if(IsPost) }cis.csuohio.edu/.../CIS611_Lab1_Guide_ASPNET_Setup.pdf · 3. Create the Stored...

Page 1: @{ if(IsPost) }cis.csuohio.edu/.../CIS611_Lab1_Guide_ASPNET_Setup.pdf · 3. Create the Stored Procedure SP_Insert_NewEmployee in your Database Server that performs: 3-1. Takes 9 inputs

Guide for Part 1 of Lab Assignment 1 on ASP.NET with SQL Server

CIS611

Object: Automatic Creation and Maintenance of Database from Web Interface

Part 1

1. Create a company webpage where a new employee can add his/her information into the Employee

table by taking a new employee information with 9 Input Boxes for each column Fname, MInit, Lname,

…, Dno as in Employee table schema With ADD button.

2. Write an Web Application Server that takes the new employee’s input from the webpage and call a

Stored Procedure SP_Insert_NewEmployee with the 9 input parameters.

I used the ASP.NET Web Pages platform to create a Single Page Application, combining the HTML input

code with the ASP.NET server output code into one file called Default.cshtml. When Default.cshtml is

accessed through a “get” request, the current Employee table and a blank input form are displayed to

the user. Once the user enters their information into the form and clicks the “ADD” button, the

Default.cshtml page is accessed again, but this time through a “post” request. Razor code is used to

detect this access method ( @{ if(IsPost) } ) and use the text entered into the form to call a stored

procedure in the Company database with the form values as parameters. The Company database

connection is defined in the web.config file: <connectionStrings>

<add name="CompanyDBConnection" connectionString= "Data Source=DESKTOP-NBIUEU3;Initial

Catalog=COMPANY;Integrated Security=True"

providerName="System.Data.SqlClient" /> </connectionStrings>

opened in the Razor code: @{ var db = Database.Open("CompanyDBConnection"); }

and used to execute two queries in the Razor code:

//Calls the stored procedure SP_Insert_NewEmployee that is defined in SQL Server Company DB @{

db.Execute("EXEC SP_Insert_NewEmployee @0, @1, @2, @3, @4, @5, @6, @7, @8, @9", fname, minit, lname, ssn, bdate, address, sex, salary, superssn, dno);

}

//Displays the contents of the Employee table in the Company DB @{

var selectQueryString = "SELECT * FROM EMPLOYEE"; db.Query(selectQueryString); }

The Employee table is displayed in both the “get” and “post” accesses to show the original list and the

new list after clicking “ADD”.

ASP.NET Web Pages Code (Default.cshtml): @{ var db = Database.Open("CompanyDBConnection");

var selectQueryString = "SELECT * FROM EMPLOYEE";

Page 2: @{ if(IsPost) }cis.csuohio.edu/.../CIS611_Lab1_Guide_ASPNET_Setup.pdf · 3. Create the Stored Procedure SP_Insert_NewEmployee in your Database Server that performs: 3-1. Takes 9 inputs

}

<!DOCTYPE html> <html>

<body>

@{ if (IsPost)

{

string fname = Request["Fname"];

string minit = Request["Minit"]; string lname = Request["Lname"];

string ssn = Request["SSN"];

string bdate = Request["Bdate"]; string address = Request["Address"];

string sex = Request["Sex"];

string salary = Request["Salary"]; string superssn = Request["SuperSSN"];

string dno = Request["Dno"];

db.Execute("EXEC SP_Insert_NewEmployee @0, @1, @2, @3, @4, @5, @6, @7, @8, @9", fname, minit, lname, ssn, bdate, address, sex, salary, superssn, dno);

}

<p> <h2>Employee List</h2>

<table>

...

</body>

</html>

Web.config File: <?xml version="1.0" encoding="utf-8"?>

<configuration>

<system.web> <compilation debug="true" targetFramework="4.6.1"/>

<httpRuntime targetFramework="4.6.1"/>

</system.web>

<connectionStrings>

<add name="CompanyDBConnection"

connectionString= "Data Source=DESKTOP-NBIUEU3;Initial Catalog=COMPANY;Integrated Security=True" providerName="System.Data.SqlClient" />

</connectionStrings>

<runtime>

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">

<dependentAssembly>

<assemblyIdentity name="DotNetOpenAuth.Core" publicKeyToken="2780ccd10d57b246"/> <bindingRedirect oldVersion="1.0.0.0-4.1.0.0" newVersion="4.1.0.0"/>

</dependentAssembly>

<dependentAssembly> <assemblyIdentity name="DotNetOpenAuth.AspNet"

publicKeyToken="2780ccd10d57b246"/>

<bindingRedirect oldVersion="1.0.0.0-4.1.0.0" newVersion="4.1.0.0"/> </dependentAssembly>

Page 3: @{ if(IsPost) }cis.csuohio.edu/.../CIS611_Lab1_Guide_ASPNET_Setup.pdf · 3. Create the Stored Procedure SP_Insert_NewEmployee in your Database Server that performs: 3-1. Takes 9 inputs

<dependentAssembly>

<assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35"/>

<bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0"/>

</dependentAssembly> <dependentAssembly>

<assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35"/>

<bindingRedirect oldVersion="1.0.0.0-1.5.2.14234.0.0"

newVersion="1.5.2.14234.0.0"/> </dependentAssembly>

<dependentAssembly>

<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35"/> <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>

</dependentAssembly>

<dependentAssembly> <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35"/>

<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>

</dependentAssembly>

</assemblyBinding> </runtime>

<system.data>

<DbProviderFactories> <remove invariant="System.Data.SqlServerCe.4.0"/>

<add name="Microsoft SQL Server Compact Data Provider 4.0"

invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server

Compact"

type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral,

PublicKeyToken=89845dcd8080cc91"/>

</DbProviderFactories>

</system.data> <system.codedom>

<compilers>

<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider,

Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.7.0, Culture=neutral,

PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701"/>

<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"

type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider,

Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

warningLevel="4" compilerOptions="/langversion:default /nowarn:41008

/define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"/> </compilers>

</system.codedom>

</configuration>

Entering a new Employee into the form (before ADD is clicked):

Page 4: @{ if(IsPost) }cis.csuohio.edu/.../CIS611_Lab1_Guide_ASPNET_Setup.pdf · 3. Create the Stored Procedure SP_Insert_NewEmployee in your Database Server that performs: 3-1. Takes 9 inputs

After ADD is clicked, the new Employee is shown in the table, and a new blank form is generated:

Verifying the new employee is in SQL Server EMPLOYEE table and added to WORKS_ON table with

appropriate projects:

Page 5: @{ if(IsPost) }cis.csuohio.edu/.../CIS611_Lab1_Guide_ASPNET_Setup.pdf · 3. Create the Stored Procedure SP_Insert_NewEmployee in your Database Server that performs: 3-1. Takes 9 inputs

3. Create the Stored Procedure SP_Insert_NewEmployee in your Database Server that performs:

3-1. Takes 9 inputs to Insert a new employee tuple into the Employee table

3-2. Find all the projects that are controlled by the department that the new employee works

for.

3-3. Add all the project numbers with the new employee into Works_On table.

USE COMPANY

GO

CREATE PROCEDURE SP_Insert_NewEmployee (@Fname nvarchar(10), @Minit char(1),

@Lname varchar(20), @SSN char(9), @BDate date, @Address varchar(50), @Sex char(1), @Salary decimal (10,2), @SuperSSN char(9), @Dno smallint)

AS

BEGIN

--Insert new employee into EMPLOYEE table

...

--Find all projects controlled by the department assigned to new employee

...

--Assign employee to each of these projects, starting with 0 hours worked ...

END

Page 6: @{ if(IsPost) }cis.csuohio.edu/.../CIS611_Lab1_Guide_ASPNET_Setup.pdf · 3. Create the Stored Procedure SP_Insert_NewEmployee in your Database Server that performs: 3-1. Takes 9 inputs