DotNet Blogs

Articles of ASP.NET, C# & SQL Server

Archive for September, 2008

export excel data into sql server using c#

Posted by Sujit Kumar on September 6, 2008

Condition :
excel file columns and sql server tables columns should be same and datatype also should be same.

public void Exel2Sql()
{
OdbcConnection connection;
SqlBulkCopy bulkCopy;
string ConnectionString = @”server=sujitkumar\sqlexpress;database=pubs;uid=sa;pwd=1234;”;
string connstr = @”Driver={Microsoft Excel Driver (*.xls)};DriverId=790;Dbq=c:\contact.xls”;
using (connection = new OdbcConnection(connstr))
{
OdbcCommand command = new OdbcCommand(“Select * FROM [Sheet1$]“, connection);

//you can change [Sheet1$] with your sheet name

connection.Open();

// Create DbDataReader to Data Worksheet

using (OdbcDataReader dr = command.ExecuteReader())
{
// Bulk Copy to SQL Server

using (bulkCopy = new SqlBulkCopy(ConnectionString))
{
bulkCopy.DestinationTableName = “Names”;
//”Names” is the sql table where you want to copy all data.

bulkCopy.WriteToServer(dr);
}
dr.Close();
}

}

bulkCopy.Close();
connection.Close();
}

Posted in asp.net | Tagged: , , , | 7 Comments »

Open popup window in center using javascript

Posted by Sujit Kumar on September 6, 2008

function popup(url)
{
var width = 450;
var height = 300;
var left = (screen.width – width)/2;
var top = (screen.height – height)/2;
var params = ‘width=’+width+’, height=’+height;
params += ‘, top=’+top+’, left=’+left;
params += ‘, directories=no’;
params += ‘, location=no’;
params += ‘, menubar=no’;
params += ‘, resizable=no’;
params += ‘, scrollbars=no’;
params += ‘, status=no’;
params += ‘, toolbar=no’;
newwin=window.open(url,’Chat’, params);
if (window.focus) {newwin.focus()}
return false;
}

How to use:
popup(‘http://www.yahoo.com’)

Posted in asp.net | Tagged: , | Leave a Comment »

Auto Refresh update panel on page using Timer

Posted by Sujit Kumar on September 6, 2008

private void BindGridView()
{
string connectionString = @”Server=sujitkumar\sqlexpress;Database=pubs;uid=sa;pwd=1234;”;

SqlConnection conn = new SqlConnection(connectionString);
SqlDataAdapter ad = new SqlDataAdapter(“SELECT * FROM names”, conn);

DataSet ds = new DataSet();
ad.Fill(ds);

gvName.DataSource = ds;
gvName.DataBind(); //gvName is Gridview
}

protected void timer1_tick(object sender, EventArgs e)
{
BindGridView()
up1.Update(); //up1 is updatepanel object.
}

Posted in asp.net | Tagged: , , , , | Leave a Comment »

Show div as tooltip using javascript

Posted by Sujit Kumar on September 5, 2008

function mouseXY(e)
{
var posx = 0;
var posy = 0;
if (e.pageX || e.pageY)
{
posx = e.pageX+5;
posy = e.pageY;
}
else if (e.clientX || e.clientY)
{
posx = e.clientX+5;
posy = e.clientY;
}

document.getElementById(“movingDiv”).style.right=posy+”px”;
document.getElementById(“movingDiv”).style.left=posx+”px”;
document.getElementById(“movingDiv”).style.top=posy+”px”;
document.getElementById(“movingDiv”).style.display=’block’;
}

function hideDiv()
{
document.getElementById(“movingDiv”).style.display=’none’;
}

window.onload=mouseXY;

here “movingDiv” is div.

Posted in asp.net | Tagged: , , , | 1 Comment »