Shalvin Interests

Wednesday, February 11, 2015

SharePoint SPWeb Class

using Microsoft.SharePoint;

 static void Main(string[] args)
        {
            SPSite s = new SPSite("http://toshiba-pc");

            SPWeb w = s.RootWeb;
            Console.WriteLine(w.Title );
            Console.WriteLine(w.CurrentUser);

            Console.WriteLine("");

         

            Console.WriteLine("All users");
             foreach (var item in w.AllUsers )
            {
                Console.WriteLine(item);
            }

             Console.WriteLine("Fields");
             foreach (var item in w.Fields)
             {
                 Console.WriteLine(item);
             }

        }

Wednesday, May 21, 2014

Html Input Type Date and MVC

View

<pre style="font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; color: #000000; background-color: #eee;font-size: 12px;border: 1px dashed #999999;line-height: 14px;padding: 5px; overflow: auto; width: 100%"><code>&lt;form method =&quot;post&quot; action =&quot;/&quot;&gt;
    &lt;input type =&quot;date&quot; name =&quot;calDOJ&quot; /&gt;
    &lt;div&gt;
        &lt;input type =&quot;submit&quot; /&gt;
    &lt;/div&gt;
&lt;/form&gt;
</code></pre>

Controller

 [HttpPost]
        public ActionResult Index(FormCollection fc)
        {
            DateTime dt =DateTime.Parse(  Request.Form["calDOJ"]);
            ViewBag.Hello = dt.ToString("D");
            return View();

        }

Wednesday, February 19, 2014

Windows Forms Calculator



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

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
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.