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! | <a href="http://zeus.developershed.com/shonuff.php?blackbird=3853&zoneid=442&source=&dest=http%3A%2F%2Fwww.ibm.com%2Fdeveloperworks%2Fspaces%2Fjazz%3FS_TACT%3D105AGY31%26S_CMP%3DDEVSHED&ismap="><img src="http://images.devshed.com/corp/img/news/jazz01.gif" alt="developerWorks Jazz space" align="left"></a>You've heard the buzz about Jazz... want to know more about it from a developer's perspective? Check out the Jazz space on developerWorks. This space is an up-to-date resource for developers, including technical information about Jazz and products built on Jazz, like Rational Team Concert Express. The Jazz space includes content from a wide variety of sources, including links, feeds, and comments from experts. 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!
| | | | Visit IBM developerWorks to try the IBM SOA Sandbox for connectivity. The SOA Sandbox for connectivity provides a trial environment with the tooling and components to help you explore how to effectively connect your infrastructure and integrate all of the people, processes and information in your company. Use the hosted sandbox to explore SOA techniques that streamline connecting existing IT assets together, as well as learn how to connect them to new business logic. FREE! Go There Now!
| | | | As organizations integrate software into every aspect of business, they are constantly pressured to deliver faster, better, and cheaper results. Unfortunately, a “dis-integrated” software delivery approach reduces returns while increasing costs. This IBM Rational White Paper shows how Integrated Requirements Management aligns organizations around maximizing value and keeping pace with change. FREE! Go There Now!
| | | | Attend this launch webcast with Scott Hebner, Vice President of IBM Rational Marketing and Strategy, for an overview of Rational’s new software offerings and resources to help modernize and accelerate software innovation on i on Power Systems – while ensuring past application investments are protected and continue to grow. Learn how these solutions are helping customers extend their core i5/OS solutions toward modern architectures such as SOA and web technologies to deliver business improvements that stand the test of time. FREE! Go There Now!
| | | | Visit IBM developerWorks to download a free trial version of WebSphere Extended Deployment Compute Grid, which lets you schedule, execute, and monitor batch jobs. Because online transaction processing and batch jobs execute simultaneously on the same server resources, you can avoid costly duplication of resources. Compute Grid supports job types of Java transactional batch, compute-intensive and a new type called "native execution", which enables non-Java workloads to run on distributed end points. FREE! Go There Now!
| | | | Attend this launch webcast with Scott Hebner, Vice President of IBM Rational Marketing and Strategy, where he will overview Rational’s new offerings and programs to help customers accelerate software innovation on System z. He will discuss how these solutions help organizations extend their core business processes toward modern architectures such as SOA and web technologies to deliver business improvements that stand the test of time. FREE! Go There Now!
| | | | Visit IBM developerWorks to download a free trial of the latest release of IBM Lotus Sametime Standard V8.0. Lotus Sametime Standard V8.0 is a platform for unified communications and collaboration that combines security features with an extensible, open solution including integrated Voice over IP, geographic location awareness, mobile clients, and a robust Business Partner community offering telephony and video integration. FREE! Go There Now!
| | | | Listen to this webcast to get an overview of Info 2.0 and a technical demo of how to quickly build an enterprise mashup. IBM's Info 2.0 technology leverages emerging Web 2.0 technologies such as mashups, feeds, AJAX, and JSON in order to simplify assembly of information using feeds and services. Come learn about the technical elements of Info 2.0 including the Feed Generation framework, Mashup Engine, and mashup assembly components. Learn how to pull information from databases, departmental information, and the Web to create mashups critical to your company’s success. We will also discuss best practices to help you get started. FREE! Go There Now!
| | | | Get a free trial download of IBM Lotus Forms V3.0 (formerly Workplace Forms), which provides a zero-footprint eForms solution to help you automate and move forms-based business processes off the desktop and onto the Web. With Lotus Forms, you can extend applications beyond the firewall by creating a single electronic form document ready for use in both thick and Web 2.0 thin client format. FREE! Go There Now!
| | | | All FREE IBM® developerWorks Tools! | |