using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
/// <summary>
/// Summary description for db
/// </summary>
public class db
{
public static string connectionstring = @”Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True”;
public db()
{
//
// TODO: Add constructor logic here
//
}
//function login for string
public bool login(string user, string pass)
{
//connection to the database
SqlConnection con = new SqlConnection(connectionstring);
//sql query to retreive data
string sql = “select password from student where student_id = @user”;
//sql paramterize to secure the sql injection attack
SqlParameter pl = new SqlParameter(“@user”, user);
//sql command
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.Add(pl);
//open the connection
con.Open();
//reader to excute the sql query of sql command
SqlDataReader rdr = cmd.ExecuteReader();
bool log = false;
//check if the reader can read
if (rdr.Read())
{
//conver the password to md5 hash encryption
string password = GetMD5Hash(pass);
//check if the password equal the pass in database
if (rdr[0].ToString() == password)
{
log = true;
}
}
//close the reader
rdr.Close();
//close the connection
con.Close();
return log;
}
//md5 function ecryption
public string GetMD5Hash(string input)
{
System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] bs = System.Text.Encoding.UTF8.GetBytes(input);
bs = x.ComputeHash(bs);
System.Text.StringBuilder s = new System.Text.StringBuilder();
foreach (byte b in bs)
{
s.Append(b.ToString(“x2″).ToLower());
}
string password = s.ToString();
return password;
}
//admin login function
public bool login_admin(string user, string pass)
{
SqlConnection con = new SqlConnection(connectionstring);
string sql = “select password from Admin where username = @user”;
SqlParameter pl = new SqlParameter(“@user”, user);
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.Add(pl);
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
bool log = false;
if (rdr.Read())
{
string password = GetMD5Hash(pass);
if (rdr[0].ToString() == password)
{
log = true;
}
}
rdr.Close();
con.Close();
return log;
}
//get the vote of each nomine to view it in result
public string get_voting(string id)
{
SqlConnection con = new SqlConnection(connectionstring);
string sql = “select vote from nomeni where Student_ID= ‘”+ id + “‘ “;
SqlCommand cmd = new SqlCommand(sql, con);
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
string vote = “”;
if (rdr.Read())
{
vote = rdr[0].ToString();
}
rdr.Close();
con.Close();
return vote;
}
//get the statiscits data
public string [] set_election_year()
{
SqlConnection con = new SqlConnection(connectionstring);
string sql = “select count(*) , sum(vote) from student”;
SqlCommand cmd = new SqlCommand(sql, con);
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
string vote = “”;
string unvote = “”;
if (rdr.Read())
{
unvote = rdr[0].ToString();
vote = rdr[1].ToString();
}
rdr.Close();
con.Close();
string[] x = new string[2];
x[0] = unvote;
x[1] = vote;
return x;
}
//check if student id already exist in adduser page
public bool check_user_exist(string user)
{
SqlConnection con = new SqlConnection(connectionstring);
string sql = “select student_id from student where student_id = ‘” + user + “‘”;
SqlCommand cmd = new SqlCommand(sql, con);
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
bool log = false;
if (rdr.Read())
{
if (rdr[0].ToString() == user)
{
log = true;
}
}
rdr.Close();
con.Close();
return log;
}
//get nomine data to view it on vote page
public SqlDataReader get_nomine(string user)
{
SqlConnection con = new SqlConnection(connectionstring);
string sql = “select * from nomeni where student_id = ‘” + user + “‘”;
SqlCommand cmd = new SqlCommand(sql, con);
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
bool log = false;
rdr.Read();
return rdr;
}
//get date time data
public SqlDataReader get_year()
{
SqlConnection con = new SqlConnection(connectionstring);
string sql = “select * from timedate”;
SqlCommand cmd = new SqlCommand(sql, con);
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
rdr.Read();
return rdr;
}
//check if student already vote
public string checkvoting(string user)
{
SqlConnection con = new SqlConnection(connectionstring);
string sql = “select vote from student where student_id = ‘” + user + “‘”;
SqlCommand cmd = new SqlCommand(sql, con);
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
string vote = “”;
if (rdr.Read())
{
vote = rdr[0].ToString();
}
rdr.Close();
con.Close();
return vote;
}
public void voting(string std_id)
{
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
/// <summary>
/// Summary description for db
/// </summary>
public class db
{
public static string connectionstring = @”Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True”;
public db()
{
//
Read more »