Click here to Skip to main content
15,897,718 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private void btn_submit_Click(object sender, EventArgs e)
       {
           try
           {

               string name = textBox1.Text;
               string filepath="D://"+name;

               System.Data.OleDb.OleDbConnection MyConnection;
               System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand();
               string sql = null;
               MyConnection = new System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source="+filepath+".xls;Extended Properties=Excel 8.0;");
               MyConnection.Open();
               myCommand.Connection = MyConnection;
               sql = "Insert into [Sheet1$] (name,email) values('"+textBox1.Text+"','"+textBox2.Text+"')";
               myCommand.CommandText = sql;
               myCommand.ExecuteNonQuery();
               MyConnection.Close();
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.ToString());
           }
       }


What I have tried:

i'm trying to write data to excel file but its giving me an error as sheet1$ does not exists.I don't know what to do.please help
Posted
Updated 27-Aug-16 20:59pm

1 solution

Check your excel file: make sure that you haven't renamed the sheet.
And don't do it like that: Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead - SQL Injection isn't just confined to databases!
 
Share this answer
 
Comments
Member 10549697 28-Aug-16 3:29am    
yes i will use that and thanks for the answer.one more question can i create table for defining rows and columns same like this insert query?Please help me
OriginalGriff 28-Aug-16 3:41am    
That's a separate question - open a new one, and explain in detail what you are trying to do.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900