ASP Code
  Home arrow ASP Code arrow ASP.NET Image poll generator using a datab...
Click Here
Iron Speed
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  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
VeriSign Whitepapers 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
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 / 131
    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

    AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th -1:00PM EST. Register Today!

    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!


    IBM DB2 Deep Compression ROI Tool

    The IBM DB2 Deep Compression ROI tool is designed for DBA’s and IT management personnel to perform a clinical analysis of the cost savings gained from the Storage Optimization feature of DB2 9 for Linux, UNIX and Windows. The feature, also known as Deep Compression, compresses data that lies within a database by up to 80% at times.
    FREE! Go There Now!


    NEW! Driving Business Success with Rational Process Library

    Join this webcast, to learn how the Rational Process Library can help with compliance issues, drive process improvement, and assist in service-oriented architecture (SOA) or Agile development. We will take a peek into the Rational Process Library with content around software and systems engineering (including RUP), operations and systems management, program and portfolio management, and asset and SOA governance.
    FREE! Go There Now!


    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! Develop Systems Software Assets with IBM Rational Asset Manager

    Join us for this on demand webcast to learn about developing complex systems more quickly and efficiently. We'll cover market drivers for developing, governing and reusing systems software assets and how you can develop system software assets with Rational Asset Manager.
    FREE! Go There Now!


    NEW! Download a free trial of WebSphere Business Modeler Advanced V6.1.1

    Visit IBM developerWorks to download a free trial version of WebSphere Business Modeler Advanced V6.1.1, IBM’s premier business process modeling and analysis tool for business users that offers process modeling, simulation, and analysis capabilities. IBM WebSphere Business Modeler helps you visualize, understand, and document business processes for continuous improvement.
    FREE! Go There Now!


    NEW! Hello World: Learn how to install and use the Rational Asset Manager Eclipse client

    In this tutorial, you can learn how to install and configure the IBM Rational Asset Manager Eclipse client, explore the different views in the Asset Management perspective, learn various search techniques, work with existing assets, and submit a new asset.
    FREE! Go There Now!


    NEW! Rational Talks to You: Manage RUP-based CMMI initiatives

    Join this Rational Talks to You teleconference on December 4 at 1:00 pm ET to discuss how Rational Method Composer can help meet your compliance objectives. Get your questions answered!
    FREE! Go There Now!


    NEW! Trial download: IBM Rational Tester for SOA Quality V7.0.1

    Get a free trial download of the latest version of IBM Rational Tester for SOA Quality V7.0.1, a functional and regression testing tool that enables the creation, comprehension, modification and execution of testing GUI-less Web services.
    FREE! Go There Now!


    NEW! Using Rational Business Developer to enhance your developer productivity

    Join this Rational Talks to You teleconference, to hear how Enterprise Generation Language (EGL) eliminates the need for tedious and error-prone low level coding, so developers can focus on business requirements. EGL extends the Rational software development platform with a simplified programming language that enables developers who have little or no experience with Java, Web technologies or Service Oriented Architecture, to create enterprise-class applications and services quickly and easily. It also allows developers who may have little or no mainframe programming experience to quickly create traditional mainframe components.
    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!



    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-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway