Vicomi
Archive for 2014
ASP Web Service
================ App Code --> Services -==============
[WebMethod]
public string HelloWorld() {
return "Hello World";
}
[WebMethod]
public void insertEmployeeService(string eid, string name, string house, string street, string city, string tel)
{
SqlConnection conn = new SqlConnection(Connection.connectionString);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "insert into TechOneEmployee values ('"+eid+"','"+name+"', '"+house+"', '"+street+"','"+city+"','"+tel+"');";
cmd.ExecuteNonQuery();
conn.Close();
}
[WebMethod]
public DataSet ViewEmployee()
{
SqlConnection conn = new SqlConnection(Connection.connectionString);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select * from TechOneEmployee;";
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "TechOneEmployee");
conn.Close();
return ds;
}
[WebMethod]
public void DeleteEmployee(string eid)
{
SqlConnection conn = new SqlConnection(Connection.connectionString);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "delete from TechOneEmployee where eid='"+eid+"'";
cmd.ExecuteNonQuery();
conn.Close();
}
[WebMethod]
public void UpdateEmployee(string eid, string name, string house, string street, string city, string tel)
{
SqlConnection conn = new SqlConnection(Connection.connectionString);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "update TechOneEmployee set name='"+name+"', house='"+house+"', street='"+street+"', city='"+city+"', tel='"+tel+"' where eid='"+eid+"'";
cmd.ExecuteNonQuery();
conn.Close();
}
================================================
====================== ASP Web/ Statnd Alone / MVC -----------
localhost.Service employee = new localhost.Service();
employee.insertEmployeeService(eid, name, house, street, city, tel);
------------------------------------------------------
[WebMethod]
public string HelloWorld() {
return "Hello World";
}
[WebMethod]
public void insertEmployeeService(string eid, string name, string house, string street, string city, string tel)
{
SqlConnection conn = new SqlConnection(Connection.connectionString);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "insert into TechOneEmployee values ('"+eid+"','"+name+"', '"+house+"', '"+street+"','"+city+"','"+tel+"');";
cmd.ExecuteNonQuery();
conn.Close();
}
[WebMethod]
public DataSet ViewEmployee()
{
SqlConnection conn = new SqlConnection(Connection.connectionString);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select * from TechOneEmployee;";
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "TechOneEmployee");
conn.Close();
return ds;
}
[WebMethod]
public void DeleteEmployee(string eid)
{
SqlConnection conn = new SqlConnection(Connection.connectionString);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "delete from TechOneEmployee where eid='"+eid+"'";
cmd.ExecuteNonQuery();
conn.Close();
}
[WebMethod]
public void UpdateEmployee(string eid, string name, string house, string street, string city, string tel)
{
SqlConnection conn = new SqlConnection(Connection.connectionString);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "update TechOneEmployee set name='"+name+"', house='"+house+"', street='"+street+"', city='"+city+"', tel='"+tel+"' where eid='"+eid+"'";
cmd.ExecuteNonQuery();
conn.Close();
}
================================================
====================== ASP Web/ Statnd Alone / MVC -----------
localhost.Service employee = new localhost.Service();
employee.insertEmployeeService(eid, name, house, street, city, tel);
------------------------------------------------------
ViewBag Example
public ActionResult viewChartGirlsVsBoys()
{
DataManager dataManager = new DataManager();
var vSessionValue = new GeneralChartGirlsVsBoys();// For handling Sessions display Name
if ((Object)Session["SessionUserID"] != null)
{
List
ViewBag.TestList = new SelectList(tests, "testID", "testID");
vSessionValue.sSessionValue = "You are logged in as " + Session["SessionUserID"].ToString();
vSessionValue.sSessionValueName = Session["SessionUserName"].ToString().Replace(" ", "").Replace("*", ""); ;
return View(vSessionValue);
}
else
{
return RedirectToAction("Index", "Home");
}
}
======================= View =========-==-=-=-=-===========
<%: Html.DropDownList("CentreDropDown", new SelectList(ViewBag.Centres))%>
<%: Html.DropDownList("TestDropDown", new SelectList(ViewBag.Tests, "testID", "testID"))%>
MVC Login Authentication
-------------------------------- Home Controller ---------------------------------
public ActionResult Index()
{
Session.RemoveAll();
return View();
}
//--------------------------------------------------------------------------------
[HttpPost]
public ActionResult Index(Login model, string returnUrl)
{
// Assign value into session
DataManager dataManager = new DataManager();
if (ModelState.IsValid)
{
bool flag = (model.rememberMe); // setting remember me check box - test lakshika
FormsAuthentication.SetAuthCookie(model.userName, flag);
//xxxxx if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
// xxxxx && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
// if ((model.userName == "GR201300003") && (model.password == "GR201300003"))
if (dataManager.Login_Check(model.userName,model.password)==true)
{
//var vSessionValue = new Login();
Session["SessionUserName"] = dataManager.studentIDtoName(model.userName);
Session["SessionUserID"] = model.userName;
return RedirectToAction("ViewPage");
}
else
{
// ModelState.AddModelError("", "The user name or password provided is incorrect.");
ModelState.AddModelError("aaa", "adfdghdghgdhgdhdgda");
ViewBag.error = "Lakshika";
return RedirectToAction("Index", "Home","asaas");
}
}
else
{
// model.error_msg = model.update_content(model);
return RedirectToAction("Index");
}
}
public ActionResult ViewPage(Login model)
{
// Create object of model call for call properties and set value - lakshika345
var vSessionValue = new Login();
try
{
// check existing value into session (tested)
if ((Object)Session["SessionUserID"] != null)
{
vSessionValue.userName = model.userName;
vSessionValue.password = model.password;
vSessionValue.sSessionValue = "You are logged in as " + Session["SessionUserID"].ToString();
vSessionValue.sSessionValueName = Session["SessionUserName"].ToString().Replace(" ", "").Replace("*", ""); ;
}
else
{ // if session expired display the msg - tested
vSessionValue.sSessionValue = "Session Expired";
return RedirectToAction("Index");
}
}
catch(Exception)
{
}
// return value to view
return View(vSessionValue);
}
.NET MVC 3 Coding
------------------------------------- Connection Class -----------------------------------
class Connection
{
public static string connectionString
{
get { return "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=innosoftPOS;Data Source=Lakshika-PC\\SQLEXPRESS"; }
}
}
================================================================
-------------------------- Data Manager Class ---------------------------------------------------------
public class DataManager
{
public void insertCustomer(Customer customer)
{
string name = customer.name;
string house = customer.house;
string street = customer.street;
string city = customer.city;
string tel1 = customer.tel1;
string tel2 = customer.tel2;
int crediLimit = customer.creditLimit;
int currentCredit = customer.currentCredit;
SqlConnection conn = new SqlConnection(Connection.connectionString);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "insert into Customer values ('"+name+"','"+house+"','"+street+"','"+city+"','"+tel1+"','"+tel2+"',"+crediLimit+","+currentCredit+")";
cmd.ExecuteNonQuery();
conn.Close();
}
public List viewCustomer()
{
List customers = new List();
SqlConnection conn = new SqlConnection(Connection.connectionString);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select * from Customer";
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
Customer customer = new Customer();
customer.name = dr.GetString(0);
customer.house = dr.GetString(1);
customer.street = dr.GetString(2);
customer.city = dr.GetString(3);
customer.tel1 = dr.GetString(4);
customer.tel2 = dr.GetString(5);
customer.creditLimit = dr.GetInt32(6);
customer.currentCredit = dr.GetInt32(7);
customers.Add(customer);
}
// conn.Close();
return customers;
}
public Customer getCustomerByName(string name)
{
Customer customer = new Customer();
SqlConnection conn = new SqlConnection(Connection.connectionString);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select * from Customer where name ='"+name+"'";
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
customer.house = dr.GetString(1);
customer.street = dr.GetString(2);
customer.city = dr.GetString(3);
customer.tel1 = dr.GetString(4);
customer.tel2 = dr.GetString(5);
customer.creditLimit = dr.GetInt32(6);
customer.currentCredit = dr.GetInt32(7);
}
conn.Close();
return customer;
}
public void updateCustomer(Customer customer)
{
string name = customer.name;
string house = customer.house;
string street = customer.street;
string city = customer.house;
string tel1 = customer.tel1;
string tel2 = customer.tel2;
int creditLimit = customer.currentCredit;
int currentCredit = customer.currentCredit;
SqlConnection conn = new SqlConnection(Connection.connectionString);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "update Customer set house='" + house + "', street='" + street + "',city='" + city + "',telephone1='" + tel1 + "',telephone2='"+tel2+"',creditLimit='"+creditLimit+"',currentCredit='"+currentCredit+"' where name='"+name+"'";
cmd.ExecuteNonQuery();
conn.Close();
}
public void deleteCustomer(string name)
{
SqlConnection conn = new SqlConnection(Connection.connectionString);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "delete from Customer where name='"+name+"'";
cmd.ExecuteNonQuery();
conn.Close();
}
}
===============================================================
------------------------------------ Customer Controller ----------------------------------
//--------------------------------- Customer Create ---------------------------------
[HttpPost]
public ActionResult Create(Customer customer)
{
DataManager dataManager = new DataManager();
dataManager.insertCustomer(customer);
return View();
}
public ActionResult Create()
{
return View();
}
//-----------------------------------------------------------------------------------
//-------------------------------------- Customer View -----------------------------
public ActionResult View()
{
DataManager dataManager = new DataManager();
List customer = dataManager.viewCustomer();
return View(customer);
}
//-----------------------------------------------------------------------------------
//--------------------------------- Update ----------------------------------------
public ActionResult Update(string name)
{
DataManager dataManager = new DataManager();
Customer customer = dataManager.getCustomerByName(name);
return View(customer);
}
[HttpPost]
public ActionResult Update(Customer customer)
{
DataManager dataManager = new DataManager();
dataManager.updateCustomer(customer);
return RedirectToAction("View");
}
//---------------------------------------------------------------------------------
//------------------------------------ Delete ------------------------------------
public ActionResult Delete()
{
return View();
}
[HttpPost]
public ActionResult Delete(string name)
{
DataManager dataManager = new DataManager();
dataManager.deleteCustomer(name);
return RedirectToAction("View");
}
//----------------------------------------------------------------------------------
==================== Delete View ****---------------------------------------
<% foreach (var item in Model) { %>
<%: Html.ActionLink("Edit", "Update", new { name=item.name }) %> |
<%: Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) %> |
<%: Html.ActionLink("Delete", "Delete", new { name=item.name }) %>
<% using (Html.BeginForm("Delete", "Customer", new { name = item.name })) %>
<% { %>
<%} %>
<%: item.name %>
<%: item.house %>
<%: item.street %>
<%: item.city %>
<%: item.tel1 %>
<%: item.tel2 %>
<%: item.creditLimit %>
<%: item.currentCredit %>
<% } %>
-----------------------==========================================
class Connection
{
public static string connectionString
{
get { return "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=innosoftPOS;Data Source=Lakshika-PC\\SQLEXPRESS"; }
}
}
================================================================
-------------------------- Data Manager Class ---------------------------------------------------------
public class DataManager
{
public void insertCustomer(Customer customer)
{
string name = customer.name;
string house = customer.house;
string street = customer.street;
string city = customer.city;
string tel1 = customer.tel1;
string tel2 = customer.tel2;
int crediLimit = customer.creditLimit;
int currentCredit = customer.currentCredit;
SqlConnection conn = new SqlConnection(Connection.connectionString);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "insert into Customer values ('"+name+"','"+house+"','"+street+"','"+city+"','"+tel1+"','"+tel2+"',"+crediLimit+","+currentCredit+")";
cmd.ExecuteNonQuery();
conn.Close();
}
public List
{
List
SqlConnection conn = new SqlConnection(Connection.connectionString);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select * from Customer";
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
Customer customer = new Customer();
customer.name = dr.GetString(0);
customer.house = dr.GetString(1);
customer.street = dr.GetString(2);
customer.city = dr.GetString(3);
customer.tel1 = dr.GetString(4);
customer.tel2 = dr.GetString(5);
customer.creditLimit = dr.GetInt32(6);
customer.currentCredit = dr.GetInt32(7);
customers.Add(customer);
}
// conn.Close();
return customers;
}
public Customer getCustomerByName(string name)
{
Customer customer = new Customer();
SqlConnection conn = new SqlConnection(Connection.connectionString);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select * from Customer where name ='"+name+"'";
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
customer.house = dr.GetString(1);
customer.street = dr.GetString(2);
customer.city = dr.GetString(3);
customer.tel1 = dr.GetString(4);
customer.tel2 = dr.GetString(5);
customer.creditLimit = dr.GetInt32(6);
customer.currentCredit = dr.GetInt32(7);
}
conn.Close();
return customer;
}
public void updateCustomer(Customer customer)
{
string name = customer.name;
string house = customer.house;
string street = customer.street;
string city = customer.house;
string tel1 = customer.tel1;
string tel2 = customer.tel2;
int creditLimit = customer.currentCredit;
int currentCredit = customer.currentCredit;
SqlConnection conn = new SqlConnection(Connection.connectionString);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "update Customer set house='" + house + "', street='" + street + "',city='" + city + "',telephone1='" + tel1 + "',telephone2='"+tel2+"',creditLimit='"+creditLimit+"',currentCredit='"+currentCredit+"' where name='"+name+"'";
cmd.ExecuteNonQuery();
conn.Close();
}
public void deleteCustomer(string name)
{
SqlConnection conn = new SqlConnection(Connection.connectionString);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "delete from Customer where name='"+name+"'";
cmd.ExecuteNonQuery();
conn.Close();
}
}
===============================================================
------------------------------------ Customer Controller ----------------------------------
//--------------------------------- Customer Create ---------------------------------
[HttpPost]
public ActionResult Create(Customer customer)
{
DataManager dataManager = new DataManager();
dataManager.insertCustomer(customer);
return View();
}
public ActionResult Create()
{
return View();
}
//-----------------------------------------------------------------------------------
//-------------------------------------- Customer View -----------------------------
public ActionResult View()
{
DataManager dataManager = new DataManager();
List
return View(customer);
}
//-----------------------------------------------------------------------------------
//--------------------------------- Update ----------------------------------------
public ActionResult Update(string name)
{
DataManager dataManager = new DataManager();
Customer customer = dataManager.getCustomerByName(name);
return View(customer);
}
[HttpPost]
public ActionResult Update(Customer customer)
{
DataManager dataManager = new DataManager();
dataManager.updateCustomer(customer);
return RedirectToAction("View");
}
//---------------------------------------------------------------------------------
//------------------------------------ Delete ------------------------------------
public ActionResult Delete()
{
return View();
}
[HttpPost]
public ActionResult Delete(string name)
{
DataManager dataManager = new DataManager();
dataManager.deleteCustomer(name);
return RedirectToAction("View");
}
//----------------------------------------------------------------------------------
==================== Delete View ****---------------------------------------
<% foreach (var item in Model) { %>
<%: Html.ActionLink("Edit", "Update", new { name=item.name }) %> |
<%: Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) %> |
<%: Html.ActionLink("Delete", "Delete", new { name=item.name }) %>
<% using (Html.BeginForm("Delete", "Customer", new { name = item.name })) %>
<% { %>
<%} %>
<%: item.name %>
<%: item.house %>
<%: item.street %>
<%: item.city %>
<%: item.tel1 %>
<%: item.tel2 %>
<%: item.creditLimit %>
<%: item.currentCredit %>
<% } %>
-----------------------==========================================
ASP.NET Codings
public void insertCustomer(string name, string house, string street, string city, string tel1, string tel2, int crediLimit, int currentCredt)
{
SqlConnection conn = new SqlConnection(Connection.connectionString);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "insert into Customer values ('"+name+"','"+house+"','"+street+"','"+city+"','"+tel1+"','"+tel2+"',"+crediLimit+","+currentCredt+");";
cmd.ExecuteNonQuery();
conn.Close();
}
public void deleteCustomer(string name)
{
SqlConnection conn = new SqlConnection(Connection.connectionString);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "delete from Customer where name='"+name+"'";
cmd.ExecuteNonQuery();
conn.Close();
}
------------------------------------------------------------------------------------------------
Delete =================================
-------------------------------------------------------------------
protected void Button1_Click(object sender, EventArgs e)
{
DbAccess dba = new DbAccess();
string name = GridView1.SelectedValue.ToString();
dba.deleteCustomer(name);
}
----------------------------- Fill Culumns ----------------------------------------
protected void Button3_Click(object sender, EventArgs e)
{
GridViewRow row = GridView1.SelectedRow;
TextBox1.Text = row.Cells[1].Text;
TextBox2.Text = row.Cells[2].Text;
TextBox3.Text = row.Cells[3].Text;
TextBox4.Text = row.Cells[4].Text;
TextBox5.Text = row.Cells[5].Text;
TextBox6.Text = row.Cells[6].Text;
TextBox7.Text = row.Cells[7].Text;
TextBox8.Text = row.Cells[8].Text;
}
--------------------------------------------------------------
Stand Alone Application
public class Connection
{
public static string connectionString
{
get { return "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=innosoftPOS;Data Source=Lakshika-PC\\SQLEXPRESS"; }
}
}
---------------------------------------------------------------------------------------------------------
public void insertCustomer(string name, string house, string street, string city, string tel1, string tel2, int creditLimit, int currentCredit)
{
SqlConnection conn = new SqlConnection(Connection.connectionString);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "insert into Customer values ('"+name+"','"+house+"','"+street+"','"+city+"','"+tel1+"','"+tel2+"',"+creditLimit+","+currentCredit+");";
cmd.ExecuteNonQuery();
conn.Close();
}
public void deleteCustomer(string name)
{
SqlConnection conn = new SqlConnection(Connection.connectionString);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "delete from Customer where name='"+name+"';";
cmd.ExecuteNonQuery();
conn.Close();
}
public void updateCustomer(string name, string house, string street, string city, string tel1, string tel2, int creditLimit, int currentCredit)
{
SqlConnection conn = new SqlConnection(Connection.connectionString);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "update Customer set house='"+name+"',street='"+street+"',city='"+city+"',telephone1='"+tel1+"',telephone2='"+tel2+"',creditLimit="+creditLimit+", currentCredit="+currentCredit+" where name='"+name+"';";
cmd.ExecuteNonQuery();
conn.Close();
}
---------------------------------------------------------------------------------------------------------
Calling to DeleteCustomer Fuction
------------------------------------------
private void button2_Click(object sender, EventArgs e)
{
string name = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[0].Value.ToString();
DbAccess dba = new DbAccess();
dba.deleteCustomer(name);
}
private void button3_Click(object sender, EventArgs e)
{
textBox16.Text = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[0].Value.ToString();
textBox15.Text = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[1].Value.ToString();
textBox14.Text = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[2].Value.ToString();
textBox13.Text = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[3].Value.ToString();
textBox12.Text = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[4].Value.ToString();
textBox11.Text = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[5].Value.ToString();
textBox10.Text = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[6].Value.ToString();
textBox9.Text = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[7].Value.ToString();
}
----------------------------------------------------------------------------------------