Hello and welcome
Guys,
Tonight I am
going to share you very much interesting topic (one of my friend) asked about
how to create graph in C#. There are so many way to do it but I gave him very
simple and generic solution to create graph. Look below it’s very simple and
awesome.
Step 1: Create below Tables by running the
scripts:
SET ANSI_NULLS
ON
GO
SET
QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[pollMaster](
[pollID] [int] IDENTITY(1,1) NOT NULL,
[poll] [nchar](100) COLLATE
SQL_Latin1_General_CP1_CI_AS NULL,
[active] [bit] NULL CONSTRAINT [DF_pollMaster_active] DEFAULT (1),
CONSTRAINT
[PK_pollMaster] PRIMARY KEY CLUSTERED
(
[pollID] ASC
) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_NULLS
ON
GO
SET
QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[pollOption](
[pollOptionID] [int] IDENTITY(1,1) NOT NULL,
[pollOption] [nchar](100) COLLATE
SQL_Latin1_General_CP1_CI_AS NULL,
[pollId] [int] NULL,
[active] [bit] NULL CONSTRAINT [DF_pollOption_active] DEFAULT (1),
CONSTRAINT
[PK_pollOption] PRIMARY KEY CLUSTERED
(
[pollOptionID] ASC
) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_NULLS
ON
GO
SET
QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[pollAnswer](
[pollAnswerID] [int] IDENTITY(1,1) NOT NULL,
[pollOptionID] [int] NULL,
[pollID] [int] NULL,
[active] [bit] NULL CONSTRAINT [DF_pollAnswer_active] DEFAULT (1),
CONSTRAINT
[PK_pollAnswer] PRIMARY KEY CLUSTERED
(
[pollAnswerID] ASC
) ON [PRIMARY]
) ON [PRIMARY]
Here is Data
after adding into tables it looks like:
SELECT * FROM pollMaster
Result:
1
|
What do you think about festival Dussehra?
|
1
|
2
|
Who will be IPL next Chairman?
|
1
|
SELECT * FROM pollOption
Result:
1
|
Dussehra is a Holy Festival of Maa Durga
|
1
|
1
|
2
|
It's Festival of Victory on bad thing.
|
1
|
1
|
3
|
The Lord Rama had glorious victory on Ravan.
|
1
|
1
|
4
|
Everyone should go with gud faith.
|
1
|
1
|
5
|
Saurabh Ganguly
|
2
|
1
|
6
|
Sachin Tendulkar
|
2
|
1
|
7
|
M. S. Dhoni
|
2
|
1
|
SELECT * FROM pollAnswer
Result:
1
|
1
|
1
|
1
|
2
|
2
|
1
|
1
|
3
|
1
|
1
|
1
|
4
|
1
|
1
|
1
|
5
|
2
|
1
|
1
|
6
|
3
|
1
|
1
|
7
|
3
|
1
|
1
|
8
|
3
|
1
|
1
|
9
|
4
|
1
|
1
|
10
|
4
|
1
|
1
|
Step 2: Create .aspx page like below:
<%@ Page Language="C#"
AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Poll
Result</title>
</head>
<body>
<form id="form1" runat="server">
<div id="divPoll" runat="server"></div>
</form>
</body>
</html>
Step 2: Create .aspx.cs page like below:
using System;
using
System.Configuration;
using
System.Data;
using
System.Linq;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.HtmlControls;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Xml.Linq;
using
System.Data;
using
System.Data.SqlClient;
using
System.Text;
public partial class _Default : System.Web.UI.Page
{
SqlConnection
dbConn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connString"].ToString());
protected void Page_Load(object
sender, EventArgs e)
{
if
(!IsPostBack)
{
GetPollResult();
}
}
protected void GetPollResult()
{
string
strTotalPollQry = "SELECT count(pollOptionID)
as TotalPollCount FROM pollAnswer WHERE pollID = 2";
string
strPollQry = "SELECT PO.pollOption, PM.Poll,
(SELECT COUNT(PA.pollOptionID)FROM pollAnswer PA WHERE PA.pollOptionID =
PO.pollOptionID) as VoteCount " +
" FROM
pollMaster PM INNER JOIN pollOption PO ON PM.pollID = PO.pollID AND PM.PollID =
2";
SqlCommand
cmd = new SqlCommand(strTotalPollQry);
cmd.Connection = dbConn;
dbConn.Open();
SqlDataAdapter
da = new SqlDataAdapter(cmd);
DataTable
dt = new DataTable();
da.Fill(dt);
float
TotalCount = Convert.ToInt32(dt.Rows[0]["TotalPollCount"]);
dbConn.Close();
SqlCommand
cmdPoll = new SqlCommand(strPollQry);
cmdPoll.Connection = dbConn;
dbConn.Open();
SqlDataReader
drPoll = cmdPoll.ExecuteReader();
StringBuilder
strbPollRes = new StringBuilder();
strbPollRes.Append(""
);
int
pollCnt = 0;
while
(drPoll.Read())
{
float
PercentVote = ((Convert.ToInt32(drPoll["VoteCount"]) / TotalCount) * 100);
if
(pollCnt == 0)
{
strbPollRes.Append("
"
+ drPoll["poll"] + "
}
strbPollRes.Append("
"
+ drPoll["pollOption"] + "
");
strbPollRes.Append(""
);
strbPollRes.Append("
"
+ Math.Round(PercentVote,
1, MidpointRounding.ToEven) + "%
");
strbPollRes.Append("
");
strbPollRes.Append("
");
pollCnt++;
}
strbPollRes.Append("
");
dbConn.Close();
divPoll.InnerHtml =
strbPollRes.ToString();
}
}
I hope it
will help you. Let me know if you have any queries.
Cheers,
Ved Pathak
Comments