Wednesday, October 17, 2012

Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironmentException


Few days ago, I was working on a Windows Azure App, I created a Web Role  which was representing my application interface. Its purpose was to upload images. I needed to generate thumbnails as well.I thought to delegate this responsibility to a Worker Role. 
My Web Role  add image to a Blob and add a message in queue as well for the worker role, so that it can read it and generate thumbnails for each queued image, later.
When I created a worker role and tried to access the storage account with my defined connection string, it started giving me RoleEnvironmentException , I set the connecting-string for the application so I thought it will work for both Web ad Worker Roles, but it was not the case.
For Windows Azure, we have to set connection string for every worker and web roles, independently. I hope this will help any new bee on Windows Azure. 
Thank You and Happy Coding.......

Tuesday, October 16, 2012

Datatable belongs to another dataset


DataTable already belongs to another DataSet this error we get when we are trying to add table into datatset which is already belonging to another dataset. So whats the solution?Its simpleUse Copy() method of DataTable to add to your dataset e.g.

objDataSet.Tables.Add( dataTableFromDataSetA.Copy() ); 

//here objDataSetis your Dataset and dataTableFromDataSetA is DataTable.


Hope this post save your time. Thank You.

Tuesday, October 2, 2012

Jquery Tabs


Let me mention few simple steps to implement tab in Jquery :


First, make sure the HTML (ASPX) page you are creating is pointing at the stylesheet that contains the jQuery UI theme you are using.  The tabs make use of the themes and will not render as tabs unless the styles are pulled in.


Next you’ll need to create the HTML.  Everything that has to do with tabs should be wrapped in a DIV.  You can either give the ID a class or give it an ID, as long as you can select it later to apply the tabs() method that turns it all into a tabbed interface. Suppose our parent div is  


Within that DIV, you’ll place an unordered list:

<ul> <li><a href="#cricketTab">Cricket</a>

</li> <li><a href="#hockeyTab">Hockey</a></li> <li>

<a href="#footballTab"> Foot Ball </a></li> <li>

<a href="#basketballTab"> Basket Ball </a></li> </ul>

which represents your tabs.  Notice the href=”#id” stuff.  Those are pointing to ids of the DIVS that come next.

<div id="cricketTab">Cricket content</div> <
<div id="hockeyTab">Hockey content</div> <
<div id="footballTab"> Foot Ball content</div> <
<div id="basketballTab">Basket Ball content</div>

Next, in your jQuery code, select the outer DIV and call tabs()


$(function() {
$("#tabs").tabs();
});



By following this simple steps , tabs will be created.  For full documentation go to Jquery Tab. Thank You.

Tuesday, September 25, 2012

SET NOCOUNT Usage


SET NOCOUNT ON, in SQLSERVER, is used to stop the message that shows the count of the number of rows affected by the SQL statement written in the stored procedure or directly SQL Statement. You can view this message in the Management Studio in the Message tab of the result pan. 

When NOCOUNT is ON - the number of affected rows will not be returned 
 When NOCOUNT is OFF - the number of affected rows will be returned   

Triggers also need it, as its useful, If we don't turned on NOCOUNT, their performance can be drastically affected . Take, for example, INSERT triggers that are fired repeatedly, especially when using INSERT INTO statements for massive insert operations. In such cases where the trigger is fired over and over again during the course of the statement, the trigger will issue DONE_IN_PROC messages for each INSERT action, which can slow things down drastically. 

This slow down is especially pronounced if the trigger is being fired as the result of a scheduled SQL Server Agent job. SQL Server Agent automatically imposes a delay after each DONE_IN_PROC signal to avoid server congestion. If you try running the same set of commands through the Query Analyzer, it will execute much faster since no such delays are imposed. If you run such a query through Query Analyzer and see multiple "n rows affected" statements, there's a good chance the query is iterating repeatedly and re-firing the trigger many more times than it really needs to. To turn off DONE_IN_PROC messages, use the SET NOCOUNT ON command at the start of a trigger statement. 

 Microsoft actually encourages the use of SET NOCOUNT ON in Stored Procedures. 
 There is another thing @@ROWCOUNT, which is relevant to this. It is used to get the number of rows affected. Note that either the SET NONCOUNT is ON or OFF, @@ROWCOUNT is always updated with the number of rows affected.

 I hope this post help everyone in understanding the usage of SET NOCOUNT. Thank You.


Monday, September 24, 2012

Multiple Active Result Sets


SQL Server 2005 has so many new features , One of those is Multiple Active Result Sets or MARS. Multiple Active Result Sets is a new SQL Server 2005 feature that, putting it simply, allows the user to run more than one SQL batch on an open connection at the same time.

Pre-SQL 2005 era

In SQL Server's prior to 2005, you could only run one batch per connection. This means simply that you could only do this:
private void MARS_Off()
{
    SqlConnection conn = new SqlConnection("Server=serverName;
        Database=adventureworks;Trusted_Connection=yes;");

    string sql1 = "SELECT * FROM [Person].[Address]";
    string sql2 = "SELECT * FROM [Production].[TransactionHistory]";

    SqlCommand cmd1 = new SqlCommand(sql1, conn);
    SqlCommand cmd2 = new SqlCommand(sql2, conn);
    cmd1.CommandTimeout = 500;
    cmd2.CommandTimeout = 500;
    conn.Open();
    SqlDataReader dr1 = cmd1.ExecuteReader();
    // do stuff with dr1 data
    conn.Close();

    conn.Open();
    SqlDataReader dr2 = cmd2.ExecuteReader();
    // do stuff with dr2 data
    conn.Close();
}
And the accompanying profiler trace:
This example shows that you could use the same connection with the second SqlDataReader only when you finished using the connection with first one. The connection must be closed and reopened as it is shown with Audit Login and Audit Logout events. Opening and closing a connection is an expensive operation so this can hurt performance, even if your connection is stored in the connection pool.
If you for instance wanted to do some processing of the data in your data reader and updating the processed data back to the database you had to use another connection object which again hurts performance. There was no way to use the same opened connection easily for more than one batch at the time. There are of course server side cursors but they have drawbacks like performance and ability to operate only on a single select statement at the time.

SQL 2005 era

SQL Server 2005 team recognized the above mentioned drawback and introduced MARS. So now it is possible to use a single opened connection for more than one batch. A simple way of demonstrating MARS in action is with this code:
private void MARS_On()
{
    SqlConnection conn = new SqlConnection("Server= serverName;Database=adventureworks;
        Trusted_Connection=yes;MultipleActiveResultSets=true;");
    
    string sql1 = "SELECT * FROM [Person].[Address]";
    string sql2 = "SELECT * FROM [Production].[TransactionHistory]";

    SqlCommand cmd1 = new SqlCommand(sql1, conn);
    SqlCommand cmd2 = new SqlCommand(sql2, conn);
    cmd1.CommandTimeout = 500;
    cmd2.CommandTimeout = 500;
    conn.Open();
    SqlDataReader dr1 = cmd1.ExecuteReader();
    SqlDataReader dr2 = cmd2.ExecuteReader();
    conn.Close(); 
}
And the accompanying profiler trace:
MARS is disabled by default on the Connection object. You have to enable it with the addition of MultipleActiveResultSets=true in your connection string.

Wednesday, May 16, 2012

Refresh Rad Grid After Save Button Clicked 

Here is a sample code when save button is clicked then it refreshes the RAD Grid.

I am gonna put RAD Grid in panel.


<asp:Panel runat="server" ID="UsersPanel">
        <telerik:RadGrid AutoGenerateColumns="False" ID="RadGrid_Users" PageSize="10" AllowCustomPaging="true"
            EnableViewState="true" AllowFilteringByColumn="True" AllowPaging="True" AllowSorting="True"
            runat="server" Skin="Vista"
            OnNeedDataSource="RadGrid_Users_OnNeedDataSource">
            <PagerStyle Mode="NextPrevAndNumeric" />
            <GroupingSettings CaseSensitive="false" />

 <MasterTableView TableLayout="Fixed" DataKeyNames="User_id">

<NoRecordsTemplate>
                    <div>
                        No records to display.</div>
                </NoRecordsTemplate>
 <Columns>

<telerik:GridNumericColumn Aggregate="Count" HeaderText="User_id" DataField="User_id"
                        UniqueName="Customer_id" SortExpression="User_id" HeaderStyle-Width="50px" FilterControlWidth="50px"
                        AutoPostBackOnFilter="true" CurrentFilterFunction="equalto" ShowFilterIcon="true" FilterListOptions="VaryByDataType"
                        Display="false">
                    </telerik:GridNumericColumn>

 <telerik:GridBoundColumn HeaderText="Navn" DataField="Name" UniqueName="Name"
                        SortExpression="Name" HeaderStyle-Width="120px" FilterControlWidth="100px" FilterListOptions="VaryByDataType"
                        AutoPostBackOnFilter="true" CurrentFilterFunction="Contains" ShowFilterIcon="true">
                    </telerik:GridBoundColumn>

 </Columns>
            </MasterTableView>
            <ClientSettings EnablePostBackOnRowClick="true">
                <Selecting AllowRowSelect="True" />
            </ClientSettings>
        </telerik:RadGrid>      
        </asp:Panel>


  <telerik:RadButton ID="Button_Save" runat="server"  Text="Gem" Width="125px" OnClick="Button_Save_Click"
                        Skin="Office2007" />


In RadAjaxManager  define which control you want to update.Here give Control ID of panel.








<telerik:RadAjaxManager ID="RadAjaxManager1"  EnableAJAX="true"  runat="server" >
            <AjaxSettings>
                <telerik:AjaxSetting AjaxControlID="UsersPanel">
                    <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="UsersPanel"  LoadingPanelID="RadAjaxLoadingPanel1"/>
                   
                    </UpdatedControls>
                </telerik:AjaxSetting>


<telerik:AjaxSetting AjaxControlID="Button_Save" >
                    <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="UsersPanel" LoadingPanelID="RadAjaxLoadingPanel1"/>                    
                    </UpdatedControls>
                </telerik:AjaxSetting>


 </AjaxSettings>
 <ClientEvents OnRequestStart="RequestStart" OnResponseEnd="ResponseEnd" />

</telerik:RadAjaxManager>      
     
        <telerik:RadAjaxLoadingPanel runat="server" ID="RadAjaxLoadingPanel1"
            Skin="Windows7" Transparency="20" />



<telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
            <script type="text/javascript">
                function RequestStart(sender, eventArgs) {
                    var eventTarget = eventArgs.get_eventTarget();
                    if (eventTarget.indexOf("Button_Save_Click") != -1) {
                        eventArgs.set_enableAjax(false);
                    }

                }
                function ResponseEnd(sender, eventArgs) {
                }
            </script>
        </telerik:RadCodeBlock>


Have Fun ;)





Tuesday, April 24, 2012

Differnce between new and override

It is asked very frequently, that how can we call the parent version of a method in inheritance. In C# we can use new keyword to define a new method in child class and get this effect. I have mentioned a little example that will illustrate you the difference between override and new keyword :


//Calls Base class function 1 as new keyword is used.

BaseClass bd = new DeriveClass();

bd. function ();



//Calls Derived class function 2 as override keyword is used.

BaseClass bd2 = new DeriveClass();

bd2. function ();




Thank You. Happy coding....

Tuesday, April 17, 2012

Drag Events on IOS Devices using JQuery

These issues arise many times when you create a site e.g. on Asp.net with JQuery and some of the events doesn't work at all... Events like Drag drop, double click gestures.... 

We will talk about drag drop events in this case. Assuming that Drag drop events are working perfecting fine on the normal system browsers, here is the way to make it work for smart devices and tablets.

1. Add "touch-punch.min.js" file on your project.
2. Add the following line on your aspx page or control:

<script src="jquery.ui.touch-punch.min.js"></script>
 
3.  Now just add this line on the script portion

<script>
$('#widget').draggable();
</script>
 
 
That's it. You just need to add few lines and your website will now support drag drop gestures
on an iPhone/iPad/iPodTouch. 
 
 Happy coding!!!
 
Click on this link to download the script. 

Thursday, April 12, 2012

Images are not appearing in IIS 7-7.5

Today I have found a funny issue in my asp.net site deployment in IIS 7 on windows server 2008. After deploying my site, I was unable to see images. Initially, I though its a permission issue. I gave IIS_IUSR permission to deployment directory but nothing happened. I tried to reconfigure i.e. broswer settings but nothing turns in my way.

After that I started doing some RND towards IIS settings. I was amused to identify that in IIS, I didn't check static content under WWW Services and it was the reason that my images are not appearing. After enabling the following setting the images started appearing.I hope it would be helpful for you guys.. :)

Tuesday, February 14, 2012

Difference between Theme and StyleSheetTheme

Just like me, you may have wondered why there is theming options in ASP.NET - Theme and StyleSheetTheme. Understanding this difference may help you implement styling in your web page.

Theme overrides the styling applied by control attributes, on the other hand StyleSheetTheme do not override them.

Lets see an example of what is meant by the above line.

Say, we have a have a page Default.aspx which declares the page directive,

<%@ Page Theme="MyTheme" %>

If MyTheme contains a skin file which defines the style for default Label control



now if we use a label control in our Default.aspx page like below,



The label control's text will be in Blue color, becuase Theme always forcefully overrides the Styling attributes declared in the control level.

Now if we change the Page directive to use StyleSheetTheme,

<%@ Page StyleSheetTheme="MyTheme" %>

Now the label control font will be in Red as the StyleSheetTheme will no more override the inline style attribute.

If we look at the order in which the styling is applied we may get a more clear idea.

1. Theme attribute in the Page directive.
2. Theme declaration element in the Web.config file.
3. Local control attributes.
4. StyleSheetTheme attribute in the Page directive.
5. StyleSheetTheme element in the Web.config file.
Earn Money ! Affiliate Program
Open Directory Project at dmoz.org