Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

Error after making a request to Excel file

$
0
0

Hi everyone,

So, my app connects to Excel files via OpenFileDialog func, then app automatically gets all worksheets from the file and add them as values to combobox. Everything is good, but next step is "search". My app gives me an error "Invalid Argument" when i try to search an item from selected worksheet (from combobox).

Here is the code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
using Accord.IO; 



namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
     
        public static string path;
        public static string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;";
        OleDbConnection conn = new OleDbConnection(connStr);
        OpenFileDialog opf = new OpenFileDialog();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            showbtn.Enabled = false;
            searchbtn.Enabled = false;
            addmorebtn.Enabled = false;
        }

        private void openbtn_Click(object sender, EventArgs e)
        {
            opf.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm";
            if (opf.ShowDialog() == DialogResult.OK)
            {
                textBox3.Text = opf.FileName;
                path = opf.FileName;
            }
        }
        private void checkbtn_Click(object sender, EventArgs e)
        {
            try
            {
                if (conn != null && conn.State == ConnectionState.Closed)
                {
                    connlbl.Text = "Connected";
                    showbtn.Enabled = true;
                    Accord.IO.ExcelReader reader = new Accord.IO.ExcelReader(path);
                    string[] wsheets = reader.GetWorksheetList();
                    foreach (string worksheet in wsheets)
                    {
                        testcb.Items.Add(worksheet);
                    }

                }
                else
                {
                    connlbl.Text = "Not Connected";
                }
            }
            catch
            {
                connlbl.Text = "Not Connected";
            }
        }

        private void showbtn_Click(object sender, EventArgs e)
        {

                try
                {
                    OleDbDataAdapter da = new OleDbDataAdapter("Select * from [" + testcb.SelectedItem.ToString() + "$]", conn);
                    DataTable dt = new DataTable();
                    dataGridView2.DataSource = dt;
                    da.Fill(dt);                    
                    searchbtn.Enabled = true;

                }
                catch (Exception ex)
                {
                 
                    MessageBox.Show (ex.Message);
                }

        }

        private void searchbtn_Click(object sender, EventArgs e)
        {
            try
            {
                OleDbDataAdapter da = new OleDbDataAdapter("Select * from [" + testcb.SelectedItem.ToString() + "$] where [Well] = '" + textBox5.Text + "'", conn);
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGridView2.DataSource = dt;
            }
            catch
            {
                MessageBox.Show("SMTH went wrong");
            }
        }
    }
}

Thanks a lot.

Best regards, 

Adil Aliyev


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>