export dynamic data from div to excel in asp.net using jquery and c#

In this tutorial you will learn how to export dynamically data from div to excel in asp.net using jquery and c#. I have already written a tutorial that teaches how to export div data to excel. Couple of days ago, one of my friends asked me to solve his problem and his problem was exporting div data to excel. I asked him to go to the above mentioned tutorial, get and use that code, it will solve your problem but when he tried, he failed to export div data to excel. He informed me that the code is not working. I was very surprised after hearing that and when I checked his code and found that the data inside the div was coming from database, it means div was populating dynamically. When I put some static data to div and tried to export then it was all OK, at that time it came in my knowledge that my code is not exporting dynamic data from div to excel. So to overcome this problem, I have applied another logic and became successful to export dynamically data from div to excel. Let's have a look over my .aspx page

Export dynamic data from div to excel in asp.net using jquery and c#


export-dynamic-data-from-div-to-excel.aspx
<form id="form1" runat="server">
<script language="javascript" type="text/javascript">
function ExportDivDataToExcel()
{
var html = $("#divExport").html();
html = $.trim(html);
html = html.replace(/>/g,'&gt;');
html = html.replace(/</g,'&lt;');
$("input[id$='HdnValue']").val(html);
}
</script>
<div id="divExport">
<table width="500" border="1" cellspacing="0" cellpadding="0">
<tr>
<td align="left" valign="top" bgcolor="#FFECF5"><strong>id</strong></td>
<td align="left" valign="top" bgcolor="#FFECF5"><strong>Name</strong></td>
<td align="left" valign="top" bgcolor="#FFECF5"><strong>Birth Date</strong></td>
<td align="left" valign="top" bgcolor="#FFECF5"><strong>Gender</strong></td>
</tr>
<tr>
<td align="left" valign="top">1</td>
<td align="left" valign="top">Adeel Fakhar</td>
<td align="left" valign="top">15/10/1984</td>
<td align="left" valign="top">male
</td>
</tr>
<tr>
<td align="left" valign="top">2</td>
<td align="left" valign="top">Omer Fakhar</td>
<td align="left" valign="top">01/09/1986</td>
<td align="left" valign="top">male</td>
</tr>
</table>
</div><br/>
<asp:HiddenField ID="HdnValue" runat="server" />
</form>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Export to Excel" OnClientClick="ExportDivDataToExcel()"/>

Let's suppose the table inside the div is populated dynamically from database. You may use<asp:GridView> inside div, it is up to you. Now let's have a look over the hidden field just before the </form>. Using that hidden field I am exporting dynamically div data to excel. In javascript custom function, i am using jquery to get all the inner html of the div, removing all the extra spaces by using built-in jquery trim() function and then replacing > to &gt; and < to &lt; so that data can be transfer to server without any html injection. After replacing the > to &gt; and < to &lt; I am storing all the div data to hidden field and when focus will go to server side then the function ExportToExcel() will export the div data to excel. ExportToExcel() function is self explanatory. In this function I am converting &gt; back to > and &lt; back to < and using Response.Header all the data is being exported to excel.

.cs
protected void Button1_Click(object sender, EventArgs e)
{
string html = HdnValue.Value;
ExportToExcel(ref html, "MyReport");
}


public void ExportToExcel(ref string html, string fileName)
{
html = html.Replace("&gt;", ">");
html = html.Replace("&lt;", "<");
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + fileName + "_" + DateTime.Now.ToString("M_dd_yyyy_H_M_s") + ".xls");
HttpContext.Current.Response.ContentType = "application/xls";
HttpContext.Current.Response.Write(html);
HttpContext.Current.Response.End();
}
So this is the proper way to export dynamic data from div to excel in asp.net using jquery and c#.
Read more...

Get the size of SQL Server Database and Tables

In this tutorial you will learn how to get the size of SQL Server database and its tables. It is a good approach to check the size of your database routine wise and then performed appropriate action with respect to analysis. In the following example we will find the size of a table.

Get the size of SQL Server Database and Tables

Getting the size of table

Syntax

EXEC SP_SPACEUSED 'YOUR TABLE NAME'


MS SQL Server contains a built-in system store procedure named SP_SPACEUSED. This store procedure is very handy. Using this store procedure you can find the size of the database as well as the size of the any table you want. When you execute the SP_SPACEUSED with table name as a parameter then it will provide you following information of that table.


1) name
2) rows
3) reserved
4) data
5) index_size
6) unused


The following example shows how to get the size of all tables from database


EXEC sp_MSforeachtable @command1="EXEC sp_spaceused '?'"



Getting the size of ms sql server database


To find the size of ms sql server database, you have to execute the SP_SPACEUSED store procedure without any parameter. It will automatically provides you the size of the database that you will be using.


Syntax


EXEC SP_SPACEUSED


I love your feedback
Read more...

Format currency using javascript

In this tutorial you will learn how to format currency using javascript. In javascript it is not difficult to format currency. By using examples given in this tutorial you can do these two things.
1) Insert commas between every third digit from right to left for numbers 1000 and greater. It will make numbers easier to read. The number 9,999,999.00 is easy to read as compare to 9999999.00


Input:-
1000.00


Output:-
1,000.00


2) Round numbers to the nearest hundredth, with two digits after the decimal point. It will help you when displaying the total price of the orders.


Input:-
9999.999


Output:-
10,000.00

Let's have a look over the first example that will insert commas between every third digit from right to left for numbers 1000 and greater.

Format the currency using javascript

Format-currency-using-javascript.html

<html>
<head>
<title>Format currency using javascript</title>
<script language="javascript" type="text/javascript">
//Javascript function to perform currency formatting
function CommaFormatted(amount)
{
var delimiter = ","; // replace comma if desired
var a = amount.split('.',2)
var d = a[1];
var i = parseInt(a[0]);
if(isNaN(i)) { return ''; }
var minus = '';
if(i < 0) { minus = '-'; }
i = Math.abs(i);
var n = new String(i);
var a = [];
while(n.length > 3)
{
var nn = n.substr(n.length-3);
a.unshift(nn);
n = n.substr(0,n.length-3);
}
if(n.length > 0) { a.unshift(n); }
n = a.join(delimiter);
if(d.length < 1) { amount = n; }
else { amount = n + '.' + d; }
amount = minus + amount;
return amount;
}


var amount="1000.00";
var formattedAmount=CommaFormatted(amount);
alert(formattedAmount);
</script>
</head>
</html>

At the top of the function, the comma character is assigned to variable delimiter. That's the comma between the quotation marks. If you want to use a different delimiter, replace the comma character with the character of your choice.


The function splits the amount on the basis of decimal point. The digits after the decimal point are stored in variable d.


The number in front of the decimal point is the entire amount, stored as an integer in variable i. If i is not a valid number, the function returns a null value.


Negative numbers are handled similar to the currency conversion function.


After then three digits are excluded from the end of the number and assigned to array variable a, so long as the number is longer than three digits. The number is then reformed using array a elements, with the delimiter digit inserted between each element.


Finally, the decimal portion is appended to the number, minus sign put in front, if appropriate, and the number returned.


Now formattedAmount variable contains the formatted currency amount.


Note: - This function can only accept amount that contains the decimal point like 1000.00, if you will provide amount 1000(No decimal point) to this function then it will not format the amount.


For those who wish to provide input of amount without any decimal point such as 1000 then they can refer to the below example in which I further customized the CommaFormatted() function according to our need.

<html>
<head>
<title>How to format currency in javascript</title>
<script language="javascript" type="text/javascript">
//Javascript function to perform currency formatting
function CommaFormatted(amount)
{
var delimiter = ",";
var i = parseInt(amount);
if(isNaN(i)) { return ''; }
var minus = '';
if(i < 0) { minus = '-'; }
i = Math.abs(i);
var n = new String(i);
var a = [];
while(n.length > 3)
{
var nn = n.substr(n.length-3);
a.unshift(nn);
n = n.substr(0,n.length-3);
}
if(n.length > 0)
{
a.unshift(n);
}
n = a.join(delimiter);
amount = n;
amount = minus + amount;
return amount;
}
var amount="1000";
var formattedAmount=CommaFormatted(amount);
alert(formattedAmount);
</script>
</head>
</html>

Now let's have a look over the second example that can round numbers to the nearest hundredth, with two digits after the decimal point. It will help you when displaying the total price of the orders.



<html>
<head>
<title>
Format currency in javascript
</title>
<script language="javascript" type="text/javascript">
function CommaFormatted(amount)
{
var delimiter = ","; // replace comma if desired
var a = amount.split('.',2)
var d = a[1];
var i = parseInt(a[0]);
if(isNaN(i)) { return ''; }
var minus = '';
if(i < 0) { minus = '-'; }
i = Math.abs(i);
var n = new String(i);
var a = [];
while(n.length > 3)
{
var nn = n.substr(n.length-3);
a.unshift(nn);
n = n.substr(0,n.length-3);
}
if(n.length > 0) { a.unshift(n); }
n = a.join(delimiter);
if(d.length < 1) { amount = n; }
else { amount = n + '.' + d; }
amount = minus + amount;
return amount;
}


function CurrencyFormatted(amount)
{
var i = parseFloat(amount);
if(isNaN(i)) { i = 0.00; }
var minus = '';
if(i < 0) { minus = '-'; }
i = Math.abs(i);
i = parseInt((i + .005) * 100);
i = i / 100;
s = new String(i);
if(s.indexOf('.') < 0) { s += '.00'; }
if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
s = minus + s;
return s;
}


var amount ="9999.999";
var formattedAmount = CommaFormatted(CurrencyFormatted(amount));
alert(formattedAmount);
</script>
</head>
</html>


The function CurrencyFormatted(amount) stores the amount in variable i as a floating decimal point number. It also uses parseFloat() in case the function is sent a string representing a number instead of number itself.


The next line checks the existence of valid number. If user sends function the value "hello world" would result in a non-number and as a response the function converts the non-number into "0.00", you can also prompt a message to the user to enter valid number.


After that, the function CurrencyFormatted(amount) determines whether the number is positive or negative, converts it to positive if necessary, and then rounds the number to the nearest hundredths.


Finally, it makes sure there are two digits following the decimal point and prepends the "-" character if it started as a negative number, and returns the result.


Now if user will provide the amount 9999.999 as an input then you will get 10,000.00 as an output.


So these are the examples to format the currency using javascript. I love your feedback.
Read more...