Design(View Part)
<%@ Page Language=”C#” Title=”Untitled Page” CodeFile=”~/createaccount.aspx.cs” Inherits=”createaccount” %>
<script type=”text/javascript”>
function comparetext()
{ if((document.getElementById(txt_newpwd).contains)==(document.getElementById(txt_cnfpwd).contains))
alert(“hi”);
else
alert(“sorry”);
}</script>
<div id=”container”>
<div class=”header”>
<div class=”logo”>
<h1>Create Account</h1>
</div>
</div>
</div>
<div>
<asp:Panel ID=”p_Panel1″ runat=”server” Height=”590px” Style=”left: 0px; position: relative;
top: 0px;” Width=”674px” BorderColor=”Silver” BorderStyle=”Solid” BorderWidth=”1px”>
<div Style=”left: 0px; position: absolute; top: 0px; width: 336px; height: 582px;”>
<asp:GridView ID=”GridView_account” runat=”server” AutoGenerateColumns=”false” ShowFooter=”True”
OnRowCancelingEdit=”GridView_account_RowCancelingEdit” OnRowEditing=”GridView_account_RowEditing”
OnRowUpdating=”GridView_account_RowUpdating” OnRowCommand=”GridView_account_RowCommand”
OnRowDeleting=”GridView_account_RowDeleting” OnSelectedIndexChanged=”GridView_account_SelectedIndexChanged” DataKeyNames=”CID” Height=”110px” Width=”497px” >
<Columns>
<asp:TemplateField HeaderText=”ID” SortExpression=”departmentid”>
<ItemTemplate>
<asp:Label ID=”label_accid” runat=”server” Text=’<%# Bind(“CID”) %>’></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=”User Name (required)” SortExpression=”CID”>
<EditItemTemplate>
<asp:TextBox ID=”txt_name” runat=”server” Text=’<%# Bind(“NAME”) %>’></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID=”txt_newname” runat=”server” ></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID=”label_name” runat=”server” Text=’<%# Bind(“NAME”) %>’></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=”Password (required)”>
<EditItemTemplate>
<asp:TextBox ID=”txt_password” runat=”server” Text=’<%# Bind(“PASSWORD”) %>’ TextMode=”Password”></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID=”txt_newpassword” runat=”server” ></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID=”label_pwd” runat=”server” Text=”*****”></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=”Email Address (required)” SortExpression=”CID”>
<EditItemTemplate>
<asp:TextBox ID=”txt_email” runat=”server” Text=’<%# Bind(“EMAIL”) %>’></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID=”txt_newemail” runat=”server” ></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID=”label_email” runat=”server” Text=’<%# Bind(“EMAIL”) %>’></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=”Options” ShowHeader=”False”>
<EditItemTemplate>
<asp:LinkButton ID=”LinkButton_update” runat=”server” CausesValidation=”True” CommandName=”Update” Text=”Update”></asp:LinkButton>
<asp:LinkButton ID=”LinkButton_cancel” runat=”server” CausesValidation=”False” CommandName=”Cancel” Text=”Cancel”></asp:LinkButton>
<asp:LinkButton ID=”LinkButton_delete” runat=”server” CausesValidation=”False” CommandName=”Delete” Text=”Delete”></asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID=”LinkButton_addnew” runat=”server” CausesValidation=”False” CommandName=”AddNew” Text=”Add New”></asp:LinkButton>
</FooterTemplate>
<ItemTemplate>
<asp:LinkButton ID=”LinkButton_edit” runat=”server” CausesValidation=”False” CommandName=”Edit” Text=”Edit”></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText=”Manage Role” ShowHeader=”False”>
<ItemTemplate>
<asp:LinkButton ID=”Selectbtn” runat=”server” CausesValidation=”False” CommandName=”Select” Text=”Select”></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID=”Button1″ runat=”server” OnClick=”Button1_Click” Text=”Button” />
<div Style=”left: 263px; position: absolute; top: -30px; width: 185px; height: 20px;” >
<asp:RadioButton ID=”adminradio” runat=”server” GroupName=”usertype” Text=”Admin”/>
<asp:RadioButton ID=”empradio” runat=”server” GroupName=”usertype” Text=”Employee” />
<asp:RadioButton ID=”genradio” runat=”server” GroupName=”usertype” Text=”General” Width=”54px” Checked=”True” />
</div>
</div>
</asp:Panel>
</div>
Code Behind
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.Odbc;
using System.Security.Cryptography;
using System.Text;
public partial class createaccount : System.Web.UI.Page
{
OdbcConnection con;
OdbcCommand cmd;
OdbcDataReader myreader;
ConnectionClass objCon;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
fillgridview();
}
}//
private void fillgridview()
{
objCon = new ConnectionClass();
con = new OdbcConnection();
con = objCon.Connect_thru_config();
con.Open();
string str = “select *from FK_viwUser”;
//string strrole = “select name from FK_TBLROLE”;
OdbcCommand cmd = new OdbcCommand(str, con);
OdbcDataAdapter da = new OdbcDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
GridView_account.DataSource = ds;
GridView_account.DataBind();
con.Close();
}//end of fillgridview
public enum ValidationCode
{
LoginFailed = 1,
LoginSucceeded = 2,
ConnectionFailed = 3,
UnspecifiedFailure = 4,
LoginCreated = 5
}//end of ValidationCode
protected void GridView_account_SelectedIndexChanged(object sender, EventArgs e)
{
string selectedID = ((Label)GridView_account.SelectedRow.FindControl(“label_accid”)).Text;
Session["manage"] = selectedID.ToString();
//Response.Write(“Original message :”+selectedText);
Page.Response.Redirect(“payrollfeature.aspx”);
}
protected void delete_Click(object sender, EventArgs e)
{
try
{
con.Open();
//string sql = “SELECT count(*)as rows FROM FK_TBLUSER where NAME=’” + struser + “‘”;
}
catch (Exception e1)
{
Page.Response.Write(“Error in Delete : ” + e1);
}
finally { con.Close(); }
}//end of delete_click
protected void GridView_account_RowCommand(object sender, GridViewCommandEventArgs e)
{
ValidationCode result;
objCon = new ConnectionClass();
con = new OdbcConnection();
con = objCon.Connect_thru_config();
if (e.CommandName.Equals(“AddNew”))
{
TextBox name = (TextBox)GridView_account.FooterRow.FindControl(“txt_newname”);
TextBox passowrd = (TextBox)GridView_account.FooterRow.FindControl(“txt_newpassword”);
TextBox email = (TextBox)GridView_account.FooterRow.FindControl(“txt_newemail”);
passowrd.Text = “11111″;
if (name.Text == “”)
{
Page.Response.Write(“Please enter User name”);
}
else if (email.Text == “”)
{
Page.Response.Write(“Please enter Email address “);
}
else
{
int valid = checkuser(name.Text);
if (valid >= 1)
{
Page.Response.Write(“<b><font-weight: bold font-color:red >User already exists</font></b>” + valid);
}
else
{
result = CreateNewLogin(name.Text, passowrd.Text, email.Text);
if (result.Equals(“LoginCreated”))
{
Page.Response.Write(“Login has been created sucessfully :” + result);
Page.Response.Write(“<b>Welcome :<font color=\”blue\”>Login has been successfully created</font></b>”);
}
else
Page.Response.Write(“UnSuccessful: ” + result);
fillgridview();
}
}//
}
}//
protected void GridView_account_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView_account.EditIndex = e.NewEditIndex;
fillgridview();
}//
protected void GridView_account_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView_account.EditIndex = -1;
fillgridview();
}//
protected void GridView_account_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Label accid = (Label)GridView_account.Rows[e.RowIndex].FindControl(“label_accid”);
TextBox name = (TextBox)GridView_account.Rows[e.RowIndex].FindControl(“txt_name”);
//TextBox passowrd = (TextBox)GridView_account.FooterRow.FindControl(“txt_password”);
objCon = new ConnectionClass();
con = new OdbcConnection();
con = objCon.Connect_thru_config();
con.Open();
string updateQuery = “update FK_TBLUSER set NAME=’” + name.Text + ‘where CID=’” + accid.Text
+ “‘”;
OdbcCommand updatecmd = new OdbcCommand(updateQuery, con);
updatecmd.CommandType = CommandType.Text;
updatecmd.ExecuteNonQuery();
con.Close();
GridView_account.EditIndex = -1;
fillgridview();
}//
protected void GridView_account_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string a = GridView_account.DataKeys[e.RowIndex].Values[0].ToString();
objCon = new ConnectionClass();
con = new OdbcConnection();
con = objCon.Connect_thru_config();
try
{
con.Open();
string deletequery = “delete from FK_TBLUSER where CID=’” + a.ToString() + “‘”;
OdbcCommand delcmd = new OdbcCommand(deletequery, con);
delcmd.ExecuteNonQuery();
}
catch (Exception e1)
{
Page.Response.Write(“Can not delete : ” + e1);
}
finally
{
con.Close();
}
GridView_account.EditIndex = -1;
fillgridview();
}//
public ValidationCode CreateNewLogin(string strLogin, int intParent, int strType, string strPassword)
{
//Create a connection
ConnectionClass conobj = new ConnectionClass();
con = new OdbcConnection();
con = conobj.Connect_thru_config();
con.Open();
// Create a command object for the query
Page.Response.Write(“Before SQL”);
string strSQL = “INSERT INTO FK_TBLUSER (NAME,PASSWORD) ” + “VALUES(?,?)”;
Page.Response.Write(“After SQL”);
OdbcCommand objCmd = new OdbcCommand(strSQL, con);
OdbcParameter paramUsername;
paramUsername = new OdbcParameter(“@Username”, OdbcType.VarChar, 50);
paramUsername.Value = strLogin;
objCmd.Parameters.Add(paramUsername);
//for Password parameters
//Encrypt the password
MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
byte[] hashedBytes;
UTF8Encoding encoder = new UTF8Encoding();
hashedBytes = md5Hasher.ComputeHash(encoder.GetBytes(strPassword));
OdbcParameter paramPwd;
paramPwd = new OdbcParameter(“@Password”, OdbcType.Binary, 16);
paramPwd.Value = hashedBytes;
objCmd.Parameters.Add(paramPwd);
//for User Name parameters
OdbcParameter paramUsermail;
paramUsermail = new OdbcParameter(“@Usermail”, OdbcType.VarChar, 50);
paramUsermail.Value = strEmail;
objCmd.Parameters.Add(paramUsermail);
objCmd.ExecuteNonQuery();
con.Close();
return ValidationCode.LoginCreated;
}//end of CreateNewLogin
// to check user exist or not
public int checkuser(string struser)
{
ConnectionClass conobj = new ConnectionClass();
con = new OdbcConnection();
con = conobj.Connect_thru_config();
int countuser = 0;
con.Open();
string sql = “SELECT count(*)as rows FROM FK_TBLUSER where NAME=’” + struser + “‘”;
cmd = new OdbcCommand(sql, con);
myreader = cmd.ExecuteReader();
while (myreader.Read())
{
countuser = Convert.ToInt32(myreader["rows"]);
//Page.Response.Write(result);
}
Page.Response.Write(“Value of Result :” + countuser + “<br>”);
myreader.Close();
con.Close();
return countuser;
}
Table Structure
CREATE TABLE [FK_TBLUSER] (
[CID] [int] IDENTITY (1, 1) NOT NULL ,
[NAME] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[PASSWORD] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
CONSTRAINT [FK_TBLUSER_PK] PRIMARY KEY CLUSTERED
(
[CID]
) ON [PRIMARY]
) ON [PRIMARY]
GO
