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

Contributed by
Rating: 4 stars4 stars4 stars4 stars4 stars / 177
August 04, 2001
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

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

blog comments powered by Disqus
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...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials