Thursday, April 9, 2009

Use of enterprise library to read excel sheet

Enterprise library is precompiled code provided by MicroSoft. Enterprise library provides inbuilt functions to do jobs which are common. Here I am giving code for reading excel sheet using Enterprise library.


The following sample code gives an idea to use enterprise library:

String path = @"filepath";

String ConnectionString = @"Data Source=" + path + ";Provider=Microsoft.ACE.OLEDB.12.0; Extended Properties=Excel 12.0;";

DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb");

using (DbConnection connection = factory.CreateConnection())

{

connection.ConnectionString = ConnectionString;

using (DbCommand command = connection.CreateCommand())

{

connection.Open();

command.CommandText = "SELECT * FROM [Sheet1$]"; //Sheet1 here is the name of the sheet which is to be read

using (DbDataReader reader = command.ExecuteReader())

{

while (reader.Read())

{

}

}

}

}

Wednesday, April 8, 2009

Creating custom prompt popup with javascript

Hello All:

There are cases when we need a customized JavaScript popup for getting user input.
On the net I found a good article on creating a JavaScript popup, here I am giving a customized version of mine which I used in one of my applications.

Javascript:


var ae_cb = null;
var isCLicked = false;
var isCancel = false;
function getObjectById(id) {
return document.getElementById(id);
}

function getValue(func,txtHeading) {
ae_cb = func;
var lblMessage = getObjectById('lblMessage');
lblMessage.innerText = txtHeading;
getObjectById('txtInput').value = "";
var div = getObjectById('mainDiv');
div.style.display = "block";
}

function getClick(click) {
var div = getObjectById('mainDiv');
div.style.display = "none";

if (!click) {
isCancel = true;
isCLicked = false;
ae_cb(null);
}
else {
isCancel = false;
ae_cb(getObjectById('txtInput').value)
}
}

function SetValue(valueSet) {
var hiddenF = getObjectById('hdnfValue');
if (parseFloat(valueSet) > 0) {
hiddenF.value = valueSet;
if(isCLicked)
getObjectById('LinkButton1').click();
}

else {
if (!isCancel) {
alert("Please enter number more than 0");
isCLicked = false;
}
}
}


function getCloneCount() {
isCLicked = isCLicked ? false : true;
if (isCLicked) {
getValue(SetValue, "Enter number of rows to be cloned");
}
else {
return true;
}
return false;
}


ASP Code:
Default.aspx:

<html xmlns="http://www.w3.org/1999/xhtml" >


<head runat="server">
<title></title>
<script src="Scripts.js" type="text/javascript"></script>
<link href="Stylesheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">LinkButton1</asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server">LinkButton2</asp:LinkButton>

<div id="mainDiv" class="timeSheetDiv" >
<table border="1">
<tr><td colspan="3" style="text-align:center"><label id="lblMessage"></label></td></tr>
<tr><td colspan="3" style="text-align:center"><input type="text" id="txtInput" /></td></tr>
<tr><td style="text-align:center"><input id="btnOk" type="button" value="Ok" onclick="getClick(1)" /></td><td>&nbsp;
</td><td style="text-align:center"><input id="btnCancel" type="button" value="Cancel" onclick="getClick(0)"/></td></tr>
</table>
</div>

<asp:HiddenField ID="hdnfValue" runat="server" />
</form>

</body>
</html>


Default.aspx.cs:

Modify
protected void Page_Load(object sender, EventArgs e)
{
LinkButton1.Attributes.Add("onclick", "return getCloneCount();");
}

protected void LinkButton1_Click(object sender, EventArgs e)
{
Response.Write(hdnfValue.Value);
}