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! | David Barnes, Lead Evangelist for IBM Emerging Internet Technologies 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!
| | | | Download a free trial version of IBM DB2 9.5 for Linux, UNIX, and Windows. DB2 9 is the result of a five-year development project that transformed traditional (static) database technology into an interactive data server that merges the high performance and ease of use of DB2 with the self-describing benefits of XML. FREE! Go There Now!
| | | | This tutorial shows new users of IBM WebSphere Business Monitor Version 6.0.2 how to perform the "Hello World" equivalent for monitoring business process applications. It is intended to help you get familiar with the capabilities of the product. FREE! Go There Now!
| | | | As systems increase in complexity, communication between systems and software teams becomes more and more difficult. Now, there’s a way to improve product quality and communication.<br />Read the “Model Driven Systems Development” white paper to see how. Also included in this kit are more educational white papers, customer examples, tutorials, informative Webcasts, and best practices for designing, building and managing systems.<br /> 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!
| | | | Join this Rational Talks to You teleconference on November 29 at 1:00 pm ET to participate in an interactive discusssion with Grady Booch around architecture and reuse. Get your questions answered! FREE! Go There Now!
| | | | Join this Rational Talks to You teleconference on December 6 at 1:00 pm ET to participate in an agile application development discussion and get your questions answered on using IBM Rational Method Composer in a distributed environment.Get your questions answered! FREE! Go There Now!
| | | | This whitepaper provides areas to consider when evaluating any software configuration management solution. It addresses how the IBM solutions (Rational ClearCase and Rational ClearQuest) meet the needs and requirements of both project leaders and developers to provide successful Software Change and Configuration Management. FREE! Go There Now!
| | | | Visit IBM developerWorks to try the IBM SOA Sandbox for process. The SOA Sandbox for process focuses on providing a trial environment with the necessary tooling and components required to gain a better understanding of business processes and how to best improve existing business processes to derive value quickly. FREE! Go There Now!
| | | | The discipline of assembling and delivering software is maturing beyond standard developer-centric compile/test software builds. The end-to-end software development lifecycle is emerging as the new focus moves “Beyond the Build.” Join this on demand webcast to learn about methods for streamlining software delivery and key capabilities of the IBM Rational Build Forge framework for automating build and release management in environments of any size. FREE! Go There Now!
| | | | All FREE IBM® developerWorks Tools! | |