Friday, May 14, 2010

Java Script function - Email Validation

Java Script function - Email Validation
function emailValidator(elem, helperMsg)
{
var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,20}$/;
if (elem.value != "")
{
if(elem.value.match(emailExp))
{
return true;
}
else
{
alert(helperMsg);
elem.focus();
elem.select();
return false;
}
}
}

Use of this function: you can call on onblur event of text box
onblur="emailValidator(this,'Invalid official mail id-1.')"


Java Script function to check numbers only..

Java Script function to check numbers only..

function onlyNumbers(evt)
{
var e = event evt; // for trans-browser compatibility
var charCode = e.which e.keyCode;
if (charCode > 31 && (charCode <> 57))
return false;
return true;
}

Call this function on button onkeypress event
onkeypress="return onlyNumbers();"

Thursday, May 13, 2010

Problem in uploading excel file

This is my method where I am sending my excel file add...
but in case of con open it is throwing exception "The file is allready open or permission problem"
it is only in the case when I am deplyoing my application in server . in local its working fine.


public DataTable GetTableFromExcel(string FileName)
{
try
{
//str = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FileName + ";User Id=;Password=;Extended Properties=Excel 12.0;HDR=NO;IMEX=1";
//str = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FileName + @";Mode=ReadWrite;ReadOnly=false;Extended Properties=""Excel 12.0;HDR=NO;IMEX=1""";
//str = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FileName + @";Extended Properties=""Excel 12.0;HDR=NO;IMEX=1""";
str = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FileName + @";Mode=ReadWrite;Extended Properties=""Excel 12.0;HDR=NO;IMEX=1""";

DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb");
using (connection = factory.CreateConnection())
{
connection.ConnectionString = str;
using (DbCommand command = connection.CreateCommand())
{
command.CommandText = "Select * from [Sheet1$]";
connection.Open();
excelda = new OleDbDataAdapter();
excelda.SelectCommand = command;
dt = new DataTable();
excelda.Fill(dt);
}
}

}
catch (Exception)
{
}
finally
{
connection.Close();
}
return dt;
}

Wednesday, May 12, 2010

Delegate with real life example

coming very soon.....

Displaying Grid View With empty Data Source.

Step1: Check your data source.
Step2: if in data source there is no record then send grid view control with in this method and change the name of grid view wherever it is necessary.

private void BindGridView(GridView GrdQuali)
{
//Check to ensure we have zero rows in our GridView
//and that our GridView has been assigned a DataSource
if (GrdQuali.Rows.Count == 0 && GrdQuali.DataSource != null)
{
//Create ourselves a DataTable to
//hold our "dummy" row
DataTable dt = null;
// need to clone sources otherwise it will be indirectly adding to
// the original source
//Check to see if the DataSource of our grid is a DataTable or DataSet
if (GrdQuali.DataSource is DataSet)
{
dt = ((DataSet)GrdQuali.DataSource).Tables[0].Clone();
}
else if (GrdQuali.DataSource is DataTable)
{
dt = ((DataTable)GrdQuali.DataSource).Clone();
}
//Check to ensure our DataTable object was assigned a value
if (dt == null)
{
return;
}
// now we add our empty row
dt.Rows.Add(dt.NewRow());
//Bind our GridView
GrdQuali.DataSource = dt;
GrdQuali.DataBind();
//hide our "dummy" row
GrdQuali.Rows[0].Visible = false;
GrdQuali.Rows[0].Controls.Clear();
}
}