double Operand1, Operand2, result, value;
string Op;
Boolean clearDisplay;
private void Digit_Click(object sender, EventArgs e)
{
if (clearDisplay)
{
lblDisplay.Text = "";
clearDisplay = false;
}
//namespace display():
Button b = (Button)sender;
lblDisplay.Text = lblDisplay.Text + b.Text;
}
private void btnAdd_Click(object sender, EventArgs e)
{
}
private void GetOperator_Click(object sender, EventArgs e)
{
if (lblDisplay.Text.Length == 0)
{
return;
}
Operand1 = Convert.ToDouble(lblDisplay.Text);
Button b = (Button)sender;
Op = b.Text;
lblDisplay.Text = "";
clearDisplay = true;
}
private void btnEquals_Click(object sender, EventArgs e)
{
if (lblDisplay.Text.Length == 0)
{
return;
}
Operand2 = Convert.ToDouble(lblDisplay.Text);
clearDisplay = true;
switch (Op)
{
case "+":
result = Operand1 + Operand2;
lblDisplay.Text = result.ToString();
break;
case "-":
result = Operand1 - Operand2;
lblDisplay.Text = result.ToString();
break;
case "*":
result = Operand1 * Operand2;
lblDisplay.Text = result.ToString();
break;
case "/":
if (Operand2 != 0)
{
result = Operand1 / Operand2;
lblDisplay.Text = result.ToString();
}
break;
default:
break;
}
}
private void btnClear_Click(object sender, EventArgs e)
{
lblDisplay.Text = "";
}
private void btnBackSpace_Click(object sender, EventArgs e)
{
if (lblDisplay.Text.Length == 0)
{
return;
}
lblDisplay.Text = lblDisplay.Text.Remove(lblDisplay.Text.Length - 1, 1);
}
private void btnInverse_Click(object sender, EventArgs e)
{
if (lblDisplay.Text.Length == 0)
{
return;
}
value = Convert.ToDouble(lblDisplay.Text);
if (value != 0)
{
result = 1 / value;
lblDisplay.Text = result.ToString();
}
}
private void btnSign_Click(object sender, EventArgs e)
{
if (lblDisplay.Text.Length == 0)
{
return;
}
value = Convert.ToDouble(lblDisplay.Text);
result = -1 * value;
lblDisplay.Text = result.ToString();
}
Courtesy : Binish Babu
Wednesday, February 19, 2014
Windows Forms Calculator
Tuesday, February 11, 2014
Asp .Net MVC HttpPost and BeginForm Html Helpers
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(FormCollection frm)
{
string strName = Request.Form["Name"];
ViewBag.Name = strName;
return View();
}
}
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@ViewBag.Name
@using (Html.BeginForm())
{
@Html.Label("Name")
@Html.TextBox("Name")
<br />
<input type ="submit" />
}
Monday, February 10, 2014
Asp .Net Bank with Deposit and Withdraw Functionality
using System.Data;
using System.Data.SqlClient;
public class UserInfo
{
SqlConnection cnn;
public DataTable GetUsers()
{
using (cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
{
cnn.Open();
DataSet ds = new DataSet();
SqlDataAdapter da;
da = new SqlDataAdapter("select * from UserInfo", cnn);
da.Fill(ds, "Usr");
return ds.Tables ["Usr"];
}
}
using System.Data.SqlClient;
public partial class Admin_AdminHome : System.Web.UI.Page
{
UserInfo ui = new UserInfo();
SqlConnection cnn;
double dblCurBal = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ShowBal();
}
}
private void ShowBal()
{
ddlUser.DataSource = ui.GetUsers();
ddlUser.DataTextField = "UserName";
DataBind();
}
protected void ddlUser_SelectedIndexChanged(object sender, EventArgs e)
{
using (cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
{
ShowBalInLabel();
}
}
private void ShowBalInLabel()
{
if (cnn.State == System.Data.ConnectionState.Closed)
{
cnn.Open();
}
SqlCommand cmd = new SqlCommand("select * from UserInfo where UserName = @UserName", cnn);
cmd.Parameters.AddWithValue("@UserName", ddlUser.Text);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
lblCurBal.Text = dr["Balance"].ToString();
}
}
protected void btnDeposit_Click(object sender, EventArgs e)
{
using (cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
{
cnn.Open();
SqlCommand cmd = new SqlCommand("select * from UserInfo where UserName = @UserName", cnn);
cmd.Parameters.AddWithValue("@UserName", ddlUser.Text);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
dblCurBal = double.Parse(dr["Balance"].ToString());
}
dr.Close();
double dblNewBal = dblCurBal + double.Parse(txtAmount.Text);
cmd = new SqlCommand("update UserInfo set Balance = @NewBal where UserName = @UserName", cnn);
cmd.Parameters.AddWithValue("@NewBal", dblNewBal);
cmd.Parameters.AddWithValue("@UserName", ddlUser.Text);
cmd.ExecuteNonQuery();
ShowBalInLabel();
}
}
protected void btnWithdraw_Click(object sender, EventArgs e)
{
using (cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
{
cnn.Open();
SqlCommand cmd = new SqlCommand("select * from UserInfo where UserName = @UserName", cnn);
cmd.Parameters.AddWithValue("@UserName", ddlUser.Text);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
dblCurBal = double.Parse(dr["Balance"].ToString());
}
dr.Close();
double dblNewBal = dblCurBal - double.Parse(txtAmount.Text);
cmd = new SqlCommand("update UserInfo set Balance = @NewBal where UserName = @UserName", cnn);
cmd.Parameters.AddWithValue("@NewBal", dblNewBal);
cmd.Parameters.AddWithValue("@UserName", ddlUser.Text);
cmd.ExecuteNonQuery();
ShowBalInLabel();
}
}
}
Sunday, February 9, 2014
ExecuteScalar with Asp .Net
CREATE TABLE Users(
UserId int IDENTITY primary key,
UserName varchar(50),
Password varchar(50))
using System.Data.SqlClient;
using System.Configuration;
SqlConnection cnn;
protected void btnLogin_Click(object sender, EventArgs e)
{
using (cnn = new SqlConnection(
@"Data Source=.\sqlexpress;Initial Catalog=CustomLogin;Integrated Security=True"))
{
SqlCommand cmd;
cnn.Open();
cmd = new SqlCommand(
"select Count(*) from Users where UserName = @UserName and Password = @Password",
cnn);
cmd.Parameters.AddWithValue("@UserName", txtUserName.Text);
cmd.Parameters.AddWithValue("@Password", txtPassword.Text);
int intCount = Int32.Parse( cmd.ExecuteScalar().ToString());
if (intCount == 1)
Response.Redirect("Welcome.aspx");
else
lblMessage.Text = "Invalid credentials";
}
Saturday, February 8, 2014
Asp .Net MVC @Html.DropDownList
Hard coding the Drop Down List
Fillling DropDownList with Database data
Countries
@Html.DropDownList("Countries",
new List<SelectListItem> {
new SelectListItem {Text = "India" },
new SelectListItem {Text = "US" }
})
Fillling DropDownList with Database data
public ActionResult Index()
{
ContactGroups cg = new ContactGroups();
var cgs = cg.GetGroups();
ViewBag.groups =new SelectList(cgs,"GroupId","GroupName");
return View();
}
<div>
@Html.DropDownList("groups", "select Group")
</div>
Here groups is the name of ViewBag.
Asp .Net MVC BeginForm
@using(Html.BeginForm())
{
@Html.TextBox("txtName")
<BR />
<input type ="Submit" />
}
Friday, February 7, 2014
Linq to Objects
List<string> glsAspNet = new List<string> { "Shalvin", "Manoj", "Simil", "Tina" };
protected void btnContains_Click(object sender, EventArgs e)
{
var C = from p in glsAspNet
where p.Contains("a") | p.StartsWith("S")
select p;
ListBox1.DataSource = C;
DataBind();
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
List<string> obj = new List<string>{"Anjali", "Anju", "Ammu", "athira", "Minu",
"gayathri", "gopu", "anu"};
protected void Page_Load(object sender, EventArgs e)
{
ListBox1.Items.Clear();
// var Astart = from p in obj where p.StartsWith("A") select p;
// ListBox1.DataSource = Astart;
// DataBind();
}
protected void btnContains_Click(object sender, EventArgs e)
{
ListBox1.Items.Clear();
var res = from p in obj
where p.Contains("a")
select p;
ListBox1.DataSource = res;
DataBind();
}
protected void btnEndsWith_Click(object sender, EventArgs e)
{
ListBox1.Items.Clear();
var res = from p in obj
where p.EndsWith ("a")
select p;
ListBox1.DataSource = res;
DataBind();
}
protected void btnFirst_Click(object sender, EventArgs e)
{
ListBox1.Items.Clear();
var res = (from p in obj
select p).First();
//ListBox1.DataSource = res;
//DataBind();
Response.Write(res);
}
protected void Button4_Click(object sender, EventArgs e)
{
ListBox1.Items.Clear();
var res = from p in obj
where p.StartsWith("A") | p.StartsWith("g")
select p;
ListBox1.DataSource = res;
DataBind();
}
protected void btnAnd_Click(object sender, EventArgs e)
{
ListBox1.Items.Clear();
var res = from p in obj
where p.StartsWith("A") & p.EndsWith("a")
select p;
ListBox1.DataSource = res;
DataBind();
}
protected void btnOrderBy_Click(object sender, EventArgs e)
{
ListBox1.Items.Clear();
var res = from p in obj
orderby p
select p;
ListBox1.DataSource = res;
DataBind();
}
protected void btnOrderByDescending_Click(object sender, EventArgs e)
{
ListBox1.Items.Clear();
var res = from p in obj
orderby p descending
select p;
ListBox1.DataSource = res;
DataBind();
}
protected void btnContainsOrderBy_Click(object sender, EventArgs e)
{
ListBox1.Items.Clear();
var res = from p in obj
where p.Contains("a")
orderby p
select p;
ListBox1.DataSource = res;
DataBind();
}
protected void Button9_Click(object sender, EventArgs e)
{
ListBox1.Items.Clear();
var res = from p in obj
where p.Contains("a")|p.StartsWith("A")
orderby p
select p;
ListBox1.DataSource = res;
DataBind();
}
protected void btnTake_Click(object sender, EventArgs e)
{
ListBox1.Items.Clear();
var res = (from p in obj
select p).Take(5);
ListBox1.DataSource = res;
DataBind();
}
protected void btnTake2_Click(object sender, EventArgs e)
{
ListBox1.Items.Clear();
var res = (from p in obj where p.StartsWith("A")
select p).Take(5);
ListBox1.DataSource = res;
DataBind();
}
protected void btnSkip_Click(object sender, EventArgs e)
{
ListBox1.Items.Clear();
var res = (from p in obj
select p).Skip(5);
ListBox1.DataSource = res;
DataBind();
}
protected void btnStartsWith2_Click(object sender, EventArgs e)
{
ListBox1.Items.Clear();
var res = obj.Where(p => p.StartsWith("A"));
ListBox1.DataSource = res;
DataBind();
}
}
Linq and Collection of Class
List<Trainee> glTrainee = new List<Trainee>
{
new Trainee{Name = "Rejitha", Location = "Varkala", Hobby ="Music"},
new Trainee{Name = "Sarath", Location = "Sub Station", Hobby = "Reading"},
new Trainee{Name = "Anish", Location = "Sub Station", Hobby= "Swimming"},
new Trainee{Name ="Anoop" , Location ="Attinkuzhi", Hobby="Blogging"},
new Trainee{Name = "Vishnu", Location ="Front Gate", Hobby = "Photography"},
new Trainee{Name ="Sanal", Location ="Police Station", Hobby = "Talking"},
new Trainee{Name = "Manikandan", Location = "Police Station", Hobby = "Cricket"},
new Trainee{Name = "Saritha", Location = "Sub Station", Hobby="Reading"},
};
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
var Loc = glTrainee.Select(p => p.Location).Distinct().ToList();
ddlLocation.DataSource = Loc;
DataBind();
}
}
protected void btnSubStation_Click(object sender, EventArgs e)
{
var S = from p in glTrainee
where p.Location == "Sub Station"
select p;
GridView1.DataSource = S;
DataBind();
}
protected void btnStartsWithA_Click(object sender, EventArgs e)
{
var s = glTrainee.Where(p => p.Name.StartsWith("A"));
GridView1.DataSource = s;
DataBind();
}
protected void ddlLocation_SelectedIndexChanged(object sender, EventArgs e)
{
var s = from p in glTrainee
where p.Location == ddlLocation.Text
select p;
GridView1.DataSource = s;
DataBind();
}
protected void btnSelect_Click(object sender, EventArgs e)
{
var s = from p in glTrainee
select new {p.Name, p.Location};
GridView1.DataSource = s;
DataBind();
}
protected void btnSortByLocation_Click(object sender, EventArgs e)
{
var s = from p in glTrainee
orderby p.Location
select p;
GridView1.DataSource = s;
DataBind();
}
protected void btnSortByLocationThenByName_Click(object sender, EventArgs e)
{
var s = from p in glTrainee
orderby p.Location, p.Name
select p;
GridView1.DataSource = s;
DataBind();
}
protected void btnSingle_Click(object sender, EventArgs e)
{
//var s = from p in glTrainee
// where p.Name == "Manikandan"
//select p;
var s = (from p in glTrainee
where p.Name == "Manikandan"
select p).Single();
Label1.Text = s.Name;
//GridView1.DataSource = s;
//DataBind();
}
protected void btnSingleOrDefault_Click(object sender, EventArgs e)
{
var s = (from p in glTrainee
where p.Name == "Manikandans"
select p).SingleOrDefault();
if( s != null)
Label1.Text = s.Name;
}
Thursday, February 6, 2014
Asp .Net Balance Updating with Session Variable
In the Blog Creating and Consuming Class Library in C#, Object Initializer I have demonstrated creating a simple Bank with Deposit and Withdraw functionality in Windows Forms.
Creating that in Windows Forms is straight forward.
When it comes to Web Forms (Asp .Net) you have to maintain sessions to get the same functionality.
Creating that in Windows Forms is straight forward.
When it comes to Web Forms (Asp .Net) you have to maintain sessions to get the same functionality.
int Balance = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ShowBal();
}
else
{
if (Session["Balance"] != null)
Balance = (int)Session["Balance"];
}
}
private void ShowBal()
{
lblCurBal.Text = Balance.ToString();
txtAmount.Text = "";
txtAmount.Focus();
}
protected void btnDeposit_Click(object sender, EventArgs e)
{
Balance += Int32.Parse(txtAmount.Text);
Session["Balance"] = Balance;
ShowBal();
}
protected void btnWithras_Click(object sender, EventArgs e)
{
Balance -= Int32.Parse(txtAmount.Text);
Session["Balance"] = Balance;
ShowBal();
}
.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:Label ID="Label1" runat="server" Text="Balance"></asp:Label>
<asp:Label ID="lblCurBal" runat="server"></asp:Label>
<br />
<br />
<asp:Label ID="Label2" runat="server" Text="Amount"></asp:Label>
<asp:TextBox ID="txtAmount" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="btnDeposit" runat="server" OnClick="btnDeposit_Click" Text="Deposit" />
<asp:Button ID="btnWithras" runat="server" OnClick="btnWithras_Click" Text="Withdraw" />
</form>
</body>
</html>
Courtesy : Arjun Pandavath, Speridian Technologies
Subscribe to:
Comments (Atom)
