Wednesday 2 January 2013

Cascading dropdownlist using Jquery in MVC

Hii guys it's little difficult to do code for cascading dropdwon in asp.net mvc. In asp.net web forms We have a directly event of asp.net control. While in mvc we have html control and there is no event. So What we have to do we have to use Jquery event for this and make ajax call to our server side code and then bind the dropdown. Here below explain how to achieve this. First of all we have to import jquery so we can use jquery functions like we importing namespace in our C# code.

Here below is Jquery functions required to bind the dropdown list. Let me explain how these functions are working.
$('#Country').change this is jquery dropdown change event whatever code you write inside this it will run when country dropdown index is changed. Inside this event i am calling function GetState($(this).val())
In Getstate function i am calling one ajax method and calling action result "GetState" which is my C# code is written in controller named nik.
As you can see in action result i have argument name countryid so i can pass from here like this way data: { 'Countryid': Countryid }
Now with this country id i will return states in json format of the passed coutryid.
I also get city of selected state same way here is change event for state dropdwon $('#State').change


Here below is code you can see is that simple html code. But something strange you can find like VIEWBAG. View bag helps to maintain data when you move from controller to view. So here using view bag i am binding country dropdown. You can see in action result DropDown i am setting value of ViewBag. ViewBag.Country = items;
<%-- Here is dropdowns --%>
@{
    ViewBag.Title = "DropDown";
}

DropDown

Country : @Html.DropDownList("Country", (IEnumerable)ViewBag.Country, htmlAttributes: new { id = "Country"})
State :
City :

Here below you guys can see it is controller code. There are 3 different action result Dropdown,GetState,GetCity.
Let me explain when these all will fired.
1.Dropdwon :- This will fire when my page is load first time or when page is postback again.
2.GetState :- As i explain above it will fired by ajax method which i write in GetState(Countryid) jquery function.
3.GetCity :- Same as "GetState" it will fired by ajax method which i write in GetCity(Stateid) jquery function.
'Here is controller code

namespace MvcApplication1.Controllers
{
    public class NikController : Controller
    {
        public ActionResult DropDown()
        {       
            List items = new List();
                items.Add(new SelectListItem { Text = "---Select Country---", Value = "0", Selected = true });
                items.Add(new SelectListItem { Text = "India", Value = "1",Selected= true  });
                items.Add(new SelectListItem { Text = "USA", Value = "2" });
                items.Add(new SelectListItem { Text = "Russia", Value = "3"});
                items.Add(new SelectListItem { Text = "South Africa", Value = "4" });
                ViewBag.Country = items;            
                return View(); 
        }


        public ActionResult GetState(int Countryid)
        {
            List States = new List();

            if (Countryid==1)
            {
                States.Add(new SelectListItem { Text = "Gujarat", Value = "1", Selected = true });
                States.Add(new SelectListItem { Text = "Maharashtra", Value = "2"});
                States.Add(new SelectListItem { Text = "Rajsthan", Value = "3"});
                States.Add(new SelectListItem { Text = "Punjab", Value = "4"});
                States.Add(new SelectListItem { Text = "MP", Value = "5" });
                States.Add(new SelectListItem { Text = "Keral", Value = "6" });
                States.Add(new SelectListItem { Text = "Karnatak", Value = "7"});
                States.Add(new SelectListItem { Text = "Jammu", Value = "8"});
                States.Add(new SelectListItem { Text = "Assam", Value = "9"});
                States.Add(new SelectListItem { Text = "AP", Value = "10"});
            }                                                                                                    
            else if(Countryid ==2)
            {
                States.Add(new SelectListItem { Text = "Alabama", Value = "11", Selected = true });
                States.Add(new SelectListItem { Text = "Alaska", Value = "12"});
                States.Add(new SelectListItem { Text = "Arizona", Value = "13"});
                States.Add(new SelectListItem { Text = "Arkansas", Value = "14"});
                States.Add(new SelectListItem { Text = "California", Value = "15"});
                States.Add(new SelectListItem { Text = "Colorado", Value = "16"});
                States.Add(new SelectListItem { Text = "Connecticut", Value = "17"});
                States.Add(new SelectListItem { Text = "Delaware", Value = "18"});
                States.Add(new SelectListItem { Text = "Florida", Value = "19" });
                States.Add(new SelectListItem { Text = "Georgia", Value = "20"});
            }

            return Json(States);
        }


        public ActionResult GetCity(int Stateid)
        {
            List City = new List();

            if (Stateid == 1)
            {
       City.Add(new SelectListItem { Text = "Ahmedabad", Value = "1", Selected = true });
                City.Add(new SelectListItem { Text = "Surat", Value = "2" });
                City.Add(new SelectListItem { Text = "Baroda", Value = "3" });
                City.Add(new SelectListItem { Text = "Rajkot", Value = "4" });
                City.Add(new SelectListItem { Text = "Jamnagar", Value = "5" });
                City.Add(new SelectListItem { Text = "Junagadh", Value = "6" });
                City.Add(new SelectListItem { Text = "Jetpur", Value = "7" });
                City.Add(new SelectListItem { Text = "Bharuch", Value = "8" });
                City.Add(new SelectListItem { Text = "Limabi", Value = "9" });
                City.Add(new SelectListItem { Text = "Vapi", Value = "10" });
            }
            return Json(City);
        } 
    }
}
Here it is how it look likes.

Friday 14 December 2012

Track Alter stored procedure in Sql Server

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE trigger [Tracking_Alter_SP]
on database
for create_procedure, alter_procedure, drop_procedure,
create_table, alter_table, drop_table,
create_function, alter_function, drop_function
as set nocount on

SET ARITHABORT ON

declare @data xml
set @data = EVENTDATA()

insert into changelog(databasename, eventtype,
    objectname, objecttype, sqlcommand, loginname,hostname)
values(
@data.value('(/EVENT_INSTANCE/DatabaseName)[1]', 'varchar(300)'),
@data.value('(/EVENT_INSTANCE/EventType)[1]', 'varchar(60)'),
@data.value('(/EVENT_INSTANCE/ObjectName)[1]', 'varchar(300)'),
@data.value('(/EVENT_INSTANCE/ObjectType)[1]', 'varchar(50)'),
@data.value('(/EVENT_INSTANCE/TSQLCommand)[1]', 'varchar(max)'),
@data.value('(/EVENT_INSTANCE/LoginName)[1]', 'varchar(300)'),
HOST_NAME()
)
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO


Get Control in Gridview's Row command Event

<%-- Let us consider below is our grid.--%>

 
      
           
              

              
           
      
 

'Here is code behind.
Private Sub gridview_RowCommand(sender As Object, _

e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gridview.RowCommand
 
Dim btnActive As Button = DirectCast(e.CommandSource, Button)
Dim index As Integer = DirectCast(btnActive.Parent.Parent, GridViewRow).RowIndex
Dim chkmail As CheckBox = DirectCast(gridview.Rows(index).FindControl("chkmail"), CheckBox) 

'Here i get control like btnactive.parent.parent.It may be different for you.
'It depends how many controls you use in grid view. 

End Sub

Performance of CTE(Common table expression) Vs Hash(#)Tables.


Let say we have one report and in which we want data from around 20 tables.

so i just created some 4 to 5 cte(Common table expression) for every 4 to 5 tables.

and then join cte's and get the data.but when i execute that stored procedure it will take around 10 Minutes.

i am really strange about this.

Now what i do just replace all cte(Common table expression) with Hash(#)table.

and it's executed with only around 15 seconds.

So i suggest when cte 1 or 2 then ok but 4 to 5 then you can use #tables.

This is work for me.Let's you guys try it.if you use cte and it will take too much time to execute.

Friday 7 December 2012

Bind WebGrid in Asp.net MVC


I just learn how to bind grid in MVC. I hope you guys just know basic about MVC.
First of all You guys just have to install one package.for that got to Tools->Library Package Manager
and run command in output window
Command:- Install-Package RazorGenerator.Templating

1.) Below is controller code.
in above code i just create simple List of my model.you can also get that list from database also.

2.) Below is model code.


in above code simple properties are created.

3.) Below is view code.







Here is your view code. here one thing is that you have to take care is that is first line.i tried with @model MvcApplication1.Models.NiksModel but it means it accept model from controller.but here we are passing list from controller so you just have defined as list of your model.

Reference:
http://www.dotnetcurry.com/ShowArticle.aspx?ID=618


Wednesday 28 November 2012

View Last Excuted Quries in Sql Server


View Last Excuted Quries in Sql Server
SELECT DEQS.LAST_EXECUTION_TIME AS [TIME]
,SUBSTRING(DEST.TEXT,(DEQS.STATEMENT_START_OFFSET/2)+1
,((CASE DEQS.STATEMENT_END_OFFSET WHEN -1 THEN DATALENGTH(DEST.TEXT)ELSE DEQS.STATEMENT_END_OFFSET
END - DEQS.STATEMENT_START_OFFSET)/2) + 1) AS STATEMENT_TEXT
,DEST.*
FROM SYS.DM_EXEC_QUERY_STATS AS DEQS
CROSS APPLY SYS.DM_EXEC_SQL_TEXT(DEQS.SQL_HANDLE) AS DEST
ORDER BY DEQS.LAST_EXECUTION_TIME DESC

Thursday 24 May 2012

Comma seperated records in sql without loop or cursor

SELECT STUFF
(
(

SELECT ',' + T1.FULLNAME AS [text()]

FROM DBO.TABLE_1 AS T1

INNER JOIN DBO.TABLE_2 AS T2 ON T1.ID = T2.ID

FOR XML PATH('')
)
,1,1,''
)


DECLARE @COLS NVARCHAR(2000)

SELECT @COLS = COALESCE(@COLS + ',[' + CONVERT(NVARCHAR(100),T1.FULLNAME ) + ']','[' + CONVERT(NVARCHAR(100),T1.FULLNAME) + ']')

FROM DBO.TABLE_1 AS T1

INNER JOIN DBO.TABLE_2 AS T2 ON T1.ID = T2.ID