IT Community - Software Programming, Web Development and Technical Support

please tell me how to import excel data to sql server without datagrid

This is a discussion on please tell me how to import excel data to sql server without datagrid within the C# Programming forums, part of the Software Development category; Can any of u tell me how to import excel data to sql.i have used datagrid to display the ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > C# Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 12-16-2007, 09:27 PM
oxygen oxygen is offline
D-Web Architect
 
Join Date: Jun 2007
Posts: 633
oxygen is on a distinguished road
Default please tell me how to import excel data to sql server without datagrid

Can any of u tell me how to import excel data to sql.i have used datagrid to display the excel data before importin to sql. Now i neednot want datagrid (it should not be displayed during runtime)i should create a object instead to upload the data to sql.else is there any option to deactivate datagrid during run time?The code i have used is;

private void Button1_Click(object sender, System.EventArgs e)

{
string sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Path.GetFullPath(File1.PostedFile.FileNam e)+";Extended Properties=Excel 8.0";OleDbConnection objConn = new OleDbConnection(sConnectionString);

objConn.Open();
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter("SELECT * FROM [Sheet1$]",objConn);


DataSet objDataset1 = new DataSet();



objAdapter1.Fill(objDataset1);

DataGrid1.DataSource = objDataset1.Tables[0].DefaultView;

DataGrid1.DataBind();

objConn.Close();


foreach(DataGridItem dg in DataGrid1.Items)

{
string strmerid=dg.Cells[0].Text;

string strtransid = dg.Cells[1].Text;
string strcashdate = dg.Cells[2].Text;

string strfirstname = dg.Cells[3].Text;
string strsurname = dg.Cells[4].Text;

string strcustomeremail = dg.Cells[5].Text;
string stramt = dg.Cells[6].Text;

string strcreditcardno = dg.Cells[7].Text;
string strcreditcardexp = dg.Cells[8].Text;

string strcurr = dg.Cells[9].Text;
string strcountry = dg.Cells[10].Text;


SqlCommand cmdinsert=new SqlCommand("insert into excelDB values('"+strmerid+"','"+strtransid+"','"+strcashd ate+"','"+strfirstname+"','"+strsurname+"','"+strc ustomeremail+"','"+stramt+"','"+strcreditcardno+"' ,'"+strcreditcardexp+"','"+strcurr+"','"+strcountr y+"')",con1);

cmdinsert.ExecuteNonQuery();

RegisterStartupScript("display","<script language=javascript>alert('Excel Records Inserted')</script>");

cmdinsert.Dispose();
__________________
The OXYGEN
Delivers edgy, intelligent Technology to all...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-16-2007, 09:31 PM
Balasubramanian.S Balasubramanian.S is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 182
Balasubramanian.S is on a distinguished road
Default Re: please tell me how to import excel data to sql server without datagrid

In your code you have written

foreach(DataGridItem dg in DataGrid1.Items)

{ -------------- }

Instead of this just Write following code in place of the above code

foreach (DataRow dr in objDataset1.Tables[0].Rows )

{

string strmerid=dr[0].ToString();

string strtransid = dr[1].ToString();

------ }

Now you will not need datagrid .You can remove datagrid related code.

Here you are directly accessing the Data Set.
__________________
S.Balasubramanian
Nothing is impossible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 12-16-2007, 09:33 PM
oxygen oxygen is offline
D-Web Architect
 
Join Date: Jun 2007
Posts: 633
oxygen is on a distinguished road
Default Re: please tell me how to import excel data to sql server without datagrid

Thanks..

Exactly works in the same manner which i want!!Thanks alot.
__________________
The OXYGEN
Delivers edgy, intelligent Technology to all...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Merging two Data Tables and displaying in DataGrid S.Vinothkumar VB.NET Programming 8 03-27-2008 02:37 AM
Copy SQL data structures from one SQL server to another SQL server? arjkhanna Server Management 5 11-05-2007 03:57 AM
Import TFS 2005 projects from one server to a clean TFS 2008 install on another serve satheesh Server Management 1 10-17-2007 03:58 AM
How do we import or export data in MySql Jeyaseelansarc Database Support 8 08-17-2007 07:25 AM
How to read data from excel files in webload? devarajan.v Software Testing 0 07-16-2007 04:07 AM


All times are GMT -7. The time now is 01:47 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0