Sunteți pe pagina 1din 36

APPENDIX

CHAPTER 9

9.1. SOURCE CODES

Admin
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Location_Search
{
public partial class AdminPage : Form
{
#region //========== Global Declaration of SQL connection
=====================================
SqlConnection conn = new SqlConnection("Data Source=.;Initial
Catalog=webSearch;Integrated Security=True");
#endregion

public AdminPage()
{
InitializeComponent();
}
#region //=========== Execute code to store values in database
=====================
private void bt_add_Click(object sender, EventArgs e)
{
//Check whether user has specified all the contents in textboxes.
if ((tb_descripiton.Text != "") && (tb_keywords.Text != "") && (tb_url.Text != ""))
{
label7.Visible = label8.Visible = label9.Visible = false;
if (conn.State == ConnectionState.Closed)
conn.Open();
//Check whether the url already exist in database or not
SqlCommand cmd = new SqlCommand("select * from sites where url='" + tb_url.Text
+ "'", conn);
SqlDataReader dr_submitdetails = cmd.ExecuteReader();
if (dr_submitdetails.HasRows)
{
MessageBox.Show("URL already exists");
dr_submitdetails.Close();
tb_descripiton.Text = tb_keywords.Text = tb_url.Text = "";
}
else
{
dr_submitdetails.Close();
SqlCommand cmd_insert;
if (rb_content.Checked)
//insert values into sites table url,desc,key,rank,date
cmd_insert = new SqlCommand("insert into
sites(url,description,keywords,type,ranking,datecreated )values('" + tb_url.Text + "','" +
tb_descripiton.Text + "','" + tb_keywords.Text + "','CB','0','" + DateTime.Now + "')", conn);
else
cmd_insert = new SqlCommand("insert into
sites(url,description,keywords,type,ranking,datecreated )values('" + tb_url.Text + "','" +
tb_descripiton.Text + "','" + tb_keywords.Text + "','LB','0','" + DateTime.Now + "')", conn);
cmd_insert.ExecuteNonQuery();
MessageBox.Show("Records Inserted Successfully", "Success");
}
}
else
label7.Visible = label8.Visible = label9.Visible = true;
}
#endregion
#region //=========== Navigate to view Database windows ====================
private void bt_view_Click(object sender, EventArgs e)
{
DatBase frm_DB = new DatBase();
frm_DB.ShowDialog();
}
#endregion
#region //============ When admin window is closed navigate to login window
==========
private void AdminPage_FormClosing(object sender, FormClosingEventArgs e)
{
LoginPage Login = new LoginPage();
Login.Show();
}
#endregion
private void btnAddUsr_Click(object sender, EventArgs e)
{
AddUser newuser = new AddUser();
newuser.ShowDialog();
}
}
}

New user

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Location_Search
{
public partial class AddUser : Form
{
#region //========== Global Declaration of SQL connection
=====================================
SqlConnection conn = new SqlConnection("Data Source=.;Initial
Catalog=webSearch;Integrated Security=True");
#endregion

public AddUser()
{
InitializeComponent();
}
private void AddUser_Load(object sender, EventArgs e)
{
userid();
}
private void btnAddUser_Click(object sender, EventArgs e)
{
if ((txtUsrName.Text != "") && (txtpass.Text != "") && (txtConPass.Text != ""))
{
lblerr1.Visible = lblerr2.Visible = lblerr3.Visible = false;
if ((txtpass.Text) == (txtConPass.Text))
{
conn.Open();
SqlCommand cmd = new SqlCommand("insert into logindetails values('" +
txtUid.Text + "','" + txtUsrName.Text + "','" + txtpass.Text + "','user')", conn);
cmd.Connection = conn;
cmd.ExecuteNonQuery();
MessageBox.Show("New User Added Successfully","DataBase");
cmd.Dispose();
conn.Close();
userid();
txtUsrName.Text = "";
txtpass.Text = "";
txtConPass.Text = "";
}
else
{
MessageBox.Show("Confirmation of Password Mismatch","Alert!!");
}
}
else
{
lblerr1.Visible = lblerr2.Visible = lblerr3.Visible = true;
}
}
public void userid()
{
int uid;
conn.Open();
SqlCommand cmd = new SqlCommand("select max(uid) from logindetails", conn);
txtUid.Text = cmd.ExecuteScalar().ToString();
uid = Convert.ToInt32(txtUid.Text) + 1;
txtUid.Text = uid.ToString();
cmd.Dispose();
conn.Close();
}
}
}

Database

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Location_Search
{
public partial class DatBase : Form
{
#region //============= Globla declaration of connection string
=============================
SqlConnection conn = new SqlConnection("Data Source=.;Initial
Catalog=webSearch;Integrated Security=True");
#endregion
public DatBase()
{
InitializeComponent();
}
#region //============== Load all data from database to display in grid
====================
private void DatBase_Load(object sender, EventArgs e)
{
if (conn.State == ConnectionState.Closed)
conn.Open();
SqlCommand cmd = new SqlCommand("select id as LinkID,url as URL, description as
Description,keywords as Keywords, type as Type,ranking as Ranking,datecreated as
DateCreated from sites", conn);
DataSet ds = new DataSet();
SqlDataAdapter myadapter = new SqlDataAdapter(cmd);
myadapter.Fill(ds);
DgView_DetaildDB.DataSource = ds.Tables[0].DefaultView;
if (DgView_DetaildDB.Rows.Count == 1)
MessageBox.Show("No Records Found", "Records not Found");
if (conn.State == ConnectionState.Open)
conn.Close();
}
#endregion
#region //============= Delete selected link from database
===========================
private void bt_delselectd_Click(object sender, EventArgs e)
{
if (DgView_DetaildDB.SelectedRows.Count > 0)
{
string sz_sno =
Convert.ToString(DgView_DetaildDB.SelectedRows[0].Cells[0].Value);
if (sz_sno != "")
{
if (conn.State == ConnectionState.Closed)
conn.Open();
SqlCommand cmd = new SqlCommand("Delete from sites where id='" + sz_sno +
"'", conn);
cmd.ExecuteNonQuery();
foreach (DataGridViewRow dr in DgView_DetaildDB.SelectedRows)
{
DgView_DetaildDB.Rows.Remove(dr);
}
MessageBox.Show("Deleted Successfully");
if (conn.State == ConnectionState.Open)
conn.Close();
}
else
MessageBox.Show("Please select Valid Data");
}
else
MessageBox.Show("Please select any Data");
}
#endregion

}
}

Login

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Location_Search
{
public partial class LoginPage : Form
{
#region //========== Global Declaration of SQL connection
=====================================
SqlConnection conn = new SqlConnection("Data Source=.;Initial
Catalog=webSearch;Integrated Security=True");
#endregion
public LoginPage()
{
InitializeComponent();
}

#region //=========== Global Decalration of variables to store user name and id


==============
public static string str = "";
public static string id="";
#endregion
#region //============ Execute code when Login button clicked
======================
private void bt_login_Click(object sender, EventArgs e)
{
if (conn.State == ConnectionState.Closed)
conn.Open();
SqlCommand cmd = new SqlCommand("select type,uid from logindetails where name='" +
txt_UserName.Text + "' and pass = '" + txt_passwd.Text + "'", conn);
SqlDataReader dr_login = cmd.ExecuteReader();
if (dr_login.HasRows)
{
dr_login.Read();
if (dr_login[0].ToString() == "admin")
{
AdminPage frM_ad = new AdminPage();
frM_ad.ShowDialog();
this.Hide();
}
else
{
//===================== Store uid to global variable "id" from login table
==========================
id = dr_login["uid"].ToString();
UserPage frm_user = new UserPage();
str = txt_UserName.Text;
frm_user.ShowDialog();
//sz_frm1Dept = dr_login[0].ToString();
//sz_frm1logid = dr_login[1].ToString();
//MainPage frm_mp = new MainPage();
// frm_mp.Show();
}
dr_login.Close();
txt_passwd.Text = txt_UserName.Text = "";
//this.Hide();
}
else
{
MessageBox.Show("Invalid Login Credentials" + "," + "Please try again", "ERROR");
dr_login.Close();
}
if (conn.State == ConnectionState.Open)
conn.Close();
}
#endregion
private void LoginPage_Load(object sender, EventArgs e)
{

}
#region //======= End Application when form is closed
===========================
private void LoginPage_FormClosing(object sender, FormClosingEventArgs e)
{
Application.Exit();
}
#endregion

}
}

User History

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Location_Search
{
public partial class UserHistory : Form
{
#region //================= Global declaration of variable to store in query
==================
public static string sz_link;
public static string desc = "";
public string linkid = "";
#endregion
#region //================= Global declaration SQL connection
==================
SqlConnection conn = new SqlConnection("Data Source=.;Initial
Catalog=webSearch;Integrated Security=True");
#endregion
public UserHistory()
{
InitializeComponent();
}
#region //=========== Load user browsed history in grid and diaplay =============
private void UserHistory_Load(object sender, EventArgs e)
{
if (conn.State == ConnectionState.Closed)
conn.Open();
SqlCommand cmd = new SqlCommand("select linkid as LinkID,uid as UserID,url as
URL, description as Description,date as LastAccess from usrhistry where uid='" + LoginPage.id
+ "'", conn);
DataSet ds = new DataSet();
SqlDataAdapter myadapter = new SqlDataAdapter(cmd);
myadapter.Fill(ds);
dataGridView1.DataSource = ds.Tables[0].DefaultView;

if (dataGridView1.Rows.Count == 0)
{
MessageBox.Show("No History Found", "Records not Found");
btnLink.Enabled = false;
}
if (conn.State == ConnectionState.Open)
conn.Close();
}
#endregion
#region //============ Clear all the history for the particular user ============
private void btnHistory_Click(object sender, EventArgs e)
{
if (dataGridView1.RowCount == 0)
{
MessageBox.Show("History Cleared", "No Records Found");
btnLink.Enabled = false;
}
if (conn.State == ConnectionState.Closed)
conn.Open();
SqlCommand cmd = new SqlCommand("delete from usrhistry where uid='" +
LoginPage.id + "'", conn);
cmd.ExecuteNonQuery();
dataGridView1.DataSource = null;
btnLink.Enabled = false;
}
#endregion
#region //======== Goto Link to which user clicked ====================
private void btnLink_Click(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count > 0)
{
// Get link,desc and link id to store in user history
string sz_sno = Convert.ToString(dataGridView1.SelectedRows[0].Cells[2].Value);
desc = Convert.ToString(dataGridView1.SelectedRows[0].Cells[3].Value);
linkid = Convert.ToString(dataGridView1.SelectedRows[0].Cells[0].Value);
if (sz_sno != "")
{
sz_link = sz_sno;
if (conn.State == ConnectionState.Closed)
conn.Open();
SqlCommand cmd = new SqlCommand("Select ranking from sites where id='" +
dataGridView1.SelectedRows[0].Cells[0].Value + "'", conn);
int n_rnk = Convert.ToInt32(cmd.ExecuteScalar());
n_rnk++;
SqlCommand cmd_update = new SqlCommand("Update sites set ranking = " +
n_rnk + " where id='" + dataGridView1.SelectedRows[0].Cells[0].Value + "'", conn);
cmd_update.ExecuteNonQuery();
#region //====== store history of the user ========
//Call user function to update user record
user();
#endregion
if (conn.State == ConnectionState.Open)
conn.Close();
Engine_Data frm_engin = new Engine_Data();
frm_engin.ShowDialog();
}
else
MessageBox.Show("Please select Valid URL");
}
else
MessageBox.Show("Please select any URL");
#endregion
#region //========== Save history of the user ===============
public void user()
{
SqlConnection con = new SqlConnection("Data Source=.;Initial
Catalog=webSearch;Integrated Security=True");
con.Open();
string query = "insert into usrhistry values ('" + linkid + "','" + LoginPage.id + "','" +
sz_link + "','" + desc + "','" + DateTime.Now + "')";
SqlCommand cmd = new SqlCommand(query, con);
cmd.ExecuteNonQuery();
}
#endregion

}
}

Userpage

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Location_Search
{
public partial class UserPage : Form
{
#region //========== Global declaration for sql connection =====================
SqlConnection conn = new SqlConnection("Data Source=.;Initial
Catalog=webSearch;Integrated Security=True");
#endregion
#region //=========== Global declaration of variables for user history =======
public static string sz_link;
public static string desc = "";
public static string key = "";
public static string linkid = "";
#endregion
public UserPage()
{
InitializeComponent();
}
#region //========= Search the keywork in database ======================
private void bt_search_Click(object sender, EventArgs e)
{
if (tb_search.Text != "")
{
if (conn.State == ConnectionState.Closed)
conn.Open();
string sz_like = "%" + tb_search.Text + "%";
SqlCommand cmd; //= new SqlCommand("select id as ID,url as URL, description as
Description from searches where description like '"+sz_like+"' order by ranking desc", conn);
if (rb_content.Checked)
cmd = new SqlCommand("select id as ID,url as URL, description as Description
from sites where keywords like '" + sz_like + "' and type='CB' order by ranking desc", conn);
else
cmd = new SqlCommand("select id as ID,url as URL, description as Description from sites
where keywords like '" + sz_like + "' and type='LB' order by ranking desc", conn);
DataSet ds = new DataSet();
SqlDataAdapter myadapter = new SqlDataAdapter(cmd);
myadapter.Fill(ds);
DgView_Links.DataSource = ds.Tables[0].DefaultView;
//DataGrid.Columns(0).HeaderText = MyTextBox.Text
if (DgView_Links.Rows.Count == 1)
MessageBox.Show("No Records Found", "Records not Found");
if (conn.State == ConnectionState.Open)
conn.Close();
}
else
{
MessageBox.Show("Please Enter Search Keyword", "Search Warning");
}
}
#endregion
#region //========== Goto the link to which user selected ==============
private void bt_link_Click(object sender, EventArgs e)
{
if (DgView_Links.SelectedRows.Count > 0)
{
//get link,desc and link id to save user history
string sz_sno = Convert.ToString(DgView_Links.SelectedRows[0].Cells[1].Value);
desc = Convert.ToString(DgView_Links.SelectedRows[0].Cells[2].Value);
linkid = Convert.ToString(DgView_Links.SelectedRows[0].Cells[0].Value);
if (sz_sno != "")
{
sz_link = sz_sno;
if (conn.State == ConnectionState.Closed)
conn.Open();
SqlCommand cmd = new SqlCommand("Select ranking from sites where id='" +
DgView_Links.SelectedRows[0].Cells[0].Value + "'", conn);
int n_rnk = Convert.ToInt32(cmd.ExecuteScalar());
n_rnk++;
SqlCommand cmd_update = new SqlCommand("Update sites set ranking = " +
n_rnk + " where id='" + DgView_Links.SelectedRows[0].Cells[0].Value + "'", conn);
cmd_update.ExecuteNonQuery();

#region //==== Call user function to store user history ===============


//Call user function to update user record
user();
#endregion
if (conn.State == ConnectionState.Open)
conn.Close();
Engine_Data frm_engin = new Engine_Data();
frm_engin.ShowDialog();
}
else
MessageBox.Show("Please select Valid URL");
}
else
MessageBox.Show("Please select any URL");
}
#endregion
private void SignIn_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
LoginPage login = new LoginPage();
login.Show();
this.Hide();
}
#region //========== Display user name in screen =======================
private void UserPage_Load(object sender, EventArgs e)
{
label1.Text = LoginPage.str;
}
#endregion
#region //========== storing history of user =================
public void user()
{
SqlConnection con = new SqlConnection("Data Source=.;Initial
Catalog=webSearch;Integrated Security=True");
con.Open();
string query = "insert into usrhistry values ('" + linkid + "','" + LoginPage.id + "','" +
sz_link + "','" + desc + "','" + DateTime.Now + "')";
SqlCommand cmd = new SqlCommand(query, con);
cmd.ExecuteNonQuery();
}
#endregion
#region //=========== Goto History ====================
private void btnHistory_Click(object sender, EventArgs e)
{
UserHistory histry = new UserHistory();
histry.ShowDialog();
}
#endregion

}
}
9.2 SCREEN SHOT

FIG 9.2.1 LOGIN FORM


FIG 9.2.2 INSERTING NEW URL FOR LOCATION BASED
FIG 9.2.3 INSERTING NEW URL FOR CONTENT BASED
FIG 9.2.4.1 ADMIN PAGE
FIG 9.2.4.2 VIEW DATABASE
FIG 9.2.5 DELETING WITHOUT SELECTING A RECORD
FIG 9.2.6 DELETE A RECORD FROM THE DATABASE
FIG 9.2.7 NEW USER REGISTERED
FIG 9.2.8 SEARCH ENGINE PAGE
FIG 9.2.9 HISTORY OF NEW USER
FIG 9.2.10 USER SEARCH RESULT FOR LOCATION BASED SEARCH
FIG 9.2.11 WEBSITE SELECTED BY THE USER FROM THE RESULT
FIG 9.2.12 USER SEARCH RESULT FOR CONTENT BASED SEARCH
FIG 9.2.13 WEBSITE SELECTED BY THE USER FROM THE RESULT
FIG 9.2.14 HISTORY OF THE USER SEARCHED DATA

S-ar putea să vă placă și