Master C# Logo banner
Welcome to MasterCSharp.com - Master C#, the easy way... - by Saurabh Nandu

 


Creating a Poll Application - Creating the Admin Pages - Part 1

Add Comment
 

 
<div align="center"> <table width="75%" class="outline"> <tr> <td width="40%" class="outline"><b>Download</b></td> <td width="25%" class="outline"><b>SDK</b></td> <td class="outline"><b>Code Charge Studio</b></td> </tr> <tr> <td width="40%" class="outline"> <a class="wbox" href="../../file/pollapp-part1.zip">pollapp-part1.zip</a> (105kb)</td> <td width="25%" class="outline">v1.0.3705</td> <td class="outline">v1.0.7</td> </tr> </table> </div> <p> <span class=wboxheado>Introduction</span><br> In the <a class="wbox" target="_blank" href="http://www.mastercsharp.com/article.aspx?ArticleID=82&&TopicID=16">last article</a> I gave you a brief overview of CodeCharge Studio (CCS). In this article I will straight dive into the depths of this tool and we will build a Simple Poll Application using this tool. This tutorial will highlight how easily you can build a fairly complex database driven application. I will split this tutorial in to parts so that its easier for you to read ( and me to write ;) ). </p> <p><span class=wboxheado>Application Summary</span><br> Let's first summarize the functionality we are looking out for before we start creating the application. The application will be divided into two logical parts the first part will be the Poll User Control (Includable Page in CCS). This user control can be placed on any other page where you want to display the poll hence you reuse the user control code. <br> The second section contains of the administration section where only the administrator is allowed access. Through the admin section the administrator can easily add, update delete the poll items. The Poll application will be created in such a way that there can be multiple choices per poll. Also a user can only vote once per poll. And at a given time only one poll can be active. </p> <p>In this article I will only cover the creating of the administration section. In the further parts I'll complete the next part.</p> <p><span class=wboxheado>Database Design</span><br> Before you start creating any application in CodeCharge Studio you need to first design the database. In this example I will be using MS Access database, you can actually use any database you like. The descriptions of the 4 tables are given below for our vote.mdb file :</p> <p><b>1) Members</b> - This table is used to keep the username and password of the administrators who will be allowed to access the admin section of the application.</p> <table cellpadding="1" cellspacing="2" width="100%" class="outline"> <tr> <td width="25%" class="outline"><b>Field Name</b></td> <td width="25%" class="outline"><b>Data Type</b></td> <td width="50%" class="outline"><b>Description</b></td> </tr> <tr> <td width="25%" class="outline">member_id</td> <td width="25%" class="outline">AutoNumber</td> <td width="50%" class="outline">Primary key field for the table.</td> </tr> <tr> <td width="25%" class="outline">member_name</td> <td width="25%" class="outline">Text</td> <td width="50%" class="outline">Username of the member that will be used while authentication.</td> </tr> <tr> <td width="25%" class="outline">member_pass</td> <td width="25%" class="outline">Text</td> <td width="50%" class="outline">Password of the member that will be used while authentication.</td> </tr> </table> <p><b>2) Vote_Details</b> - This table stores the details of each poll. Also there it sets the active poll. </p> <table cellpadding="1" cellspacing="2" width="100%" class="outline"> <tr> <td width="25%" class="outline"><b>Field Name</b></td> <td width="25%" class="outline"><b>Data Type</b></td> <td width="50%" class="outline"><b>Description</b></td> </tr> <tr> <td width="25%" class="outline">vote_id</td> <td width="25%" class="outline">AutoNumber</td> <td width="50%" class="outline">Primary key field for the table.</td> </tr> <tr> <td width="25%" class="outline">vote_name</td> <td width="25%" class="outline">Text</td> <td width="50%" class="outline">Title of the poll.</td> </tr> <tr> <td width="25%" class="outline">vote_desc</td> <td width="25%" class="outline">Memo</td> <td width="50%" class="outline">Description of the poll. Usually this field will hold the question for the poll.</td> </tr> <tr> <td width="25%" class="outline">vote_active</td> <td width="25%" class="outline">Yes/No</td> <td width="50%" class="outline">Indicates which poll is active. </td> </tr> </table> <p><b>3) Vote_Items</b> - This table stores the choices related to each poll. As I mentioned before each poll will support multiple choices, hence this table is used to store the choices for the poll.</p> <table cellpadding="1" cellspacing="2" width="100%" class="outline"> <tr> <td width="25%" class="outline"><b>Field Name</b></td> <td width="25%" class="outline"><b>Data Type</b></td> <td width="50%" class="outline"><b>Description</b></td> </tr> <tr> <td width="25%" class="outline">vote_item_id</td> <td width="25%" class="outline">AutoNumber</td> <td width="50%" class="outline">Primary key field for the table.</td> </tr> <tr> <td width="25%" class="outline">vote_id</td> <td width="25%" class="outline">Number</td> <td width="50%" class="outline">The vote_id from the vote_details table to which this item is related.</td> </tr> <tr> <td width="25%" class="outline">vote_item_name</td> <td width="25%" class="outline">Text</td> <td width="50%" class="outline">The description of the choice item.</td> </tr> <tr> <td width="25%" class="outline">vote_results</td> <td width="25%" class="outline">Number</td> <td width="50%" class="outline">The number of times this item has been voted.</td> </tr> </table> <p><b>4) User_Votes</b> - This table stores the details of each users vote. Since we want a user to only vote once per poll we store his IP Address every time he votes. </p> <table cellpadding="1" cellspacing="2" width="100%" class="outline"> <tr> <td width="25%" class="outline"><b>Field Name</b></td> <td width="25%" class="outline"><b>Data Type</b></td> <td width="50%" class="outline"><b>Description</b></td> </tr> <tr> <td width="25%" class="outline">user_id</td> <td width="25%" class="outline">AutoNumber</td> <td width="50%" class="outline">Primary key field for this table.</td> </tr> <tr> <td width="25%" class="outline">vote_item_id</td> <td width="25%" class="outline">Number</td> <td width="50%" class="outline">The vote_item_id from the vote_items table, that the user has selected.</td> </tr> <tr> <td width="25%" class="outline">vote_id</td> <td width="25%" class="outline">Number</td> <td width="50%" class="outline">The vote_id from the vote_details table. </td> </tr> <tr> <td width="25%" class="outline">user_ip</td> <td width="25%" class="outline">Text</td> <td width="50%" class="outline">The IP Address of the user.</td> </tr> <tr> <td width="25%" class="outline">vote_date</td> <td width="25%" class="outline">DateTime</td> <td width="50%" class="outline">The DateTime when the user voted.</td> </tr> </table> <p>This completes the database design, the relationships between the tables is shown in figure 1 below.</p> <p align="center"> <img border="0" src="../../img/simplepoll01.gif" width="365" height="208"><br> <b>Figure 1: </b>Tables Relationships</p> <span class=wboxheado>Code Charge Studio Project</span><p><span class=wboxhead>Creating a New Project</span><br> Once the database is ready its time to start you designing your application in CodeCharge Studio. Start up CCS and you will be shown the Project Dialog (see figure 2). Select <i>New Project</i> and click <i>OK</i> to start with a new CCS project. </p> <p align="center"> <img border="0" src="../../img/simplepoll02.gif" width="381" height="392"><br> Figure 2: Project Dialog</p> <span class=wboxhead>Using the Application Builder</span><br> The Add New Project dialog (figure 3) is displayed now, enter <b>PollApp</b> as the <i>Name</i> of the application and change the <i>Language</i> to <b>ASP.NET 1.0 C#</b>. At this point you have two choices, either you can start a blank project and then manually add each page to your project or you can use the <b>Application Builder</b> which rapidly builds the entire application with just a few clicks. To lessen our work, I choose <i>Application Builder </i> and click <i>OK</i> to start the Application Builder.&nbsp; <p dir="ltr" align="center"> <img border="0" src="../../img/simplepoll03.gif" width="497" height="400"><br> <b>Figure 3:</b> Create new Project<p>In the first <i>General project properties</i> page, don't change any properties and click <i>Next</i> to come to the <i>database setup</i> page. Here click on the <i>Create</i> button to create a new connection to our <i>vote.mdb</i> database. The <i>Add New Connection</i> dialog is shown, set the <i>Connection Name</i> as <b>VoteDB</b> and <i>Database </i>as <i>MS Access</i>. Its very important to set up the database, since Code Charge Studio uses this information to construct SQL queries appropriately for the database. <br> Also in case you want to use another database connection on the production server or you want to use specific Managed Providers like SqlClient Managed Provider or the ODBC Managed Provider, switch to the <i>Server </i>tab in the <i>Add New Connection</i> dialog and setup the properties there.<br> For this example click the <i>Build</i> button and link to your vote.mdb database file. Once done click <i>OK</i> to add a connection to the project.<p align="center"> <img border="0" src="../../img/simplepoll04.gif" width="428" height="451"><br> <b>Figure 4:</b> Add New Connection</p> Once you finish building the database connection click <i>Next</i> to come to the <i>Builder Options</i> screen. Here let the default properties of <i>Enable smart naming</i>, which helps to name the different pages and <i>Autoincremented primary keys</i> be checked. Proceed forward by clicking the <i>Next</i> button, now you are presented with a <i>Access Authentication </i>page. Set the setting the appropriate properties the page looks like below (figure 5). The <i>User Table Field</i> contains the table name in the database which stores the user details, the <i>User ID Field</i> contains the primary key field of the table, the <i>Login Field</i> contains the username field and the <i>Password Field</i> contains the password file from the table. The last <i>Level/Group Field</i> is set when you have Roles defined for your users. The Panel next to is also concerned with Roles. Since in this basic application we do not have any roles we leave them to their default values. <p align="center"> <img border="0" src="../../img/simplepoll05.gif" width="288" height="149"><br> <b>Figure 5:</b> Authentication Settings</p> <p>Once you finish the above settings click <i>Next</i> to move to the next page where you are given a list of tables from the database. Add all the tables to the <i> Selected tables</i> collection. This page helps you select the tables for which you want the builder to auto-generate the pages. Since in my case, I want to create a page to administer all the tables I select all of them. Click <i>Next</i> to arrive to the pages configuration step. </p> <p align="center"> <img border="0" src="../../img/simplepoll06.gif" width="552" height="152"><br> <b>Figure 6:</b> Page Configuration </p> <p>This step is a very important step, and it gives you one place access to configure all your pages. Play around with this step as much as you can to better configure your pages. I will be outlining very few customization options, you will have to find others yourself ;).<br> First click on the <i>All </i>link since we want to update the properties for all the pages. Select both properties on this dialog (figure 7), the <i>Search, Grid &amp; Record on One Page</i> option allows easy configuration and viewing of all the relevant options on one page. Remember the Grid form is used to list the contents of a table, the search form is used to search the table and show relevant records in the Grid form and the Record form is used to insert, update or delete a entry. <br> To collate all the forms on one page is good when you know the amount of data is less. The second option of <i>Include in Menu</i> allows the pages to be listed in a Menu , which is added on all pages for easy navigation. Click <i>OK</i> to close the dialog.</p> <p align="center"> <img border="0" src="../../img/simplepoll07.gif" width="215" height="90"><br> <b>Figure 7:</b> All Page details</p> <p>Now, we will start configuring individual pages. Set the CheckBoxes to as shown below (figure 8). Basically I have removed <i>Search</i> option for all pages, since the data will be less. Also I have removed the <i>Record</i> and <i>Updatable</i> checkboxes for the <i>user_votes</i> page, since usually we don't rig poll's, do we ?? :)<br> Hence you will just be able to browse the list of users polling, but not directly add or update their details. <p align="center"> <img border="0" src="../../img/simplepoll08.gif" width="552" height="152"><br> <b>Figure 8:</b> Updated page properties</p> Click the <i>members</i> link now to configure the properties for the members page. Switch directly to the <i>Records</i> tab and select the <i>member_id</i> field. Change the <i>Control Type</i> to <i>Hidden</i> (as shown in figure 9). We take this step since we do not want the admin to change the <i>member_id</i> field since its a primary key field. <br> Repeat the same steps for the <i>vote_id</i> field on the <i> vote_details</i> page and the <i>vote_item_id</i> on the <i>vote_items</i> field to convert them into <i>Hidden</i> controls on the <i>Record</i> form. <p align="center"> <img border="0" src="../../img/simplepoll09.gif" width="284" height="278"><br> <b>Figure 9: </b>members Record form properties</p>Once you finish configuring the pages, click <i>Next</i> to come to the <i>Select Page Layout</i> section. Keep the default options and select <i>Next</i> to move to the <i>Theme</i> selection step. In this step you can select any of the Theme you want to apply throughout your application. I have selected <i>InLine</i> Theme (figure 10), you can select any theme you like and click Next. The final page in the Application Builder is shown, it summarizes the steps to be performed. This would be your last chance to go back and change any settings you might have left. If everything has been correctly setup click on <i>Finish</i> to generate the project. <p align="center"> <img border="0" src="../../img/simplepoll10.gif" width="598" height="374"><br> <b>Figure 10:</b> Select Theme</p> Once the Application Builder completes CodeCharge Studio will automatically generate 7 pages for you depending upon the properties you have selected. 2 are User Controls used for the Header and Footer respectively while 1 handles the login, rest 4 pages are specific to your tables. You can view these pages from the <i>Project Explorer</i> window. I am sure you might have not seen a faster way of creating customized ASP.NET data-bound pages which can be created so easily! I mean if you switch to the Code View for each page, you will find that so much of code as been written for you, without you actually writing a single line of code by your hand!!<p><span class=wboxhead>Publishing the Project</span><br> Now that you have come so far, you would be definitely waiting to check out how the code works, well we no further delay we publish our project so that the pages are uploaded to the server, and the code is compiled. Press <i>F9</i> or select <i>Project menu -&gt; Publish Project</i> to bring up the <i>Project Settings</i> dialog. If you have a local server use the default options, else select <b>FTP</b> from the <i> Location</i> dropdownlist and provide the appropriate credentials for your server. Click <i>OK</i> to compile and publish the project. If the project publishing directory does not exist on the local server you are prompted to create it. </p> <p><span class=wboxhead>Creating a Virtual Directory</span><br> All ASP.NET applications live within a <i>Virtual Directory</i>, so just publishing to a new directory is not enough, you need to convert this directory into a Virtual Directory. Use the <i>IIS Manager</i> from <i>Administrative Tools</i> to create a new Virtual Directory out of your publishing directory. You can also refer to the 2nd point of below linked article to learn to create a Virtual Directory. <br> [ <a class="wbox" target="_blank" href="http://www.mastercsharp.com/article.aspx?ArticleID=60&&TopicID=2"> www.mastercsharp.com/article.aspx?ArticleID=60&amp;&amp;TopicID=2</a> ]<br> Please do not skip this step or your application will give errors!!</p> <p><span class=wboxhead>Testing the pages</span><br> Once you have published your project, and created a Virtual Directory you are ready to test you pages. Navigate to the URL <i> http://localhost/PollApp/members_list.aspx</i> in your browser. Also add a new member with the <i>Name</i> <b>admin</b> and <i>Pass</i> <b> admin</b>. Some of you might be slightly worried at this point that the password textbox shows the password in clear text, well Code Charge Studio has no way of knowing from the database schema that you require a password textbox. So you can open the <i>members_list</i> page in CCS and change the <i>Format-&gt;type</i> property of the textbox to <i>password</i>.<br> There will be room for more such small adjustments, most of them can be fixed by adjusting the appropriate properties in CCS.</p> <p><span class=wboxhead>Live Page</span><br> Once you have published the project, you can shift to using <i>Live Page</i> tab for each page. This helps to have a quick look at the actual page that you have coded. When you shift to <i>Live Page</i>, CCS generates the current page and the necessary common files and compiles them. In order for the <i>Live Page</i> to show faster CCS does not generate all the pages, hence if you navigate (redirect) to another page from <i>Live Page</i> it will give an error!!</p> <p><span class=wboxhead>Adding a Index Page</span><br> Let's add a simple page that will contain a link to all the Admin pages, so its easier to navigate to them. Choose <i>New -&gt; Page</i> from the <i>File menu</i>, in the <i>New Page</i> dialog choose <i>Blank Page</i> and set its name as <b>admin</b> and click <i>OK</i>. <br> Open the admin page in CCS Design view, and from the Toolbox window switch to the HTML controls. Select the Add HyperLink control and add the links to all the Admin pages (figure 11). Once you add the links to all pages, save the project.</p> <p align="center"> <img border="0" src="../../img/simplepoll11.gif" width="562" height="276"><br> <b>Figure 11: </b>Add Hyperlinks</p> <span class=wboxhead>Restricting Access to Pages</span><br> You might have noticed that even though the login page has been generated, you can freely access all the pages. But the admin pages should be restricted right ?? Well you have to manually set a property of the Pages in CCS you want only only authorized users to access. Select the <b>members_list</b> page in the <i>Project Explorer</i> and then from the <i>Properties</i> window set the <i>Restricted</i> property to <b>Yes</b>. Similarly set the other 4 pages <i>admin</i>, <i>vote_detai_list</i>, <i>vote_items_list</i> and <i>user_votes_list</i> <i>Restricted</i> property to <b>Yes</b>. Once done, save and publish your files. Now when you test your applications you will have to first enter your credentials ( admin/admin - remember we created this earlier) only then you will be able to access the pages. <br> CodeCharge Studio relies on Session based security, so once authenticated the user remains logged into the system for 20 minutes after which the session timeout occurs (default ASP.NET setting). You can although increase this timeout lime in the <i>web.config</i> file. <p align="center"> <img border="0" src="../../img/simplepoll12.gif" width="151" height="140"> <br> <b>Figure 12:</b> Restricted Property</p> <span class=wboxheado>Conclusion</span><br> In this part of the article we learnt how to create a quick admin section using CodeCharge Studio for our Poll application. You might have also noticed that we have accomplished all this in very little time and with virtually Zero lines of code. Also since all the code is auto-generated by CodeCharge Studio you don't have to sit and debug the application, it just works!!<br> Hopefully my rants in the previous article do seem true to you, we'll have lot to see further going. As an exercise go through the pages in CodeCharge Studio and rename all the column headers appropriately and arrange record forms appropriately, remember in most cases all you need to do is select the item in <i>Design Editor</i> and change its properties from the <i>Properties explorer</i>.

Comments

Add Comment