Click here to Skip to main content
15,910,130 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Iam getting error as 'OleDbCommand' does not contain a definition for 'ExecuteNonQueryAsync' and no extension method 'ExecuteNonQueryAsync' accepting a first argument of type 'OleDbCommand' could be found (are you missing a using directive or an assembly reference?)

Its coming for both ExecuteNonQueryAsyncpre> and OpenAsyn

What I have tried:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.OleDb;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExcelElement
{
    public class Staff
    {
        public int CustomerID { get; set; }
        public string Name { get; set; }
        public string Email { get; set; }
        public string Size { get; set; }
        public string Address { get; set; }
    }
    class ExcelDataService
    {

        OleDbConnection Conn;
        OleDbCommand Cmd;


        public ExcelDataService()
        {
            string ExcelFilePath = @"C:\Users\Pulztec-3\Desktop\\book1.xlsx";
            string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + ExcelFilePath + ";Extended Properties=Excel 12.0;Persist Security Info=True";
            Conn = new OleDbConnection(excelConnectionString);
        }

        /// <summary>  
        /// Method to Get All the Records from Excel  
        /// </summary>  
        /// <returns></returns>  
        /// 
        public async Task<ObservableCollection<Staff>> ReadRecordFromEXCELAsync()


        {
            
        ObservableCollection<Staff> Staffs = new ObservableCollection<Staff>();
         

            await Conn.OpenAsync();
            Cmd = new OleDbCommand();
            Cmd.Connection = Conn;
            Cmd.CommandText = "Select * from [Sheet1$]";
            var Reader = await Cmd.ExecuteReaderAsync();
            while (Reader.Read())
            {
                Staffs.Add(new Staff()
                {
                    CustomerID = Convert.ToInt32(Reader["CustomerID"]),
                    Name = Reader["Name"].ToString(),
                    Email = Reader["Email"].ToString(),
                    Size = Reader["Size"].ToString(),
                    Address = Reader["Address"].ToString()
                });
            }
            Reader.Close();
            Conn.Close();
            return Staffs;
        }

        /// <summary>  
        /// Method to Insert Record in the Excel  
        /// S1. If the EmpNo =0, then the Operation is Skipped.  
        /// S2. If the Student is already exist, then it is taken for Update  
        /// </summary>  
        /// <param name="Emp"></param>  
        public async Task<bool> ManageExcelRecordsAsync(Staff staff)
        {
            bool IsSave = false;
            if (staff.CustomerID != 0)
            {
                await Conn.OpenAsync();
                Cmd = new OleDbCommand();
                Cmd.Connection = Conn;
                Cmd.Parameters.AddWithValue("@CustomerID", staff.CustomerID);
                Cmd.Parameters.AddWithValue("@Name", staff.Name);
                Cmd.Parameters.AddWithValue("@Email", staff.Email);
                Cmd.Parameters.AddWithValue("@Size", staff.Size);
                Cmd.Parameters.AddWithValue("@Address", staff.Address);

                if (!IsStudentRecordExistAsync(staff).Result)
                {
                    Cmd.CommandText = "Insert into [Sheet1$] values (@CustomerID,@Name,@Email,@Size,@Address)";
                }
                else
                {
                    Cmd.CommandText = "Update [Sheet1$] set CustomerID=@CustomerID,Name=@Name,Email=@Email,Size=@Size,Address=@Address where CustomerID=@CustomerID";

                }
                int result = await Cmd.ExecuteNonQueryAsync();
                if (result > 0)
                {
                    IsSave = true;
                }
                Conn.Close();
            }
            return IsSave;

        }
        /// <summary>  
        /// The method to check if the record is already available   
        /// in the workgroup  
        /// </summary>  
        /// <param name="emp"></param>  
        /// <returns></returns>  
        private async Task<bool> IsStudentRecordExistAsync(Staff staff)
        {
            bool IsRecordExist = false;
            Cmd.CommandText = "Select * from [Sheet1$] where CustomerID=@CustomerID";
            var Reader = await Cmd.ExecuteReaderAsync();
            if (Reader.HasRows)
            {
                IsRecordExist = true;
            }

            Reader.Close();
            return IsRecordExist;
        }
    }
}
Posted
Updated 20-Jun-18 0:45am
v2

1 solution

If you look at the documentation: DbCommand.ExecuteNonQueryAsync Method (System.Data.Common)[^] it's only been available since .NET V4.5 - so I'd start by checking which target version you are building for.
 
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