TOP

dblinq – asp.net MySql

http://dblinq.codeplex.com/

Read More
TOP

Linq to SQL Combine two columns to make a readonly custom column

var rqSaleRepId = from m in db.RequestSaleReps select new { m.RequestSaleRepId, @RepNoWithName=m.SaleRepNo + ” – ” + m.Name};
ViewData["RequestSaleRepID"] = new SelectList(rqSaleRepId, “RequestSaleRepId”, “RepNoWithName”, editView.RequestSaleRepId);

<%= Html.DropDownList("RequestSaleRepId", (IEnumerable)ViewData["RequestSaleRepID"])%>

Read More
TOP

Insert, Update, and Delete Operations – LINQ to SQL

http://msdn.microsoft.com/en-us/library/bb386931.aspx

Northwnd db = new Northwnd(@”c:\Northwnd.mdf”);

// Query for a specific customer.
var cust =
(from c in db.Customers
where c.CustomerID == “ALFKI”
select c).First();

// Change the name of the contact.
cust.ContactName = “New Contact”;

// Create and add a new Order to the Orders collection.
Order ord = new Order { OrderDate = DateTime.Now };
cust.Orders.Add(ord);

// Delete an existing Order.
Order ord0 = cust.Orders[0];

// Removing it from the table also removes it from the Customer’s list.
db.Orders.DeleteOnSubmit(ord0);

// Ask the DataContext to save all the changes.
db.SubmitChanges();

Read More
TOP

crud operations with asp.net mvc

http://ricardodsanchez.com/archive/2009/10/16/crud-operations-with-asp.net-mvc.aspx

Tuesday, October 20, 2009 1:12 PM Lately I have been trying to use ASP.NET MVC on new web projects. One main reason is just to get familiar with it and understand it as I understand ASP.NET web forms. Below, is a very simple tutorial that walks you through the necessary steps to create an ASP.NET MVC application with Create, Read, Update and Delete (CRUD) capabilities. This sample application also uses Linq to SQL and takes full advantage of data scaffolding.

First, if you haven’t already, you need to download and install MVC 1.0, you can do this here or better yet, you can download it using the Web Platform Installer, which you can use to download and install MVC 1.0 and many other tools such as MS SQL Express, and many toolkits to help you with your development.

Once you have installed MVC 1.0 in your computer, let’s go ahead and create our MVC web application.

Open Visual Studio, Create New Project, select Web and then ASP.NET MVC Web Application from the list of templates.

Creating a new MVC project

After you click OK, Visual Studio asks you if you want to create a Unit Test project. This is a new feature, and you should take advantage of it since one of the main reasons to use the MVC pattern is that it is easier to unit test than the web forms pattern.

If you want to create a unit test project for your new MVC application (recommended) just click OK.

After your application is created, you’ll see a solution with two projects, one is the MVC application and the other is your test project.

asp.net MVC creates the following folder structure by default, you can change it if you want:

MVC Solution

If you open the Controllers folder, you’ll see two files, AccountController and HomeController. These two files are created by default and they are intended to give you a head start with your MVC application. In the Views folder, you’ll also see three folders (Account, Home, Shared) with aspx files in them. These are View files that Visual Studio created by default.

The asp.net MVC application template is ready to run as it is, as simple as it seems, it already has Membership functionality so your users can register, log in, change password, etc… Of course the application is not very useful yet since it doesn’t have anything for your users to see or do other than register.

Let’s see what this sample application looks like, hit Ctrl-F5 to run the application. This is what the sample MVC application looks like. It has two pages (Home and About) and a link to Log On which will take you to a registration form, fully functional!

Our next step is to add a database so we can then create controllers and views to view and manage this data.

Locate the App_Data folder and right-click on it, then select Add | New Item. The Add New Item window open, select SQL Server Database and type the name of your new database. For this sample, I used a very creative name “mvcappdb”. Click OK.

After adding our new SQL Express database to our project, we need to create some tables. You can do this from within Visual Studio, just double-click on your database file and you’ll see your database objects in the Server Explorer window. I created two tables, Customers and Projects, now I’ll show you how to create LINQ to SQL classes to access our database.

This is what my database looks like after I created the two tables:

Next, I am going to add LINQ to SQL classes for our project. LINQ to SQL let’s you create CRUD ready applications very easily and it integrates very nicely with the MVC framework.

To create the LINQ to SQL class, right-click on the Models folder and select Add | New Item. Select Data from Categories and then LINQ to SQL Classes. Type a name for it (I am using the name MvcApplication) and then click Add.

Now, let’s create our data classes, to do this we just drag our tables from Server Explorer onto this design (yes. it is that easy!), see below:

Now we need to make some changes to the properties of our LINQ to SQL classes. Open the Properties of the MvcApplication.dbml file and change the values so it looks like the image below, this is important because we are going to call this classes from our code using these names:

Save the MvcApplication.dbml file and close it.

The Controller, we are going to create two controllers. One for each one of our database tables. This will allow us to create the necessary views for our application.

Locate the Controllers folder, right-click and select Add | Controller. A new window will open, make sure you name your controller as it is shown below and then add another controller for the Projects table.

When creating your controller, notice that Visual Studio asks you if you want to add action methods for Create, Update, and Details scenarios. In our example we will check this box because we are creating a CRUD application and these methods are necessary.

After creating the two controllers, one for Customer and one for Projects, you’ll see two new classes in your Controllers folder, these classes contain public methods to access and manage your data (remember we checked the box to add action methods? this is it).

Everything looks good so far, however, we still need to add some code so we can interact with our database using LINQ to SQL.

First, we need to add a reference to our LINQ to SQL model, add “using MvcApp.Model;” to the top of your class file. Now, let’s modify the Index() method in our Customers controller so we can actually return some customer data and view it.
The Index() method is going to be used to create the default (Index) view of your Customer data, it will display a read-only list of your Customers.

Locate the Index() method in your CustomersController class. We are going to replace the existing code with the following:

public ActionResult Index()
        {
            var db = new DB();
            var view = db.Customers;
            return View(view);
        }

What we are doing is instantiating our LINQ to SQL class and then returning the Customers table. Build your application, we need to make sure everything is Ok and also we need to do this before we go to the next step.

That is all the code you need to display a data grid of your Customers data. However, we still need to have a View to do this. To accomplish this will let Visual Studio create the View. Right-click anywhere inside the Index() method we just modified ans select Add View.

The following window opens, make sure you check/select the options as shown below:

Visual Studio adds a new folder labeled Customers under the Views folder. Also, a new Index.aspx file is created in this new folder, this is your list view for the customer’s table. Hit Ctrl-F5 to see this page.

Once the default page is loaded, just add “Customers” to the end of the URL. You now have an empty data grid with all your Customer table columns in it. Not very helpful, but we are getting there.

You’ll notice that MVC adds some action links to the grid (Edit, Details) and the page itself (Create New). If you click on any of those, you’ll get an error because we have no implemented any of those views (Edit, Details, Create).

Let’s go ahead and create the code and the View for the Create action so we can start adding some data to our Customer’s table.

Go to the CustomersController file and locate the Create method, and make sure you go to the one that uses POST and not the GET method. It is best practices to use GET and HEAD only for retrieval purposes and nothing else. GET and HEAD are considered “safe”.

Replace the existing Create(FormCollection collection) method with the following code:

[AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(Customer customer)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var db = new DB();
                    db.Customers.InsertOnSubmit(customer);
                    db.SubmitChanges();
                    return RedirectToAction(“Index”);
                }
                catch
                {
                    return View(customer);
                }
            }
            else
            {
                return View(customer);
            }
        }

The above code creates an instance of our database object and then it uses LINQ methods to insert our new customer object and then it submits the changes to the database. Finally, it redirects the user to the main page (the list view) and if it fails, it also redirects the user to the starting point, the list view page.

The line “ModelState.IsValid” validates our customer object before we do anything else. If any of the data is not valid, the view will display error messages. When Visual Studio created the views based on our controller(s) it also adds client validation to the views. Build the project.

Let’s create (or better said, let Visual Studio) a view to create records in the Customer table. Right-click anywhere inside the Create method and then select Add View. In the window that opens, make sure the following options are selected:

Click on Add, the new view opens. Before we run the application again to see if our new view works, we need to modify it a little bit. Visual Studio and MVC are smart when creating these views, however, it doesn’t know if your primary key is  set to increment automatically (auto number). In this example, our database is set this way so we need to remove this field from the Create view or the view will not work correctly. You need to remove the CustomerId controls from this view, see image below, I removed the highlighted text.

 

It looks like we are ready to test this, hit Ctrl-F5 and then add Customers to the URL. Once you are in the Customers list view page, go ahead and click on Create New.

The new Create page opens up and you should see a nicely formatted form with labels for each one of our table fields (excluding the CustomerId) and text boxes. Let’s fill those up with some test data and then click the Create button to save it to the database. You’ll be redirected to the Customer view list where you should see the new records you just created.

Finally, we are going to add the necessary code to add the Edit view to our application.

Replace the existing code for both Edit methods with the following:

        //
        // GET: /Customers/Edit/5

        public ActionResult Edit(int id)
        {
            var db = new DB();
            var customer = db.Customers.SingleOrDefault(x => x.CustomerId == id);
            return View(customer);
        }

        //
        // POST: /Customers/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, FormCollection collection)
        {
            var db = new DB();
            var customer = db.Customers.SingleOrDefault(x => x.CustomerId == id);

            try
            {
                UpdateModel(customer, collection.ToValueProvider());
                db.SubmitChanges();
                return RedirectToAction(“Index”);
            }
            catch
            {
                return View(customer);
            }
        }

Build the application and now let’s create the Edit view. Right-click anywhere within the second Edit method ans select Add View. Make sure the window that opens have the following options selected:

Once the new view opens, let’s go ahead and remove the textbox that was created to modify the CustomerId value, we don’t want/can’t change this value. We also do not need the validator for this field, we’ll just leave the label for now:

I just removed the “Html.TextBox” part from it and the validation.

Hit Ctrl-F5 and then add Customers to the URL and then click on Edit. This is what the new Edit view looks like:

At this point, our application can view, add and edit records. We only need to add the delete functionality to it and we’ll have a fully functional CRUD enabled ASP.NET MVC application.

ASP.NET MVC does not add the Delete function by default, so we’ll have to code this ourselves using our LINQ to SQL object, it is pretty straight forward. Following the same structure as the other methods in our CustomersController, I’ve added the two new Delete methods. See below:

//
// HTTP GET: /Customers/Delete/1

[Authorize]
public ActionResult Delete(int id)
{
        var db = new DB();
        var customer = db.Customers.SingleOrDefault(x => x.CustomerId == id);

        if (customer == null)
            return View(“NotFound”);

        return View(customer);
}

//
// HTTP POST: /Customers/Delete/1

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Delete(int id, string confirmButton)
{
        var db = new DB();
        var customer = db.Customers.SingleOrDefault(x => x.CustomerId == id);

        if (customer == null)
             return View(“NotFound”);

        db.Customers.DeleteOnSubmit(customer);
        db.SubmitChanges();

        return View(“Deleted”);
}

With the code above, we are basically doing the same thing as we did with the Update methods. We query our Customer’s object, get the one record we are about to delete and then save our changes to the database.

If you want to prevent unauthorized users from deleting records, you can add the attribute “Authorize” which prevents unauthorized users from deleting any data. This is an example of the attribute:

[AcceptVerbs(HttpVerbs.Post), Authorize]

Also, if you want to specify an specific role to authorize a certain action in a view, you just need to specify that in the attribute as well. For example, if you just want to allow users with the role “Administrator” to be able to delete records, then you’ll just add the following right above your second Delete method:

[AcceptVerbs(HttpVerbs.Post), Authorize]
[Authorize(Roles = "Administrator")]

Build your project. Let’s create our Delete view, right-click on the second Delete method and select Add View and then select the following options:

Yes, we don’t have a Delete option under view content, however we have the Empty option which will create a blank view for us.

Once the new view is opened, add the following html/tags to it:

<asp:Content ID=”Content1″ ContentPlaceHolderID=”TitleContent” runat=”server”>
    Delete Confirmation: <%=Html.Encode(Model.FirstName) %>
</asp:Content>

<asp:Content ID=”Content2″ ContentPlaceHolderID=”MainContent” runat=”server”>

    <h2>Delete Confirmation</h2>

    <div>
        <p>
            Please confirm you want to remove this Customer from the database:
            <i> <%=Html.Encode(Model.FirstName) %>? </i>
        </p>
    </div>
   
    <% using (Html.BeginForm()) { %>

        <input name=”confirmButton” type=”submit” value=”Delete” />       

    <% } %>

</asp:Content>

This will complete the Delete view. Let’s test it now. Hit Ctrl-F5 and then add the following to the URL:

“customers/delete/1″

This is telling the MVC application to go and find the customer with id 1 and delete it.

“customers” = customers object

“delete” = action

“1″ = id

If you want or need to add the same functionality to the other controller, just repeat the steps above for each one of your controllers and actions desired (list, create, update, delete).

This is a very basic tutorial, I hope it helps you understand ASP.NET MVC a little more by trying this practical approach where we created an application that has CRUD capabilities.

Read More
TOP

Team Foundation Server Overview

Read More
TOP

Version control in the clouds

http://stackoverflow.com/questions/204470/version-control-in-the-clouds

Read More
TOP

Load Balancing Web Applications

Read More
TOP

Software Development Document

1. Acquisition Plan
2. Availability Plan
3. Bill of Materials Template
4. Capacity Plan
5. Change Management Plan
6. Concept Proposal
7. Configuration Management Plan
8. Conversion Plan
9. Concept of Operations
10. Cost Benefit Analysis
11. Database Design Document
12. Deployment Plan
13. Design Document
14. Feasibility Study
15. Functional Requirements
16. Installation Plan
17. Interface Control Document
18. Maintenance Plan
19. Needs Statement
20. Operations Guide
21. Risk Management Plan
22. Setup Guide
23. Statement of Work
24. Software Requirements Specification
25. System Boundary Document
26. System Design
27. System Specifications
28. Security Plan
29. Transition Plan
30. Verification Plan

Read More
TOP

Paging in LINQ

   var query = from c in Aspnet_Users orderby c.UserName select c ;

    int pageNumber = 4;
    int pageSize = 10;

    var pagedData = query.Skip((pageNumber – 1) * pageSize).Take(pageSize);

Read More
TOP

Linq 101

Read More

Switch to our mobile site