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 / 147
    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! A Layered approach to delivering security-rich Web applications

    As businesses grow increasingly dependent upon Web applications to provide services to customers, employees and partners, these complex applications become more difficult to secure. Although traditional security solutions protect Internet infrastructure layers, they do not guard against HTTP and HTML attacks. Many organizations that conduct security testing still deploy applications that allow attackers to manipulate their logic and wreak havoc on their business. To mitigate this risk, development and delivery teams must address Web application security throughout the lifecycle, addressing the many layers detailed in this paper.
    FREE! Go There Now!


    NEW! Improve your build process with IBM Rational Build Forge, Part 2: Automate builds for a real-world Tomcat project

    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!


    NEW! Improve your build process with IBM Rational Build Forge, Part 1: Create a continuous build and integration environment

    Learn how to implement a build management system that uses and extends your existing automation technologies. This tutorial shows, step-by-step, how to install and configure IBM Rational Build Forge to manage builds for Jakarta Tomcat from source code.
    FREE! Go There Now!


    NEW! Understanding Web application security challenges

    As businesses grow increasingly dependent upon Web applications, these complex entities grow more difficult to secure. Most companies equip their Web sites with firewalls, Secure Sockets Layer (SSL), and network and host security, but the majority of attacks are on applications themselves – and these technologies cannot prevent them. This paper explains what you can do to help protect your organization, and it discusses an approach for improving your organization’s Web application security.
    FREE! Go There Now!


    NEW! Trial download: IBM Informix Dynamic Server Express Edition V11.0

    Informix Dynamic Server (IDS) Express Edition offers outstanding online transaction processing (OLTP) database performance, while helping to simplify and automate many of the tasks associated with deploying databases for small business applications. IDS 11 further extends the ease of management and applications integration with the Admin API and Scheduler, high availability with Continuous Log Restore for backup server recovery in case of a primary server failure, and column level encryption to protect personal and company private data.
    FREE! Go There Now!


    Role of Integrated Requirements Management in Software Delivery

    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!


    NEW! Hello World: WebSphere Service Registry and Repository

    Manage, govern, and share services across your organization by using WebSphere Service Registry and Repository. Follow the hands-on exercises to learn how to navigate the Web interface to publish, find, reuse, and update services.
    FREE! Go There Now!


    NEW! Evaluate IBM Rational Software Analyzer V7.0

    Download a free trial version of IBM Rational Software Analyzer Developer Edition V7.0 to identify bug defects earlier in the software development cycle. Rational Software Analyzer is an extensible software development solution that reduces the expense of bug-fixes by enabling static analysis code reviews and bug identification very early in the development cycle.
    FREE! Go There Now!


    Build Forge Express demo: Enabling software delivery excellence for small and midsized businesses

    This demonstration gives you an overview of IBM® Rational® Build Forge Express Edition, a global offering that provides a framework to automate and execute software processes. Rational Build Forge provides a software assembly line that can support all of your tools, technologies, and platforms so you can achieve a repeatable, reliable, and traceable build and release process.
    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!



    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...
    - Various methods of setting Date values to a ...
    - Conditional DataGrid Item and using checkbox...
    - Fill .NET Listbox with SQL DataReader
    - Filling Dropdown box using Code-Behinds in C#
    - FLAMES code sample written in .NET What is F...





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