C# Programming: January 2016

Tuesday, January 12, 2016

ASP.NET MVC Database Tutorial on CRUD's Update

Welcome to Another tutorial on ASP.net MVC Database CRUD's Update tutorial.
Lets start without any delay.
At first Create a UC user Context in top of class.
 private userContext UC = new userContext();

Now make an ActionResult method as follows:

 [HttpGet]
        public ActionResult update(string username)  //passing username to change
        {
            user usr = UC.users.SingleOrDefault(u => u.username == username); //bringing a row to //change
            usr.username = "John";  //changing old username to 'john'
            UC.SubmitChanges();    //confirming change...


            return RedirectToAction("users", "Home");       //After updating user return view of users list
        }

Above piece of code will easily allow you to update the user. By knowing these basics you can make your own advanced form of code with some HTML form type of work in HttpPost also.GoodLuck.

If you want to learn more on User context and how that UC is working then you can watch this video too for more clear understanding.

Saturday, January 9, 2016

Javascript Function in very easy way

Welcome to another Javascript Tutorial on Function. Function is just a bunch of codes given a name.
Lets start making a function in this tutorial. Write following line of code in notepad or any editor and save as dot html (myfunction.html).

<html>
<head>
<title>Script in head section</title>
<script type="text/javascript">      
function message()               
{
alert("This alert box was called with the onload event"); 
}
</script>
</head>

<body onLoad="message()">      


</body>
</html>

Here script tag start our javascript.
function keyword is use to make function. Here message is our function.
alert is another pre built function to show message.
Message Function will execute upon a page load.
Now start that file with any browser and see the magic :)

You can watch following video for above stuff.

Tuesday, January 5, 2016

ASP.NET MVC Full Database Tutorial

Welcome to Database tutorial on Microsoft ASP.net MVC web app. You are at perfect place to give a jumpstart on asp mvc database.

Lets Start our Journey.

Lets Create Database First.
 Just goto Appdata folder and create a database and click on create new table.
The table definition is below:

CREATE TABLE [dbo].[users] (
    [Id]          INT            IDENTITY (1, 1) NOT NULL,
    [username]   NVARCHAR (MAX) NOT NULL,
    [password]   NVARCHAR (MAX) NOT NULL,
    [email]      NVARCHAR (MAX) NOT NULL,

    CONSTRAINT [PK_users] PRIMARY KEY CLUSTERED ([Id] ASC)
);

Here PK means primary key in our users table .

Lets Create our Model.
Model means just a class.

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

namespace mvc.Models
{
    [Table(Name = "users")]
    public class user
    {
        [Column(IsPrimaryKey = true, IsDbGenerated = true)]
        public int Id;
        [Column]
        public string username;
        [Column]
        public string password;
        [Column]
        public string email;
     


    }
}

//----To remove red lines just add a sqlite reference from references-------------------------

Lets Create A Context of user:
Context means just a easy way to connect with database table related with our user class here.
//-------context-----------------------
using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Web;

namespace mvc.Models
{
    public class userContext : DataContext
    {
       // for local
        private const String ConnectionString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|mydatabase.mdf;Integrated Security=True";
//for server:
 // Data source will be different check in your connection string in server

        public userContext()
            : base(ConnectionString)
        {
        }

        public Table<user> users;

    }
}

//----------------------------------------


View and Controller:
To show view and controller will be very bulky here because must of the codes are already given with the default asp mvc framework. So i suggest you to watch this video for full scenario. It is a complete tutorial on asp.net mvc web app with database.

The first of Databse Tutorial is here.



The second Video is here.

Ok guys you are now able to make a users list fetching data from databse table in asp mvc.
For further tutorials stay tuned. For javascript tutorial, you can visit my javascript blog:
https://javascripthero.blogspot.com