Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have tried below code.

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Globalization; 

namespace DEMO
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void brwbut_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = openFileDialog1.FileName;
                     
            }


        }

        public void subbut_Click(String fileName, String tableName)
        {
            
            string connectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Mode=ReadWrite;Extended Properties=\"Excel 8.0;HDR=NO\"";
            string fieldstring  = "(ID int, Field1 char(255),Field2 char(255))";

            using (OleDbConnection conn = new OleDbConnection())
            {
                conn.Open();

                using (OleDbCommand cmd = new OleDbCommand())
                {
                    cmd.Connection = conn;
                    cmd.CommandText = string.Format(CultureInfo.InvariantCulture, @"CREATE TABLE [{0}] {1}", tableName, fieldstring);
                    cmd.ExecuteNonQuery();

                }
                conn.Close();

            }


        }

        public void InsertRow(String fileName, String tableName, string data)
        {
            string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Mode=ReadWrite;Extended Properties=\"Excel 8.0;HDR=YES\"";
            string headers = "ID,Field1,Field2";

            using (OleDbConnection conn = new OleDbConnection(connectionString))
            {
                conn.Open();
                using (OleDbCommand cmd = new OleDbCommand())
                {
                    cmd.Connection = conn;
                    cmd.CommandText = string.Format(CultureInfo.InvariantCulture, @"INSERT INTO [{0}$] ({1}) values({2})", tableName, headers, data);
                    //txtQuery.Text = cmd.CommandText;
                    cmd.ExecuteNonQuery();

                }
                for (int i = 0; i < 10; i++)
                {
                    InsertRow("C:\\path\\to\\file\\Test File.xls", "ListingDetails",
                        "'" + i.ToString() + "','test" + (i + 2).ToString() + "','test" + (i + 5).ToString() + "'");
                }

                conn.Close();

            }
        }
    }
}


What I have tried:

But i keep getting an annoying error (No overload for 'button1_Click' matches delegate 'System.EventHandler') that I don't know how to approach. I have been searching online and trouble shooting for a while, and have not found any answers. Any suggestions would be appreciated.
Posted
Updated 2-May-16 18:56pm
Comments
Raje_ 3-May-16 0:54am    
Is subbut_Click(String fileName, String tableName) your button click event or just a normal method?
Philippe Mori 3-May-16 12:56pm    
Are you too lazy to show us the line where you have the error? And where is button1_Click code? You don't even show us any pertinent information here? We cannot read code in your computer. Show us the code where the error is reported and the function that does not match. With so little pertinent information, you question does not even worth one star...

You should have a method called button1_Click with the signature like below
C#
public void button1_Click(object sender, EventArgs e)
{
    // Some code
}


Have a look inside the file Form1.Designer.cs and search for button1 and you will probably see the error.

You cannot change the names or signatures of event methods without making changes where they are referenced.
 
Share this answer
 
Go to Form [Design], Click the button(button1) -> right click -> Properties -> Click Events Tab on the top -> Find the Click Event and Double click it. Now it will go the associated event to the button in Form1.cs File
(or)
Double Click the Button in the Form[Design], it will go to its event in Form1.cs File

now replace the current button click event signature
C#
private void button1_Click()

to this
C#
private void button1_Click(object sender, EventArgs e)
 
Share this answer
 

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