In vs Exists in Sql Server

. Monday, September 19, 2011
0 comments

Hi Guys you've used lots of time In and Exists in Sql and they look almost same
but there is difference between both of these.

Suppose you have two tables as follows :

a)tbl_employee(id,emp_name,comp_id)
b)tbl_company(id,comp_name)

Now if you fire following query

select * from tbl_employee where comp_id in (select id from tbl_company)

by the way this query is executes in the following way :

select * from tbl_employee,(select distinct(id) from tbl_company) tbl_company
where tbl_employee.comp_id=tbl_company.id

What's the scenario Here:
a)the indexed result from tbl_employee,which is fast in terms of time.
b)a full scan through tbl_company for and selecting distinct of id from this,
for matching records in where condition.

Where's the IN is suitable:
well, in a scenario where your subquery having less record then IN suitable in that
case,because you main query have indexed and only need to have matched with subquery
while subquery need to have a full scan for each matching id with where condition.

Now if you fire following query

select * from tbl_employee where exists(select null from tbl_company where id=tbl_employee.comp_id)


query is executes in the following way :

for emp in ( select * from tbl_employee )
loop
if ( exists ( select null from tbl_company where id = emp.comp_id )
then
output the record
end if
end loop

What's the scenario Here:
a)indexed on tbl_employee which gives emp rowset.
b)for each emp rowset we need to check weather if exists is true or false if so output the record.


Where's the Exists is suitable:
well, in a scenario where your subquery having Huge records then Exists suitable ,because you only need to check with subquery that if the above record id exist or not and a full scan is made with main query for each rowset.









Display jquery dialog when Ajax calls in Asp.net MVC

. Wednesday, September 14, 2011
0 comments

If you want to display jquery dialog box when you post using Ajax there are various Jquery dialog available you can check them here.
For Now I am going to tell you how can you use jquery confirmation dialog when click on button which post to action in asp.net mvc using Ajax.

First We need to include neccessary libraries.
<pre name="code" class="js">
<script src="../../Scripts/jquery-1.6.2.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.bgiframe-2.1.2.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.ui.core.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.ui.widget.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.ui.mouse.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.ui.button.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.ui.draggable.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.ui.position.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.ui.resizable.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.ui.dialog.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.effects.core.js" type="text/javascript"></script>
</pre>
these are the necessary script need to include for calling jquery confirmation dialog.
in addition to this I added two css files to look dialog pretty.

<link rel="stylesheet" href="../../Content/demos.css">
<link rel="stylesheet" href="../../Content/jquery.ui.all.css">

after this we need to set Ajax in Asp.net MVC.

as we know Ajax is used for partial page refresh,which uses javascript.microsoft provides two beautiful js for ajax just include them in your page as follows:

<script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>

and for calling ajax using the following ajax extension methods with a dropdown and submit button to post to action using Ajax.
<% using(Ajax.BeginForm("GetTime",
new AjaxOptions { UpdateTargetId = "myResults" })) { %>
<p>
Show me the time in:
<select name="zone" id="zone">
<option value="utc">UTC</option>
<option value="bst">BST</option>
<option value="mdt">MDT</option>
</select>
<input type="submit" value="Go"  id="create-user"/>
</p>
<% } %>

<div id="myResults" style="border: 2px dotted red; padding: .5em;">
Results will appear here
</div>

in the above code when the submit is clicked an ajax call is made for the associated action which returns the response as string which will update the text inside div myResult.

for this my action is as follows:

 public ActionResult GetTime(string zone)
{
DateTime time = DateTime.UtcNow.AddHours(1);
if (Request.IsAjaxRequest())
{
// Produce a fragment of HTML
string fragment = string.Format(
"
The time in {0} is {1:h:MM:ss tt}
", zone.ToUpper(),time); return Content(fragment); } else { // Produce a complete HTML page return View(time); } }
well we need to write a little bit of script so that we can detect when submit button is clicked then we can call jquery to display confirmation dialog.  <script type="text/javascript">     var zone;     $(function() {         // a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!         $("#dialog:ui-dialog").dialog("destroy");  //display the dialog-form div as confirmation dialog.         $("#dialog-form").dialog({             autoOpen: false,             height: 300,             width: 350,             resizable: false,             modal: true,             closeOnEscape: true,             draggable: false,             show: 'slide',             stack: true,             buttons: { //the confirmation dialog will contain two button one is submit and other is cancel,if someone clicks on submit then will we post the data to specified url(in this case our action) and then receive the response and update the text inside myResult div.                 Submit: function() {                     var url = "/Home/GetTime/" + zone;                     $.post(url, function(data) {                         $('#myResults').html(data);                                            });                     $(this).dialog("close");                 },                 Cancel: function() {                     $(this).dialog("close");                 }             },             close: function() {             }         }); //this will use to detect wether "create-user" button has been clicked if so we need to display the div with Id "dialog-form"         $("#create-user")             .button()             .click(function() {                 zone = $("#zone").val();                 $("#dialog-form").dialog("open");                 return false;             });     });     </script> Here is the final screenshot which will appear when you click on submit button
















so when you click on Go button the confirmation dialog opens and when click on submit the content inside myresult div is updated with response given by the action using Ajax. Hope guys you enjoyed this article.

Unable to connect to Asp.Net Development Server

. Thursday, September 8, 2011
0 comments

Hi Guys, Cause of this problem is that your web server file of asp.net is corrupted.

Follow these steps to resolve this problem

1)download this file from here.


2)after downloading & unzipping ,overwrite this with existing corrupt webdev.webserver (an exe) file which is located at :
{your drive}:\Program Files\Common Files\Microsoft Shared\DevServer\9.0

XSS Problem with asp.net or in asp.net mvc

. Friday, September 2, 2011
0 comments

Hi Friends,

If you receive message like this :


A potentially dangerous Request.Form value was detected from the client


Whenever you write html or other scripting inside textbox or other html input field & when the form get posted on server the script is detected as security vulnerability(commonly known as XSS) and html inbuilt validation in applied on such scripts hence this message get generated which avoids script to run on server side.

but sometimes this could also be possible that you want to post such kind of script through input box.

so you can apply following things to Page directive in asp.net


validateRequest="false"


if you are using asp.net mvc then you can do by writing ValidateInput Attribute to "False" before your action as follows :


ValidateInput(false)
you action goes here...


also if you are using 3.5 or above framework then you need to specify version 2.0 in httpruntime inside web.config as follows:

<system.web>
    <httpruntime requestvalidationmode="2.0">
    </httpruntime>
</system.web>