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 ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| 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... |
| Sponsored Links |
| |||
| 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 |
| |||
| Thanks.. Exactly works in the same manner which i want!!Thanks alot.
__________________ The OXYGEN Delivers edgy, intelligent Technology to all... |
![]() |
| Thread Tools | |
| Display Modes | |
| |
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 |