A Very Simple but usefull ASP.NET Opinion poll example. Create a folder called polls in your "c:/Inetpub/wwwroot" and to test the polls open your web browser and type "http://localhost/polls/Default.aspx". Put all the files listed below in the folder listed above. [bold]Default.aspx[/bold] <!%@ import Namespace="Sys ... J.Ramkrshna Murty - [ASP.NET Opinion Polls - Ver1.0]Default.aspx
<!--asp@ Page Language="vb" Explicit="true" Debug="true" --> <!--asp@ Import Namespace="System.Text" --> <!--asp@ import Namespace="System.Data" --> <!--asp@ import Namespace="System.Data.OleDb" --> <script language="VB" runat="server"> Sub Page_Change(sender As Object, e As DataGridPageChangedEventArgs) MyDataGrid.CurrentPageIndex = e.NewPageIndex BindData() End Sub Sub Page_Load(Source As Object, E As EventArgs) If Not Page.IsPostBack Then BindData() End If If Page.IsPostBack Then End If End Sub Sub BindData() '1. Create a connection Dim objConn objConn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("polls.mdb") & ";") objConn.Open '2. Create a command object for the query Const strSQL as String = "select * from polls where currentquestion=0" Dim objCmd as New OleDbCommand(strSQL, objConn) '3. Create/Populate the DataReader Dim objDR as OleDbDataReader objDR = objCmd.ExecuteReader() MyDataGrid.DataSource = objDR MyDataGrid.DataBind() End Sub </script></p> <datagrid id="MyDataGrid" width="50%" runat="server" cellspacing="0" cellpadding="0" gridlines="Both" borderwidth="0" headerstyle-backcolor="white" headerstyle-font-name="Arial" headerstyle-font-bold="True" headerstyle-font-size="14" backcolor="white" font-name="Arial" font-size="11" alternatingitemstyle-backcolor="white" alternatingitemstyle-font-name="Arial" alternatingitemstyle-font-size="11" bordercolor="white" allowpaging="false" allowcustompaging="false" pagesize="8" pagerstyle-mode="NumericPages" pagerstyle-horizontalalign="Center" pagerstyle-pagebuttoncount="10" onpageindexchanged="Page_Change" autogeneratecolumns="False" /> <columns /> <templatecolumn /> <itemtemplate /> <div runat="server"> <form id="sample1" action="results.aspx" method="get"> <table cellspacing="1" cellpadding="4" bgcolor="#ffffff" border="2"> <tbody> <tr> <td bordercolor="#ff9900" width="384" bgcolor="#ff9900"> <div align="center"><strong><font face="Verdana, Arial, Helvetica, sans-serif" color="#ffffff" size="2">ASP.NET - Opinion Polls - Ver1.0</font> </strong></div></td></tr> <tr> <td> <table cellspacing="0" cellpadding="5" width="100%" border="0"> <tbody> <tr> <td colspan="2"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"> <!--asp# DataBinder.Eval(Container.DataItem, "question") --> </font></td></tr> <tr> <td width="11%"> <input id="pollid" type="hidden" value="<!--asp# DataBinder.Eval(Container.DataItem, " name="pollid" />"> <input type="radio" value="Choice1,<!--asp# DataBinder.Eval(Container.DataItem, " name="choice" />"></td> <td width="89%"> <font face="Verdana, Arial, Helvetica, sans-serif" size="2"> <!--asp# DataBinder.Eval(Container.DataItem, "choice1") --></font> </td></tr> <tr> <td> <input type="radio" value="Choice2,<!--asp# DataBinder.Eval(Container.DataItem, " name="choice" />"></td> <td><font face="Verdana, Arial, Helvetica, sans-serif" size="2"> <!--asp# DataBinder.Eval(Container.DataItem, "choice2") --> </font></td></tr> <tr> <td> <input type="radio" value="Choice3,<!--asp# DataBinder.Eval(Container.DataItem, " name="choice" />"></td> <td><font face="Verdana, Arial, Helvetica, sans-serif" size="2"> <!--asp# DataBinder.Eval(Container.DataItem, "choice3") --> </font></td></tr> <tr> <td> <input type="radio" value="Choice4,<!--asp# DataBinder.Eval(Container.DataItem, " name="choice" />"></td> <td><font face="Verdana, Arial, Helvetica, sans-serif" size="2"> <!--asp# DataBinder.Eval(Container.DataItem, "choice4") --></font> <input id="submit" type="submit" value="Vote" /> </td></tr></tbody></table></td></tr></tbody></table>
results.aspx <!--asp@ Page Language="vb" Explicit="true" Debug="true"--> <!--asp@ Import Namespace="System.Text" --> <!--asp@ import Namespace="System.Data" --> <!--asp@ import Namespace="System.Data.OleDb" --> <script language="VB" runat="server">
Sub Page_Change(sender As Object, e As DataGridPageChangedEventArgs) BindData() End Sub
Sub Page_Load(Source As Object, E As EventArgs) If Not Page.IsPostBack Then BindData() End If End Sub
Sub BindData() '1. Create a connection Dim objConn objConn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("polls.mdb") & ";")
'You must open the db connection before populating the DataReader
objConn.Open() Dim strSQL as String strSQL="select * from polls where currentquestion="& Request.querystring("pollid") Dim objCmd as New OleDbCommand(strSQL, objConn)
'3. Create/Populate the DataReader
Dim objDR as OleDbDataReader objDR = objCmd.ExecuteReader() MyDataGrid.DataSource = objDR MyDataGrid.DataBind()
'4.Updating Polls '5. Create a New connection
Dim objConn1 objConn1=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("polls.mdb") & ";")
Dim votevalue Dim ChoiceValue Dim VotedFor ChoiceValue=(left(request.QueryString("choice"),7)) if choiceValue="Choice1" then VotedFor="Votes1" else if choiceValue="Choice2" then VotedFor="Votes2" else if choiceValue="Choice3" then VotedFor="Votes3" else if choiceValue="Choice4" then VotedFor="Votes4" end if
votevalue=(Cint(right(request.QueryString("choice"), (len(request.QueryString("choice"))-8)))+1)
'2. Create the command object, passing in the SQL string
Dim strSQL1 as String = "update polls set "& VotedFor &"=" & votevalue+1 & " where currentquestion=" & Request.querystring("pollid") & "" Dim myCommand as New OleDbCommand(strSQL1, objConn1) objConn1.Open() myCommand.ExecuteReader(CommandBehavior.CloseConnection)
End Sub </script></div> <datagrid id="MyDataGrid" width="50%" runat="server" cellspacing="0" cellpadding="0" gridlines="Both" borderwidth="0" headerstyle-backcolor="white" headerstyle-font-name="Arial" headerstyle-font-bold="True" headerstyle-font-size="14" backcolor="white" font-name="Arial" font-size="11" alternatingitemstyle-backcolor="white" alternatingitemstyle-font-name="Arial" alternatingitemstyle-font-size="11" bordercolor="white" allowpaging="false" allowcustompaging="false" pagesize="8" pagerstyle-mode="NumericPages" pagerstyle-horizontalalign="Center" pagerstyle-pagebuttoncount="10" onpageindexchanged="Page_Change" autogeneratecolumns="False" /> <columns /> <templatecolumn /> <itemtemplate /> <div runat="server"> <table cellspacing="1" cellpadding="4" bgcolor="#ffffff" border="2"> <tbody> <tr> <td bordercolor="#ff9900" width="384" bgcolor="#ff9900"> <div align="center"><strong> <font face="Verdana, Arial, Helvetica, sans-serif" color="#ffffff" size="2">ASP.NET - Opinion Polls - Ver1.0</font></strong></div> </td></tr> <tr> <td> <table cellspacing="0" cellpadding="5" width="100%" border="0"> <tbody> <tr> <td width="72%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><!--asp# DataBinder.Eval(Container.DataItem, "choice1")--> </font></td> <td width="28%"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"> <!--asp#Cint(DataBinder.Eval(Container.DataItem, "votes1")/ (DataBinder.Eval(Container.DataItem, "votes1")+ DataBinder.Eval(Container.DataItem, "votes2")+ DataBinder.Eval(Container.DataItem, "votes3")+ DataBinder.Eval(Container.DataItem, "votes4"))*100)-->% </font></td></tr> <tr> <td><font face="Verdana, Arial, Helvetica, sans-serif" size="2"> <!--asp# DataBinder.Eval(Container.DataItem, "choice2")--> </font></td> <td><font face="Verdana, Arial, Helvetica, sans-serif" size="2"> <!--asp#Cint(DataBinder.Eval(Container.DataItem, "votes2")/ (DataBinder.Eval(Container.DataItem, "votes1")+ DataBinder.Eval(Container.DataItem, "votes2")+ DataBinder.Eval(Container.DataItem, "votes3")+ DataBinder.Eval(Container.DataItem, "votes4"))*100)-->% </font></td></tr> <tr> <td><font face="Verdana, Arial, Helvetica, sans-serif" size="2"> <!--asp# DataBinder.Eval(Container.DataItem, "choice3")--></font></td> <td><font face="Verdana, Arial, Helvetica, sans-serif" size="2"> <!--asp# Cint(DataBinder.Eval(Container.DataItem, "votes3")/ (DataBinder.Eval(Container.DataItem, "votes1")+ DataBinder.Eval(Container.DataItem, "votes2")+ DataBinder.Eval(Container.DataItem, "votes3")+ DataBinder.Eval(Container.DataItem, "votes4"))*100)-->% </font></td></tr> <tr> <td><font face="Verdana, Arial, Helvetica, sans-serif" size="2"> <!--asp# DataBinder.Eval(Container.DataItem, "choice4")--></font></td> <td><font face="Verdana, Arial, Helvetica, sans-serif" size="2"> <!--asp# Cint(DataBinder.Eval(Container.DataItem, "votes4")/ (DataBinder.Eval(Container.DataItem, "votes1")+ DataBinder.Eval(Container.DataItem, "votes2")+ DataBinder.Eval(Container.DataItem, "votes3")+ DataBinder.Eval(Container.DataItem, "votes4"))*100)-->% </font></td></tr></tbody></table> </td></tr></tbody></table></div> <div runat="server"> </itemtemplate /> </templatecolumn /> </columns /> </datagrid />
polls.mdb
id --------------------------------
Number pollsessionid ---------------------
Number question --------------------
Text polldate -------------------
Text choice1 -------------------
Text choice2 -------------------
Text choice3 --------------------
Text choice4 -------------------
Text votes1---------------------
(Number) votes2 ---------------------
(Number) votes3 ---------------------
(Number) votes4 ---------------------
(Number) currentquestion --------------------- (Yes/No)
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More ASP.NET Code Articles More By J.Ramkrishna Murty developerWorks - FREE Tools! | Hold your calendar on January 30, 2008 for this free webcast on the new i5/OS. Rational's Enterprise Modernization products will be discussed at this webcast as they help to drive the application development environment for this new System i OS. <br />And learn how i5/OS will take you to the next step of efficient, resilient business processing. You will hear about the new i5/OS capabilities as it will be the most significant i5/OS release in years. If you cannot join the webcast on 1/30/08 you can still use this link to listen to the replay.<br /> FREE! Go There Now!
| | | | You'll get answers to many questions and more from David Barnes, Lead Evangelist for IBM Emerging Internet Technologies. David will discuss aspects of Web 2.0 that bring value to corporations, academia, and government. He'll also discuss IBM's vision around Web 2.0, including the importance of remixability and consumability. The discussion will culminate with examples of various IBM Software Group solutions you can use to get ahead of the Web 2.0 adoption curve. FREE! Go There Now!
| | | | Join us for this web seminar to learn how you can defend your web applications from attack. Learn about the 3 most common web application attacks, including how they occur and what can be done to prevent them. We’ll also discuss manual versus automated approaches for scanning and identifying web application vulnerabilities and how IBM Rational AppScan, an automated vulnerability scanner, can help you automate more of what you are doing manually today. FREE! Go There Now!
| | | | Join this webcast to see how IBM Data Studio Developer and pureQuery can take the pain out of Java data access. uApplications developed using both Java and SQL have become a common requirement. Database connectivity using Java Database Connectivity (JDBC) to create an application is a multi-step tedious process, and tooling that covers both SQL and Java has been unavailable, until now. IBM Data Studio introduces the pureQuery platform: a high-performance, Java data access platform focused on simplifying the tasks of developing, managing, and optimizing database applications and services. FREE! Go There Now!
| | | | Secure your Web applications with IBM Rational AppScan Standard Edition V7.7, previously known as Watchfire AppScan. This Web application security testing tool automates vulnerability assessments and scans and tests for common Web application vulnerabilities. Visit IBM developerWorks to download a free trial of IBM Rational AppScan Standard Edition V7.7. FREE! Go There Now!
| | | | Learn how Rational Build Forge can extend a simple compile and package build process by adding customization and deployment capability. Go from a manual method to automating: checking for code changes; getting the latest source; compiling and packaging; customizing; copying to and restarting a deployment server; and sending e-mail notification that a new version is available. FREE! Go There Now!
| | | | This Fall, IBM Rational talks to you directly through a special teleconference series giving you access to the best minds in IBM Rational - product experts and market thought leaders who will answer your questions during these pre-scheduled telephone conference calls. Register today! FREE! Go There Now!
| | | | Learn how to do more with your reusable assets with the free Rational Asset Manager eKit. The eKit includes demos on how Rational Asset Manager tracks and audits your assets in order to utilize them for reuse. Plus you’ll find white papers and a Webcast that discuss the challenges of a Service Oriented Architecture and how Rational Asset Manager can provide quick and effective solutions. FREE! Go There Now!
| | | | Rational Modeling Extension for Microsoft .NET enhances usability for code generation supporting a more intelligent refactoring. The latest enhancements enable organizations with Java and .NET systems and software development maintain architectural integrity across heterogeneous platforms. FREE! Go There Now!
| | | | In this webcast, you'll get an introduction to the eXtreme Transaction Processing (XTP) features of WebSphere Extended Deployment and the common architectural traits required by XTP applications. See how WebSphere Extended Deployment's ObjectGrid feature provides a state-of-the-art infrastructure for hosting XTP applications. FREE! Go There Now!
| | | | All FREE IBM® developerWorks Tools! | |