Quote of the day

Friday, June 27, 2014

Monday, June 24, 2013

Datatable to json string in c#


I had a requirement to convert a datatable into json string. I saw couple of code samples in stackoverflow and code project, but I wanted to write of my own to make it simple. Below is the code


private static string DataTableToJSON(DataTable table)
    {
        string delimiter = string.Empty;
        string comma = string.Empty;
        StringBuilder json = new StringBuilder("[");
        foreach (DataRow row in table.Rows)
        {
            json.Append(delimiter);
            json.Append("{");
            foreach (DataColumn col in table.Columns)
            {
                json.Append(comma);
                json.Append("\"" + col.ColumnName + "\":\"" + row[col.ColumnName].ToString() + "\"");
                comma = ",";
            }
            json.Append("}");
            comma = string.Empty;
            delimiter = ",";
        }
        json.Append("]");
        return json.ToString();
    }

Same code is given as image below:



Monday, March 25, 2013

Could not find a base address that matches scheme net.tcp for the endpoint with binding NetTcpBinding. Registered base address schemes are [http]



I got the error

Could not find a base address that matches scheme net.tcp for the endpoint with binding NetTcpBinding. Registered base address schemes are [http].

sometime back and was google-ing for solution. Though the points and solutions which is mentioned for this issue was already setup in my machine, I still was getting the error. Then I found, there was a space after (,) comma in the "EnabledProtocols" under "Behavior" category in IIS -> myWebSite -> Advanced Settings for the net.tcp. Please refer to the below image


And we get the following error, when I try to execute the SVC file.

Later, I modified the protocol and removed the SPACE in the EnabledProtocols.


Below is the one which worked.

Online TFS

Microsoft provides TFS online. Here we can save the projects online. Now there may be no mandator requirement to have a dedicated server for saving the projects. Microsoft provides it online for FREE and it is for limited period.

                   This is something similar to GitHub, but not open source ;)

You can find more info here http://tfs.visualstudio.com/

You need to have a Windows Live ID to sign up for this.

Friday, August 17, 2012

prevent $(document).ready to load the functions on every post back

today, i was going through the jquery selectors section and tried an animation using recursion. Using call backs, i was able to do the "blinking" of text in a lable. When a button is clicked, I update the lable with current date and time (new Date()), but the default text was set again after the click and new Date display. When I went through forums, I found a solution. ie., using "e.preventDefault()" to be called in the click event. Using that prevented me to call the $(document).ready every time any event occurs in the page....



 $(document).ready(function () {

            
            BlinkLabelSample("lblSample");

        });
function BlinkLabelSample(elem) {
            var obj = $("#" + elem);
            obj.fadeIn("1000", function () {
                $(this).fadeOut("1000");
                BlinkLabelSample("lblSample");

            });
        }

$("#btnAnimationChange").live("click", function (e) {
    $(":animated").text(new Date());
    e.preventDefault();
});


Tuesday, March 13, 2012

Team foundation server working folder is already in use error

Today I faced an issue in TFS when I tried to Map the project. It said that TFS is already mapped to the c:\ drive and did not let me to map in any of the c drive's folder. I even tried to remove all the workspaces created but still it did not allow. Then I went through the following link which helped me to resolved the problem.

The issue was, we have Virtual machines which may be used by someother person in past and that user might have created a workspace mapping to local drive. I used a software called "Attrice (below link has the link to that software)", which will list all the worspaces "by user", "by date", "by machine name". When I tried to query "by machine name" and "by past date (a year back)", it listed me the old workspace which was mapped by the other user. I deleted and it worked!!!!

http://blogs.msdn.com/b/davidmcg/archive/2007/02/21/team-foundation-server-working-folder-is-already-in-use.aspx

Tuesday, February 14, 2012

jquery tooltip sample

my friend Ohm Ganesh asked for a tooltip sample in jQuery. I used a jquery plugin from http://flowplayer.org/tools/demos/tooltip/index.html

You can download the tooltip image from the above mentioned site itself. If not required, you can remove the "backgroud" property in the css class.

aspx with javascript in it:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="jTooltip.aspx.cs" Inherits="jTooltip" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.tooltip
{
display: none;
background: transparent url(_assets/images/black_arrow.png);
font-size: 12px;
height: 70px;
width: 160px;
padding: 25px;
color: #fff;
}
</style>

<script src="_assets/scripts/jquery/jquery-1.7.1.js" type="text/javascript"></script>

<script src="_assets/scripts/jquery/jquery.tools.min.js" type="text/javascript"></script>

<script type="text/javascript">
$(document).ready(function(){
$("#lblText").tooltip();
$("#tblSample tr").each(function(){
$("td:eq(2)", this).tooltip();
});
});
</script>

</head>
<body>
<form id="form1" runat="server">
<div>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<asp:Label ID="lblText" runat="server" Text="Hello World" ToolTip="this is tool tip" />
Please hover on the<h2> column 3</h2> to see the tooltip
<table id="tblSample" border="1">
<tr>
<td>
1
</td>
<td>
achu
</td>
<td title="they call him 'super'">
madurai
</td>
</tr>
<tr>
<td>
2
</td>
<td>
ohm
</td>
<td title="he is from a village">
kovilpatti
</td>
</tr>
<tr>
<td>
3
</td>
<td>
kathir
</td>
<td title="he is sumaaaar guy only">
karur
</td>
</tr>
<tr>
<td>
4
</td>
<td>
ranjith
</td>
<td title="good leader we had">
kerala
</td>
</tr>
<tr>
<td>
5
</td>
<td>
jegan
</td>
<td title="very good moulded person">
chennai
</td>
</tr>
</table>
</div>
</form>
</body>
</html>

Wednesday, December 21, 2011

Thursday, September 08, 2011

prevent post back on asp.net button click










<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>hello world</title>

<script type="text/javascript">
function helloWorld(){
var firstAlert = "This is my first alert function called. I'm happy to learn Javascript.... happy programming";
alert(firstAlert);
return false;
}
</script>

</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnSample" runat="server" Text="Click Me" OnClientClick="helloWorld();" />
</div>
</form>
</body>
</html>

There are 2 types of controls in asp.net.

1. Server Side Controls - which is to call c# or vb functions and to perform server side operations like to call database, use dataTable, dataSet, etc

2. Client Side Controls - to perform client side operations


The above code uses asp.net Button which alerts a text in javascript. But if you note, after the alert, the request goes to the server because it is a server control. All server controls by default go to the server even if there is no event written for it. How to prevent this?

You can prevent an asp.net button hitting the server using javascript "return false" method. We have a property in asp.net button "OnClientClick" where we call the javascript method. We can return false in the method which it is calling so that there won't be any post back occured.

In the above example, "helloWorld" method alerts and finally returns false, which prevents to hit the server. You can also write this "return false" in the method property after calling the "helloWorld()" method as below:

<asp:button id="btnSample" runat="server" text="Click Me" onclientclick="helloWorld(); return false"></asp:button >



happy telling,
achu

Thursday, January 06, 2011

print * in vertical

I remember one day in the year 2006, my cousin Senthil Kumaran asked me to write a program in C language to print *(stars) vertically like below:

Ha ha, really I didn't even try or know actually. Later I learned few programming languages of my own and got help from others including Senthil dha.
I have a little free time these days and was thinking to do something useful. So started writing string manipulating programs using c#. Today, this vertical star striked in my mind. As I don't have Turbo C or C++, I did it in Microsoft C#.

Below is the code for printing the *s vertically based on the numbers entered in the textbox:
Output:

My output has '+' instead of ' ' (space), because I've used asp.net label to display the stars and it does not accepts multiple spaces.However, it supports in c# console application.

My trick was to print spaces before printing * in each line till the count. For example, if the number entered is 5, the first line would print 4 spaces and then a *, the next line would print 3 spaces and then 2 *s and so on.
Console application code:


Console application output:

I'm not sure about other ways of printing. If anybody knows, then let me know.



Wednesday, January 05, 2011

swapping numbers without temp variable in c#

We all know, swapping numbers with temp variable is possible and easy. Recently I just rememberd of one of the interview questions "swap 2 numbers without using temp variable". I started thinking and did that in c#.

Following is the code which swaps 2 numbers without using temp variable


Output:

I was struggling by trying with different formulae, but nothing worked. I didn't think that we can recalculate first variable for resulting the output.
I got the idea from this post answered by this member.

Tuesday, January 04, 2011

fibonacci series using c#

Following code generates fibonacci series using c#. The output is displayed in asp.net Label. The textbox gets the no. of fibonacci series.


Output

take distinct values using c#

Following code displays only the distinct values entered in a textbox using (,)comma separator.

Output:

super happy coding.....

reverse words in a sentence using c#

Following code reverses the words in a sentence. It is done in c#. I've used asp.net Label to display the output.


Output:

Friday, December 24, 2010

jQuery method in common javascript file for asp.net

Today, I faced an issue with "object required" error in javascript in my aspx pages. I'm working on the enhancement part of my project. As I'm curious of using jQuery, I implemented it in the page which I developed. Since there was only one javascript common file written for the whole project, I created events for buttons and other controls in that file.
I inherited common javascript file in my page and everything was working fine. But in other pages it was throwing "object required" error which is not suppose to happen. When I debugged the application, I got error in the jQuery events which I created.
The reason is, when other pages are loaded, jQuery script is trying to register the events which I've written for the new page I created. I, then used try.. catch.. inside the event, but still error persisted. Then my slow brain recognized that the event registration is causing the error. So I used try.. catch.. for registering events itself. Cool, my problem solved then.
Below did the trick:
try {
$("#btnSample").live("click", function() {
//do something
});
}
catch (err) {
}
Since try.. catch.. is used, even if it throws error, it would be catched and the error won't display!

Alternatively, you can use window.onerror method to return true whenever an error occurs is any of the method in javascript, which is actually not recommended.

Friday, December 17, 2010

Free EBooks - legal

Now you can find most of the ebooks for free. Check the below link:

http://www.onlinecomputerbooks.com/

Wednesday, December 15, 2010

Exporting DataSet to Excel in asp.net problem




Last night I had an issue with exporting the asp.net dataset/datatable to excel. Requirement is to export the data into an Microsoft excel sheet. There are about 13 columns and few columns have to be in specific format. The problem was with formatting.
Couple of columns have to be in dd/MM/yyyy 00:00:00 format, so for example when the date is June 08, 2010, then the date should look like 08/06/2010 00:00:00. But for me it was displaying like 8/6/2010 which is not suppose to be. I googled for almost 2 hours for the solution, but nothing worked. Finally today I got an idea to use the formatting option available in excel sheet. When the data is passed from c# code, it can be concatenated with default excel function available.


I need to use the TEXT function in excel sheet so that it displays exactly what I send. I found the formatting options here http://www.techonthenet.com/excel/formulas/text.php

Used the following code for formatting the date as text:


Similarly, we can use the above code for formatting decimal numbers as well. Check the given link for decimal conversion as well.

Use the below code for decimal conversion. Eg: 123.44, 880.08, 334.30, 0.00







Monday, December 13, 2010

The EXECUTE permission was denied on the object 'storedProcedureName', database 'databaseName'

Today we deployed an updated version of our web application for my module. In the new module, I created a stored procedure which pull records based on the dates passed. It executed perfectly in my local machine, but when I deployed in our development/testing server, I got the following error:
The EXECUTE permission was denied on the object 'storedProcedureName', database 'databaseName'
We thought that the network\user (active directory user) doesn't have rights to execute the SP, so I contacted the DB team to give rights and it was given. But still I was getting the same error.
Later my onsite coordinator said that we are using service id in our application and that service id should be given rights/access to execute the Stored Procedure.
Service ID would be something like network\svc.yourServiceID
network - your active directory server

Access was given and we were able to execute the stored procedure!!

Wednesday, December 08, 2010

a connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed....

Hi,
Yesterday we had a problem calling 3rd party webserivce where we received the following error:
a connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond (my external IP address):(port)

I created a web proxy for Secured SSL in c#, but still was not able to establish the connection.

Earlier I created a sample console application which calls the webservice with the same code used in the original application. That console application was able to call the 3rd party webservice and got the result too! We almost wasted a day reasearching for this, though we knew there is some problem with the application configuration. No clue which configuration it was?
Actually, our application executes as tasks (like jobs in sql server) every 15 mins. There was the problem. We had to configure the tasks which permitted the application to call the 3rd party webservice. It worked then!!!

Wednesday, December 01, 2010

Maintain Scroll Position after Asynchronous Postback

Below code is used to maintain the scroll for grid or any other controls when it is posted back.