Wednesday, January 13, 2016

MVC Drop Down List

using MvcApplication3.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplication3.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";
            return View();
        }
        public ActionResult About()
        {
            return View();
        }
        public ActionResult GetSt()
        {
            List<Student> studentList = new List<Student>();
            List<SelectListItem> ItemList = new List<SelectListItem>();
            Student st1 = new Student() { Id = 1, Name = "Kassa" };
            Student st2 = new Student() { Id = 2, Name = "Sameera" };
            studentList.Add(st1);
            studentList.Add(st2);
            foreach (var student in studentList)
            {
                ItemList.Add(new SelectListItem() { Value = student.Id.ToString(), Text = student.Name });
            }
            ViewBag.StudentsBag = ItemList;
            return View();
        }
        [HttpPost]
        public ActionResult AddSt()
        {
            return View();
        }
        [HttpPost]
        public ActionResult AddStEnd(FormCollection form)
        {
            var itemId = form["Id"].Split(',');
            var itemName = form["Name"].Split(',');
            return RedirectToAction("GetSt");
        }
        [HttpPost]
        public ActionResult GetDe(FormCollection item)
        {
            //SelectListItem it = item;
            var addedItems = item["StudentListDrop"].Split(',');
            int selectedId = Convert.ToInt32(addedItems[0]);
            Student ss = new Student() { Id = 2, Name = "Samee" };
            ViewBag.StudentS = ss;
            return View();
        }
    }
}




<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<!DOCTYPE html>
<html>
<head runat="server">
    <title>GetSt</title>
</head>
<body>
    <div>
        <%using(Html.BeginForm("GetDe","Home",FormMethod.Post)){ %>
        <% = Html.DropDownList("StudentListDrop", (List<SelectListItem>)ViewBag.StudentsBag,"Select Student") %>
        <%= Html.TextBox("mmmmm") %>
        <br />
        <br />
        <input type="submit" value="GetDetails" />
        <%} %>
    </div>
    <div>
        <%=Html.ActionLink("Add New","AddSt","Home") %>
        <%using(Html.BeginForm("AddSt","Home",FormMethod.Post)){ %>
            <input type="submit" value="Add" />
        <%} %>
    </div>
</body>
</html>





<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<!DOCTYPE html>
<html>
<head runat="server">
    <title>GetDe</title>
    <h1>dfdfndjfn :</h1>
   
</head>
<body>
    <div>
        <%=Html.Label("Id :") %>
        <br />
        <%=Html.TextBox("Id",((MvcApplication3.Models.Student)ViewBag.StudentS).Id.ToString()) %>
        <br />
        <%=Html.Label("Name :") %>
        <br />
        <%=Html.TextBox("Name",((MvcApplication3.Models.Student)ViewBag.StudentS).Name) %>
    </div>
</body>
</html>


<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<!DOCTYPE html>
<html>
<head runat="server">
    <title>AddSt</title>
</head>
<body>
    <div>
        <%using (Html.BeginForm("AddStEnd","Home",FormMethod.Post)){ %>
            <%=Html.Label("Id :") %>
            <br />
            <%=Html.TextBox("Id") %>
            <br />
            <%=Html.Label("Name :") %>
            <br />
            <%=Html.TextBox("Name") %>
            <br />
            <input type="submit" value="Add" />
        <%} %>
    </div>
</body>
</html>



https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.parameters(v=vs.110).aspx

Friday, May 8, 2015

Run a query in Multiple Databases - SQL

we can use a inbuilt Stored procedure called 'sp_MSforeachdb' for querying multiple DBs.

this is a SP which run on all databases in currunt SQL Server insance.

ex:

declare @command varchar(100)
set @command='select ''?'''
exec sp_MSforeachdb @command

and this code will return all DB names as follows
























you can get this result sets to a single table using table variable as follows


declare @t table (dbname varchar(100))
declare @command varchar(100)
set @command='select ''?'''

insert into @t
exec sp_MSforeachdb @command
select * from @t























ans also you can get details of your preferred databases only


declare @t table (DBname varchar(50),TotalObjects varchar(max))
DECLARE @command varchar(max)

SELECT @command = 'IF ''?'' IN(''ExcelineDev'',''ReportServer$SQL2012'',''TestDB1''
) BEGIN USE ?
EXEC
(''
select DB_NAME(DB_ID()), count(*) as TotalObjects from [?].dbo.sysobjects
'')
END'

insert into @t
EXEC sp_MSforeachdb @command
select * from @t
























hope this would help

Thursday, May 7, 2015

SQL Server View and view types

To understand the concept of a View, we'll directly go to an example

First I create two table called 'Employee' and 'Departments' and add data to those tables

Employee Table










Departments Table











  • Assume that you need to store Employee with Department. In this point you can create a view. View is like a virtual table. A View doesn't store data. it is basically a table structure.
  • We can restrict viewing unwanted data to the users using views

we can make a view as follows

create view viewEmployee_Dept
as

select emp.EmployeeName, dep.DepartmentName
from Employee as emp inner join Departments as dep on emp.DepartmentId = dep.deptid























viewEmployee_Dept













There are few types of Views, The operations which can do with views depend on these View types


  1. System defined views - will be available in all user defined database
  • INFORMATION_SCHEMA_TABLES
  • sys.all_columns
     2.Information Schema views - display information of database, table, columns

  • INFORMATION_SCHEMA.COLUMNS

     3.Catelog view - can get db special information

  • sys.columns

     4. User defined views

  • simple view - get data from a single table only
            ex: create view viewEmployee

                  as
                  select EmployeeName,Salary
                  from Employee

  • complex view - getting data from multiple tables
           ex: create view viewEmployee_Dept

                 as
                 select emp.EmployeeName, dep.DepartmentName
                 from Employee as emp
         inner join Departments as dep on emp.DepartmentId = dep.deptid

Thursday, September 4, 2014

Simple MVC Concept



Basically MVC means Model-View-Controller.

View is the User Interfaces.That means what user or client see

Model is the back end. That means what actually happen in the server. This includes getting data from database ,handling business logic

Controller is the conncetion between View and Model

Let's understand this more clearly


Imagine that user need to login to a application

First he request permission with User name and Password. His initial request goes to the controller(in MVC) with parameter values. Then controller knows send it to the model. and model is responsible for check the user is exist or a valid user, using Data Source(normally Data base). Then model receive the status of the user and send it to the controller back. Then Controller pass the result data to the related view and then user can see whether he is granted to carry on or login failed.