Pages

Thursday, November 29, 2018

MVC link for quick learning

Action Method Parameters
  • We can organize the action methods for GET and POST requests separately.
  • We can easily create seperate action methods for each request types.
Request Type Attribute
  • [HttpGet]
  • [HttpPost]
Action method parameters
MVC framework provides 3 different action methods for Post Request, which are given below.
  1. Form Collection
  2. Formal Parameters
  3. Model Class Object 
1.
  1. using System.Web.Mvc;  
  2. namespace MVCActionMethodParameters.Controllers  
  3. {  
  4. public class HomeController : Controller  
  5. {  
  6. // GET: Home  
  7. [HttpGet]  
  8. public ActionResult Index()  
  9. {  
  10. return View();  
  11. }  
  12. [HttpPost]  
  13. public ActionResult Index(FormCollection frmobj) //FormCollection  
  14. {  
  15. string name = frmobj["userid"];  
  16. string password = frmobj["pwd"];  
  17. if (name == "Admin" && password == "123456")  
  18. {  
  19. Response.Write("<h2> Success </h2> Valid User");  
  20. }  
  21. else  
  22. Response.Write(" <h2> Failed </h2> Invalid User");  
  23. return View();  
  24. }  
  25. }  


  1. @{  
  2. ViewBag.Title = "Index";  
  3. }  
  4. <h1>Form Collection</h1>  
  5. <h2>Login </h2>  
  6. @using (Html.BeginForm())  
  7. {  
  8. <label>User Name</label>  
  9. <input type="text" class="form-control" id="userid" name="userid" />  
  10. <label>Password</label>  
  11. <input type="text" class="form-control" id="pwd" name="pwd" /> <br />  
  12. <input type="submit" class="btn btn-success" value="Submit" />  
  13. }  


2.

  1. using System.Web.Mvc;  
  2. namespace MVCActionMethodParameters.Controllers  
  3. {  
  4. public class LoginController : Controller  
  5. {  
  6. // GET: Login  
  7. [HttpGet]  
  8. public ActionResult Login()  
  9. {  
  10. return View();  
  11. }  
  12. [HttpPost]  
  13. public ActionResult Login(string userid, string pwd) //Formal Parameters  
  14. {  
  15. string username = userid;  
  16. string password = pwd;  
  17. if (username == "Admin" && password == "123456")  
  18. {  
  19. Response.Write("<h2> Success </h2> Valid User...");  
  20. }  
  21. else  
  22. Response.Write(" <h2> Failed </h2> Invalid User...");  
  23. return View();  
  24. }  
  25. }  
  26. }  

  1. @{  
  2. ViewBag.Title = "Login";  
  3. }  
  4. <h1>Formal Parameters</h1>  
  5. <hr />  
  6. <h2>Login</h2>  
  7. @using (Html.BeginForm())  
  8. {  
  9. <label>User Name</label>  
  10. <input type="text" class="form-control" id="userid" name="userid" />  
  11. <label>Password</label>  
  12. <input type="text" class="form-control" id="pwd" name="pwd" /> <br />  
  13. <input type="submit" class="btn btn-success" value="Submit" />  
  14. }  
3.
  1. namespace MVCActionMethodParameters.Models  
  2. {  
  3. public class Login  
  4. {  
  5. public string userid { getset; }  
  6. public string pwd { getset; }  
  7. }  
  1. namespace MVCActionMethodParameters.Models  
  2. {  
  3. public class Login  
  4. {  
  5. public string userid { getset; }  
  6. public string pwd { getset; }  
  7. }  

  1. <h1>Formal Parameters</h1>  
  2. <hr />  
  3. <h2>Login</h2>  
  4.    
  5. @using (Html.BeginForm())  
  6. {  
  7. <label>User Name</label>  
  8. <input type="text" class="form-control" id="userid" name="userid" />  
  9. <label>Password</label>  
  10. <input type="text" class="form-control" id="pwd" name="pwd" /> <br />  
  11. <input type="submit" class="btn btn-success" value="Submit" />  

Tuesday, October 9, 2018

Entity Framework Connectivity VS2015


/****** Object:  Table [dbo].[Employee]    ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Employee1](
[HREmpId] [numeric](18, 0) NOT NULL,
[FirstName] [nchar](10) NULL
) ON [PRIMARY]

GO


------------------------------------------------------------------------------------------------------------------


using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;


public class MyContext:DbContext

{
    public MyContext() :base("DefaultConnection")
    {
        //Database.SetInitializer<MyContext>(new CreateDatabaseIfNotExists<MyContext>());
    }

     virtual public DbSet<Employee> employee { get; set; }
}

---------------------------------------------------------------------------------------------------------------------
using System.ComponentModel.DataAnnotations;


public class Employee
{
    [Key]
    public decimal HREmpId { get; set; }
    public string FirstName { get; set; }
 

}

-------------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Data.Entity;
using System.Data.SqlClient;
using System.Linq;
using System.Runtime.Remoting.Contexts;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        using (MyContext m = new MyContext())
        { var s="";
            foreach (var a in m.employee)
            {
                s = s + a.FirstName;
            }
            Response.Write(s);
        }
     }
}

-------------------------------------------------------------------------------------------------------------------------
<connectionStrings>
    <add name="DefaultConnection"
      connectionString="Data Source=localhost\SQLSERVER;Initial Catalog=LearnEF;Persist Security Info=True;User ID=sa;password=123456"
      providerName="System.Data.SqlClient"/>
  </connectionStrings>

Monday, October 1, 2018

ReadXml c#

using System;
using System.Text;
using System.Xml;

namespace ReadXml
{
    class Program
    {
        static void Main(string[] args)
        {           
            XmlReader xmlReader = XmlReader.Create("xml file location");
            while(xmlReader.Read())
            {
                if((xmlReader.NodeType == XmlNodeType.Element) && (xmlReader.Name == "Cube"))
                {
                    if(xmlReader.HasAttributes)
                        Console.WriteLine(xmlReader.GetAttribute("currency") + ": " + xmlReader.GetAttribute("rate"));                   
                }
            }
            Console.ReadKey();
        }
    }
}