Posts

Showing posts from October, 2017

Donate

Task Management System With Entity Framework And ASP.NET MVC

Image
Hello, Here's a simple Task Management System with Entity Framework 6 taken from Udemy's Asp.Net MVC With Entity Framework From Scratch video tutorial. The output of the project should be in ASP.NET Webform but I chose to upload a sample in ASP.NET MVC which is intended for ASP.NET MVC developers. The entire source code can be downloaded here: Task Management System MVC . The project includes the stored procedure necessary to display the data through the grid. As for the database table, it is included in the tutorial series through a PDF file. Output Cheers!

Ajax.ActionLink() Not Redirecting To ActionResult With Ajax Attribute

Given that I have this code in my .cshtml page using Ajax.ActionLink() that calls a controller method using Ajax request: @Ajax.ActionLink( "Select" , "TaskListing" , new { id = item.TaskID }, new AjaxOptions (){ HttpMethod = "GET" , UpdateTargetId = "taskListing" , InsertionMode = InsertionMode.Replace }) And the controller method called by the Ajax ActionLink is decorated with Ajax attribute. [HttpGet] [AjaxOnly(true)] [ActionName("TaskListing")] public ActionResult TaskListing_Ajax ( int id = - 1 ) { var projects = projRep.GetAllProjects(); var model = new TaskAndTaskViewModel(); model.Task = te.Tasks.FirstOrDefault(t => t.TaskID == id); model.SelectList = from p in projRep.GetAllProjects() select new SelectListItem { Selected = (p.ProjectID == Convert.ToInt32(model.Task.ProjectID)), Text = p.ProjectName.ToString(), Value = p.ProjectID.ToString() ...

Read .NET Configuration Files Using NameValueSectionHandler And AppSettingsSection Types In C#

Hello all, I've read from a tutorial NameValueCollection and .NET Configuration Files which targets .NET 1.0/1.1 on how to read config files using type NameValueSectionHandler. I intend to explore more on applying this concept to recent versions of .NET frameworks. Upon doing some diggings, I came up with two options. First is using NameValueSectionHandler type and the other one is AppSettingsSections. To begin with, I have this App.config file with XML elements scriptsfiles and docfiles all registered in configSections. <?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name= "scriptfiles" type= "System.Configuration.NameValueSectionHandler" /> <section name= "docfiles" type= "System.Configuration.AppSettingsSection" /> </configSections> <scriptfiles> <add key= "C:\batchfiles\2017" value= "*.bat" />...

Cannot convert type 'System.Configuration.ConfigurationSection' to 'System.Collections.Specialized.NameValueCollection'

Image
Good evening! I've tried casting the config object to NameValueCollection using .NET Framework 4.5.2 of Visual Studio 2015 which I read from a tutorial on how to read config files using type NameValueSectionHandler . However, as I use the code below to cast the ConfigurationSection object to NameValueCollection, C# Code Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); NameValueCollection scriptFiles = (NameValueCollection)config.GetSection( "scriptfiles" ); I get an error which is the title of this post. It seems that the code was applicable to older versions of .NET Framework and not the recent ones. After doing some research, I found out that the workaround is to use ConfigurationManager.GetSection() in which the parameter passed is the SectionName of the config object instead of directly casting the ConfigurationSection object returned by the config.GetSection() method. C# Code Configuration config = Configurati...

Call Stored Procedures From Entity Framework In ASP.NET MVC

Image
Good day! Here's an ASP.NET MVC example of a CRUD(Create/Update/Delete) application using stored procedures and Entity Framework 6.First, you need to perform steps 1-3 from this link Call Stored Procedures from Entity Framework 6 in C# (Part 1) . For step 3, instead of creating a console application use ASP.NET MVC Empty project. Once done, the code for the controller and views are shown below: CustomersController private CustomerEntities db = new CustomerEntities(); // GET: Customers public ActionResult Index() { return View(db.Database.SqlQuery<Customer>( "GetAllCustomers" ).ToList()); } // GET: Customers/Create public ActionResult Create() { return View(); } // POST: Customers/Create [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create([Bind(Include = "CompanyName,ContactName,Address,Country,Phone" )] Customer customer) { if (ModelState.IsValid) { db.Database.ExecuteSqlCommand( "EXEC dbo.InsertCustomer @Compan...

Call Stored Procedures from Entity Framework 6 In C# (Part 2)

Hello, In this second part of the series on how to call stored procedures from Entity Framework 6, I will demonstrate on executing them using context.Database methods. To accomplish this, you need to perform steps 1 through 3 from this article Call Stored Procedures from Entity Framework 6 in C#. If you're done, the codes for the CRUD(Create, Update and Delete) functionalities are presented below. private static CustomerEntities ce = new CustomerEntities(); private static void InsertCustomer() { try { var result = ce.Database.ExecuteSqlCommand( "EXEC dbo.InsertCustomer @CompanyName,@ContactName,@Address,@Country,@Phone" , new SqlParameter( "CompanyName" , "TNT Bookstore" ), new SqlParameter( "ContactName" , "Mr T." ), new SqlParameter( "Address" , "Lincoln Village" ), new SqlParameter( "Country" , "UK" ), new SqlParameter( "Phone" , "42333...

Call Stored Procedures from Entity Framework 6 In C# (Part 1)

Image
Hello, Here's a tutorial on how to call stored procedures from Entity Framework 6.0 through the context object using the stored procedure name. In the second part of the series, I'll demonstrate how to call the stored procedures using methods like ExecuteSqlCommand() and SqlQuery() from context.Database class. To start with here are the steps to complete this example. Step 1 Add a Customers table in your database with fields. => CustomerID (int and identity set to true) => CompanyName(nvarchar) => ContactName(nvarchar) => Address(nvarchar) => Country(nvarchar) => Phone(nvarchar) Step 2 Create stored procedures that will perform insert, update, delete and get all records operations. Insert ALTER Procedure [dbo].[InsertCustomer]( @ CompanyName nvarchar( 40 ), @ ContactName nvarchar( 30 ), @ Address nvarchar( 60 ), @ Country nvarchar( 15 ), @ Phone nvarchar( 24 )) As Begin Insert Into dbo.Customers (CompanyName, ContactName, [Address...

Donate