Thursday, June 11, 2015

Search text in SQL Stored Procedure



#Declare an input text as given below.

DECLARE@SEARCH_TEXT VARCHAR(255)
SET@SEARCH_TEXT= 'nitindhiman@gmail.com’

#Query for Search Text in Stored Procedure

SELECT DISTINCT
OBJ.NAME AS OBJECT_NAME,
OBJ.TYPE_DESC
FROM SYS.SQL_MODULES MOD
INNER JOIN SYS.OBJECTS OBJ ON MOD.OBJECT_ID = OBJ.OBJECT_ID
WHERE MOD.DEFINITION LIKE '%'+@SEARCH_TEXT+'%'
ORDER BY TYPE_DESC


Tuesday, April 14, 2015

class does not contain a property with the name

Values in repeater are not displaying either via <%#Eval("Display") %>or <%#DataBinder.Eval(Container.DataItem, "Display")%>. Following is the class schema.


public class SwitchUser
{
    public string EmailId;
    public string Password;
    public string Display;

    public SwitchUser()
    {
    }

    public SwitchUser(string emailId, string password, string display)
    {
        EmailId = emailId;
        Password = password;
        Display = display;
    }

}
This is due to following public properties of class:
    public string EmailId;
    public string Password;
    public string Display;

Currently these are fields but not properties. Add getter and setter to these fields to make them properties. Following is the method to create properties.
    public string EmailId{ get; set; }
    public string Password{ get; set; }
    public string Display { get; set; }

This will resolve the error.


Monday, March 16, 2015

MVC : How to use RadioButtonFor


Following is the way to use Radio Button List in MVC:
1.       Create an ENUM for radio buttons list:
a.  public enum StatusList {
b.  Active = 0,
c.  Inactive = 1
d.      }
2.       Include ENUM in Model class:
a.       public StatusList Status { get; set; }
3.  Set Enum value in Controller:
a.  int val = 0;
b.      matchItem.Status = (StatusList)val;
4.  Use ENUM  in view:
a.  <div>@Html.RadioButtonFor(p => p.Status, Core.enums.StatusList.Active, new { onClick = "this.form.submit();" }) <strong>Show only Active</strong></div>
b.  <div>@Html.RadioButtonFor(p => p.Status, Core.enums.StatusList.Inactive, new { onClick = "this.form.submit();" }) <strong>Show only Inactive </strong></div>
5.       Get Enum value in Controller:

a.       int Status = (int)collection.Status;

Friday, February 20, 2015

Server.MapPath in MVC - HostingEnvironment.MapPath

Server.MapPath is throwing error in MVC? use HostingEnvironment.MapPath instead.

HostingEnvironment.MapPath needs to include to use System.Web.Hosting.

Following is the sample code:

var file = System.IO.File.AppendText(System.Web.Hosting.HostingEnvironment.MapPath("~/logs/error.txt"));

Tuesday, February 10, 2015

@Html.ActionLink("Application name", "Index", "Home", null, new { @class = "navbar-brand" }) object routevalues decoded

@Html.ActionLink("Application name", "Index", "Home", null, new { @class = "navbar-brand" })
4th Parameter in @Html.ActionLink is routeValues and of object type. This can be used to set query string and manage resulting anchor tag.

Following line will render an anchor tag with 3 query string parameters:
@Html.ActionLink("Application name", "Index", "Home", new { page = 1,name="my name",gogs="ola" }, new { @class = "navbar-brand" })

Result: http://localhost:34446/Home/Index/1?name=my%20name&gogs=ola



Similarly following code will return an anchor tag with id in url(not in query string parameter):

@Html.ActionLink("Application name", "Index", "Home", new { id = 1 }, new { @class = "navbar-brand" })

Result: http://localhost:34446/Home/Index/1