Thursday, 30 March 2017
MS SQL - Fix Error - Incorrect syntax near the keyword 'VIEW'.
Watch this example on YOuTube
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'VIEW'.
To fix it replace
IF EXISTS (Select * FROM sys.views where name='TestView')
ALTER VIEW [dbo].[TestView]
AS
SELECT dbo.Companies.*
FROM dbo.Companies
GO
with
IF EXISTS (Select * FROM sys.views where name='TestView')
Drop View [dbo].TestView
Go
CREATE VIEW [dbo].[TestView]
AS
SELECT dbo.Companies.*
FROM dbo.Companies
GO
C# - Fix Error - Cannot implicitly convert type 'bool?' to 'bool'.
Watch this example on YouTube
Error CS0266 Cannot implicitly convert type 'bool?' to 'bool'. An explicit conversion exists (are you missing a cast?)
To fix it replace
string test = "FALSE";
bool? test2 = null;
if (test2)
{
test = "OK";
}
with
string test = "FALSE";
bool? test2 = null;
if (test2 != null)
{
test = "OK";
}
Thursday, 23 March 2017
MVC - Database First - Display 2 models in 1 view
Watch this example on YouTube
1. Tables
CREATE TABLE [dbo].[Product](
[ProductID] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](50) NULL,
CONSTRAINT [PK_Product] PRIMARY KEY CLUSTERED
(
[ProductID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[ProductHistory](
[HistoryID] [int] IDENTITY(1,1) NOT NULL,
[ProductID] [int] NULL,
[PreviousName] [varchar](50) NULL,
[NewName] [varchar](50) NULL,
CONSTRAINT [PK_ProductHistory] PRIMARY KEY CLUSTERED
(
[HistoryID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
2.SP
CREATE PROCEDURE [dbo].[LoadParticularProduct]
@Id int
AS
BEGIN
SET NOCOUNT ON;
Select * From Product Where ProductID = @Id
END
GO
CREATE PROCEDURE [dbo].[LoadProductHistory]
@ProductID int
AS
BEGIN
SET NOCOUNT ON;
Select * From ProductHistory Where ProductID = @ProductID Order By ProductID
END
GO
CREATE PROCEDURE [dbo].[LoadProducts]
AS
BEGIN
SET NOCOUNT ON;
Select * From Product
END
GO
CREATE PROCEDURE [dbo].[UpdateProduct]
@ProductID int,
@ProductName varchar(50)
AS
BEGIN
SET NOCOUNT ON;
Declare @PreviousProductName varchar(50) = (Select Name From Product Where ProductID = @ProductID)
Update Product Set Name = @ProductName Where ProductID = @ProductID
Insert Into ProductHistory (ProductID, PreviousName, NewName) Values
(@ProductID, @PreviousProductName, @ProductName)
END
GO
3.New class in model
public class ProductList
{
public LoadParticularProduct_Result prod { get; set; }
public IEnumerable<LoadProductHistory_Result> hist { get; set; }
}
4. Controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVC2Model.Models;
namespace MVC2Model.Controllers
{
public class ProductController : Controller
{
CompanyEntities db = new CompanyEntities();
// GET: Product
public ActionResult Index()
{
var res = from s in db.LoadProducts()
select s;
return View(res);
}
public ActionResult Edit(int id)
{
ProductList pr = new ProductList();
pr.prod = db.LoadParticularProduct(id).FirstOrDefault();
if(pr.prod == null)
{
return HttpNotFound();
}
pr.hist = db.LoadProductHistory(id);
return View(pr);
}
[HttpPost]
public ActionResult Edit(int id, FormCollection col)
{
if (ModelState.IsValid)
{
db.UpdateProduct(id, col["prod.Name"]);
}
return RedirectToAction("Index");
}
}
}
5. View Index
@model IEnumerable<MVC2Model.Models.LoadProducts_Result>
@{
ViewBag.Title = "Index";
}
<table>
<tr>
<th>
@Html.LabelFor(model=>model.First().Name)
</th>
<th></th>
</tr>
@foreach(var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem=>item.Name)
</td>
<td>
@Html.ActionLink("Edit/View Product", "Edit", new { id=item.ProductID})
</td>
</tr>
}
</table>
6. View - Edit
@model MVC2Model.Models.ProductList
@{
ViewBag.Title = "Edit";
}
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<table>
<tr>
<th>
@Html.LabelFor(model=>model.prod.Name)
</th>
<th></th>
</tr>
<tr>
<td>
@Html.EditorFor(model=>model.prod.Name)
</td>
<td>
<input type="submit" value="save" />
</td>
</tr>
</table>
}
<table>
<tr>
<th>
@Html.LabelFor(model=>model.hist.First().PreviousName)
</th>
<th>
@Html.LabelFor(model => model.hist.First().NewName)
</th>
</tr>
@foreach(var item in Model.hist)
{
<tr>
<td>
@Html.DisplayFor(model=>item.PreviousName)
</td>
<td>
@Html.DisplayFor(model => item.NewName)
</td>
</tr>
}
</table>
Wednesday, 22 March 2017
MS Access - Fix Error - undefined function 'substring' in expression
Watch this example on YouTube
To fix it replace
Expr1: Substring([Name],1,2)
with
Expr1: Mid([Name],1,2)
To fix it replace
Expr1: Substring([Name],1,2)
with
Expr1: Mid([Name],1,2)
Saturday, 11 March 2017
MVC - Fix Error - Non-invocable member '' cannot be used like a method.
Watch solution on YouTube
Error CS1955 Non-invocable member 'Users_SelectAll_Result.LanID' cannot be used like a method.
Compiler Error Message: CS1955: Non-invocable member 'Users_SelectAll_Result.LanID' cannot be used like a method.
To Fix it replace
@Html.LabelFor(model => model.First().LanID())
With
@Html.LabelFor(model => model.First().LanID)
MVC - Fix Error - A namespace cannot directly contain members such as fields or methods
Watch solution on YouTube
Error CS0116 A namespace cannot directly contain members such as fields or methods
To fix it replace
namespace MVC2Models1View.Controllers
{
CompanyEntities db = new CompanyEntities();
public class HomeController : Controller
{
With
namespace MVC2Models1View.Controllers
{
public class HomeController : Controller
{
CompanyEntities db = new CompanyEntities();
Error CS0116 A namespace cannot directly contain members such as fields or methods
To fix it replace
namespace MVC2Models1View.Controllers
{
CompanyEntities db = new CompanyEntities();
public class HomeController : Controller
{
With
namespace MVC2Models1View.Controllers
{
public class HomeController : Controller
{
CompanyEntities db = new CompanyEntities();
MVC - Fix Error - Cannot convert method group 'DisplayFor' to non-delegate type 'object'.
Watch on YouTube
Error CS0428 Cannot convert method group 'DisplayFor' to non-delegate type 'object'. Did you intend to invoke the method?
Compiler Error Message: CS1503: Argument 1: cannot convert from 'method group' to 'HelperResult'
To Fix it replace (remove space)
@Html.DisplayFor (modelItem => item.LanID)
with
@Html.DisplayFor(modelItem => item.LanID)
MVC - Fix Error - Cannot convert method group 'LabelFor' to non-delegate type 'object'.
Watch this example on YouTube
Error
Error CS0428 Cannot convert method group 'LabelFor' to non-delegate type 'object'. Did you intend to invoke the method?
Compiler Error Message: CS1503: Argument 1: cannot convert from 'method group' to 'HelperResult'
To fix it replace (remove space)
@Html.LabelFor (model => model.First().LanID)
with
@Html.LabelFor(model => model.First().LanID)
Error
Error CS0428 Cannot convert method group 'LabelFor' to non-delegate type 'object'. Did you intend to invoke the method?
Compiler Error Message: CS1503: Argument 1: cannot convert from 'method group' to 'HelperResult'
To fix it replace (remove space)
@Html.LabelFor (model => model.First().LanID)
with
@Html.LabelFor(model => model.First().LanID)
Saturday, 4 March 2017
MVC - VIew - Get user role
Watch this example on YouTube
1. View
@if(Html.GetRole() == "Admin")
{
<h1>Hi admin!!!!</h1>
}
2. Controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MVCIntranetTest.Models;
namespace System.Web.Mvc
{
public static class SomeClassToAccess
{
public static string GetRole(this HtmlHelper html)
{
CompanyEntities db = new CompanyEntities();
string CurrentUser = HttpContext.Current.User.Identity.Name.ToString();
CurrentUser = CurrentUser.ToLower().Replace("living\\", "");
string role = db.GetUserRole(CurrentUser).FirstOrDefault();
return role;
}
}
}
3. Stored Procedure
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE GetUserRole
@LanID Varchar(50)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
SELECT UserRoles.RoleName
FROM Users INNER JOIN
UserRoles ON Users.RoleID = UserRoles.RoleID
WHERE (Users.LanID = @LanID)
END
GO
1. View
@if(Html.GetRole() == "Admin")
{
<h1>Hi admin!!!!</h1>
}
2. Controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MVCIntranetTest.Models;
namespace System.Web.Mvc
{
public static class SomeClassToAccess
{
public static string GetRole(this HtmlHelper html)
{
CompanyEntities db = new CompanyEntities();
string CurrentUser = HttpContext.Current.User.Identity.Name.ToString();
CurrentUser = CurrentUser.ToLower().Replace("living\\", "");
string role = db.GetUserRole(CurrentUser).FirstOrDefault();
return role;
}
}
}
3. Stored Procedure
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE GetUserRole
@LanID Varchar(50)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
SELECT UserRoles.RoleName
FROM Users INNER JOIN
UserRoles ON Users.RoleID = UserRoles.RoleID
WHERE (Users.LanID = @LanID)
END
GO
MVC - Simple example that shows how to access controller method directly from view
Watch this example on YouTube
1. Add to View
<h1>@Html.GetSomething()</h1>
2. In Controller add the following static class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace System.Web.Mvc
{
public static class SomeClass
{
public static string GetSomething(this HtmlHelper html)
{
return "This is something";
}
}
}
1. Add to View
<h1>@Html.GetSomething()</h1>
2. In Controller add the following static class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace System.Web.Mvc
{
public static class SomeClass
{
public static string GetSomething(this HtmlHelper html)
{
return "This is something";
}
}
}
MVC - Fix Error - The type or namespace name 'HtmlHelper' could not be found
Watch this example on YouTube
Error CS0246 The type or namespace name 'HtmlHelper' could not be found (are you missing a using directive or an assembly reference?)
To Fix it replace
namespace MVCIntranetTest.Controllers
{
public static class MyBusiness
{
public static string GetUserRole(this HtmlHelper html)
{
CompanyEntities db = new CompanyEntities();
string CurrentUser = HttpContext.Current.User.Identity.Name.ToString();
CurrentUser = CurrentUser.ToLower().Replace("living\\", "");
string Role = db.GetUserRole(CurrentUser).FirstOrDefault();
return Role;
}
}
}
with
namespace System.Web.Mvc
{
public static class MyBusiness
{
public static string GetUserRole(this HtmlHelper html)
{
CompanyEntities db = new CompanyEntities();
string CurrentUser = HttpContext.Current.User.Identity.Name.ToString();
CurrentUser = CurrentUser.ToLower().Replace("living\\", "");
string Role = db.GetUserRole(CurrentUser).FirstOrDefault();
return Role;
}
}
}
Error CS0246 The type or namespace name 'HtmlHelper' could not be found (are you missing a using directive or an assembly reference?)
To Fix it replace
namespace MVCIntranetTest.Controllers
{
public static class MyBusiness
{
public static string GetUserRole(this HtmlHelper html)
{
CompanyEntities db = new CompanyEntities();
string CurrentUser = HttpContext.Current.User.Identity.Name.ToString();
CurrentUser = CurrentUser.ToLower().Replace("living\\", "");
string Role = db.GetUserRole(CurrentUser).FirstOrDefault();
return Role;
}
}
}
with
namespace System.Web.Mvc
{
public static class MyBusiness
{
public static string GetUserRole(this HtmlHelper html)
{
CompanyEntities db = new CompanyEntities();
string CurrentUser = HttpContext.Current.User.Identity.Name.ToString();
CurrentUser = CurrentUser.ToLower().Replace("living\\", "");
string Role = db.GetUserRole(CurrentUser).FirstOrDefault();
return Role;
}
}
}
SQL Server - Fix Error -Must declare the scalar variable "@ID".
Watch this on YouTube
Msg 137, Level 15, State 2, Line 6
Must declare the scalar variable "@ID".
To Fix it - replace
Declare @ID int = (Select Top 1 ID From Customers Where [FisrtName] = 'Bob')
GO
Select @ID
with
Declare @ID int = (Select Top 1 ID From Customers Where [FisrtName] = 'Bob')
Select @ID
Thursday, 2 March 2017
Google Chrome - View Certificate Details
Watch on YouTube
Right click on page
Click Inspect
Select Secrurity
Click View Certificate
Wednesday, 1 March 2017
MVC - Add watermark effect to the TextBox
Replace
@Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })
With
@Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" , placeholder="Enter Last Name"} })
Subscribe to:
Posts (Atom)