ASP Code
  Home arrow ASP Code arrow ASP.NET Image poll generator using a datab...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP CODE

ASP.NET Image poll generator using a database and Code-behind
By: aspfree
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 151
    2001-08-04

    Table of Contents:

    Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    ASP.NET Image poll generator using a database & Code-behind


    Here is the code:
    <

    This article shows how to take data out of a database(Our web poll in thiscase) and generator gif''s on the fly. Thanks a million for Dave for contributing this user control.

    Page 1. -- This calls the Code behind page (ImgPollResults2.aspx)

    <%@ Page Language="vb" Src="ImgPollResults2.aspx.vb" Inherits="ImgPollResults2"%>

    Page 2. -Code behind page

    This is the code-behind page that calls the stored procedure to retrieve thedata. Couple of things that will have to be altered before this will work foryou is the stored proc or SQL string you pass and the connection string.

    Imports System.Drawing
    Imports System.Drawing.Drawing2D
    Imports System.Drawing.Imaging
    Imports System.IO
    Imports System.Data
    Imports System.Data.SqlClient
    Imports System.Collections
    Imports System
    Imports System.Configuration

    Public Class ImgPollResults2
    Inherits System.Web.UI.Page

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e AsSystem.EventArgs)
    ''Put user code to initialize the page here
    Dim PollId As String = Request.QueryString("PID")

    If Not (PollId Is Nothing) Then
    GetDataBars(PollId)
    End If


    End Sub


    Private Sub GetDataBars(ByVal PollId As String)

    Dim AnswerCount As New ArrayList()
    Dim AnswerLabel As New ArrayList()
    Dim PollTitle As String
    Dim dr As SqlDataReader
    ''Change the stored proc data and the querystring
    Dim sqlText As String = "exec s_PollResults @PollId=" & PollId
    dr = GetDataReader(sqlText)

    While dr.Read()
    PollTitle = dr.Item("Question").ToString()
    AnswerCount.Add(dr.Item("AnswerCount").ToString())
    AnswerLabel.Add(dr.Item("Answer").ToString())
    End While


    If AnswerCount.Count > 0 Then
    DrawBarGraph(PollTitle, AnswerLabel, AnswerCount, Response.OutputStream)
    End If

    End Sub

    Private Function GetDataReader(ByVal sqlText As String) As SqlDataReader

    Dim dr As SqlDataReader
    Dim sqlConn As SqlConnection = New SqlConnection(GetDBConnString())
    Dim sqlCmd As SqlCommand = New SqlCommand(sqlText, sqlConn)
    sqlCmd.Connection.Open()
    dr = sqlCmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
    Return dr

    End Function


    Private Function GetDBConnString() As String
    Return "server=localhost;uid=sa;pwd=;database=pubs"
    End Function

    Private Sub DrawBarGraph(ByVal strTitle As String, ByVal aX As ArrayList, ByVal aY As ArrayList, ByVal Target As Stream)

    Const iColWidth As Integer = 100
    Const iColSpace As Integer = 25
    Const iMaxHeight As Integer = 100
    Const iHeightSpace As Integer = 25
    Const iXLegendSpace As Integer = 12
    Const iTitleSpace As Integer = 50

    Dim iMaxWidth As Integer = (iColWidth + iColSpace) * aX.Count +iColSpace, iMaxColHeight As Integer = 0, iTotalHeight As Integer =iMaxHeight + iXLegendSpace + iTitleSpace

    Dim objBitmap As Bitmap = New Bitmap(iMaxWidth, iTotalHeight)
    Dim objGraphics As Graphics = Graphics.FromImage(objBitmap)

    objGraphics.FillRectangle(New SolidBrush(Color.White), 0, 0, iMaxWidth, iTotalHeight)
    objGraphics.FillRectangle(New SolidBrush(Color.Ivory), 0, 0, iMaxWidth, iMaxHeight)

    '' find the maximum value
    Dim iValue As Integer
    For Each iValue In aY
    If iValue > iMaxColHeight Then iMaxColHeight = iValue
    Next

    Dim iBarX As Integer = iColSpace, iCurrentHeight As Integer
    Dim objBrush As SolidBrush = New SolidBrush(Color.Purple)
    Dim fontLegend As Font = New Font("Arial", 8)
    Dim fontValues As Font = New Font("Arial", 8)
    Dim fontTitle As Font = New Font("Arial", 12)

    '' loop through and draw each bar
    Dim iLoop As Integer
    For iLoop = 0 To aX.Count - 1
    iCurrentHeight = (Convert.ToDouble(aY(iLoop)) /Convert.ToDouble(iMaxColHeight)) * Convert.ToDouble(iMaxHeight -iHeightSpace)

    objGraphics.FillRectangle(objBrush, iBarX, iMaxHeight - iCurrentHeight, iColWidth, iCurrentHeight)
    objGraphics.DrawString(aX(iLoop), fontLegend, objBrush, iBarX, iMaxHeight)
    objGraphics.DrawString(String.Format("{0:#,###}", aY(iLoop)),fontValues, objBrush, iBarX, iMaxHeight - iCurrentHeight - 15)

    iBarX += (iColSpace + iColWidth)
    Next

    objGraphics.DrawString(strTitle, fontTitle, objBrush, (iMaxWidth / 2) - strTitle.Length * 4, iMaxHeight + iXLegendSpace)

    objBitmap.Save(Target, ImageFormat.Gif)
    objGraphics.Dispose()
    objBitmap.Dispose()

    End Sub


    End Class


    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 Code Articles
    More By aspfree

     

    IBM® developerWorks developerWorks - FREE Tools!


    NEW! Application Development Tools for the Mainframe Developer

    You probably have thousands of lines of COBOL code loaded with business intelligence and being used to run your business, along with an army of developers maintaining these applications. Learn how to prepare your applications and developers so you can keep that competitive edge and move to a service-oriented architecture with the IBM Rational Enterprise Modernization solutions. Replay is available for 9 months.
    FREE! Go There Now!


    NEW! BlammoSplat: Build a community Web site of OpenLaszlo animations, Part 3: The community animation

    Learn to enable users to both rate existing animations and to combine existing animations into new snippets. This is the third in a series of three tutorials that chronicle the building of a site that enables collaborative discussion and animation building using Domino and OpenLaszlo.
    FREE! Go There Now!


    NEW! Download IBM Rational Developer for System z

    Download a free trial version of IBM Rational Developer for System z, software that can help you deliver core development capabilities; the power of Java Platform, Enterprise Edition (Java EE); and rapid application development support to diverse enterprise application development teams. With comprehensive development tools to help create, deploy and maintain traditional enterprise and composite applications, Rational Developer for System z enables developers with different technical backgrounds to easily participate in important technology projects.
    FREE! Go There Now!


    NEW! Run your first CICS application on a PC using TXSeries for Windows

    Learn the basics of the IBM Customer Information Control System (CICS). With a hands-on exercise, learn how to get your first CICS application up and running on your desktop using TXSeries V6.1 for Windows. The tutorial shows you how to download and install a free trial version of TXSeries V6.1.
    FREE! Go There Now!


    NEW! The dirty dozen: preventing common application-level hack attacks

    As organizations have grown increasingly dependent on online software, the risk of malicious attacks has also become far more serious. Fortunately, well-governed organizations can protect their Web applications by injecting vulnerability assessments and ethical hacks into their software development and delivery processes. This paper describes 12 of the most common hacker attacks and provides basic rules that you can follow to help create more hack-resistant Web applications.
    FREE! Go There Now!


    NEW! Trial download: IBM Lotus Forms V3.0

    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!


    NEW! Trial download: IBM Rational Functional Tester V7.0.1

    Get a free trial download of the latest version of IBM Rational Functional Tester V7.0.1. Rational Functional Tester is an automated functional and regression testing solution for QA teams concerned with the quality of their Java, Microsoft Visual Studio .NET, and Web-based applications.
    FREE! Go There Now!


    NEW! Webcast: Calling All Testers! Find Application Vulnerabilities Early in the Development Process Where they are Easier to Fix and Less Risky to your Business

    In this webcast, IBM Rational will discuss the importance of Web application security and will share techniques and best practices to introduce application security testing into current QA processes including: understanding common security vulnerabilities and techniques to integrate security testing with defect tracking and remediation systems in an effort to safeguard sensitive online information.
    FREE! Go There Now!


    NEW! Webcast: Eclipse: Empowering the universal platform

    The Eclipse community is constantly working to extend Eclipse's functionality. In this webcast, learn about some of the most important and feature-rich projects under development. From multi-language support to plug-in development, tune in to see what Eclipse is capable of now.
    FREE! Go There Now!


    NEW! Webcast: WebSphere Process Server

    WebSphere Process Server delivers a unique integration framework that simplifies existing IT resources. Often, as IT assets grow to support business demand, so too does their complexity and manageability. In this webcast, we’ll discuss how WebSphere Process Server helps deliver an SOA infrastructure that provides a common model to orchestrate, mediate, connect, map, and execute the underlying IT functions. Discover how WebSphere Process Server simplifies integration of business processes by leveraging existing IT assets as reusable services without the complexities of traditional integration methodologies.
    FREE! Go There Now!



    All FREE IBM® developerWorks Tools!

    ASP CODE ARTICLES

    - ASP Forms
    - ASP: The Beginning
    - Getting Remote Files With ASP Continued
    - Inbox and Outbox Manipulation in ASP
    - Relational DropDownList Using VB.NET
    - Ad Tracking URL Hits
    - Use ViewState to display one record per page...
    - Send Email using ASP.NET formatted in HTML
    - ASP File Explorer
    - ASP/XML Interview questions by Srivatsan Sri...
    - Pressing RETURN won't submit the form
    - This shows how you get the TEXT of a combo r...
    - Group Data by Adrian Forbes
    - Multiple checkbox select sample
    - Multiple checkbox select with all values sam...





    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 9 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek