3

i have read the other question with the same one without the OldeDb namespace in it. But I am still having a problem

I am creating a UWP app, and I want to upload a data from an Excel file to a DataGridView.

So this is my code

This is my reference code

using System.Collections.Generic;
using Windows.UI.Xaml.Controls;
using System.Data;
using Microsoft.Toolkit.Uwp.UI.Controls;
using System;
using System.IO;
using System.Data.OleDb

then this is my code on uploading my excel file

String constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fullDirectory + ";Extended Properties='Excel 12.0 XML;HDR=YES;';";

OleDbConnection con = new OleDbConnection(constr);
OleDbCommand oconn = new OleDbCommand("Select * From [" + "2018" + "$]", con);

And this is my error code

Error CS0246 The type or namespace name 'OleDbCommand' could not be found (are you missing a using directive or an assembly reference?)

Any ideas why this is occurring? Thanks

Full Code

using System.Collections.Generic;
using Windows.UI.Xaml.Controls;
using System.Data;
using Microsoft.Toolkit.Uwp.UI.Controls;
using System;
using System.IO;
using System.Data.OleDb;

namespace App
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        private void SaveButton_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {

        }

        private void CancelButton_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {

        }

        private void AppBarButton_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            if (this.Frame.CanGoBack)
            {
                this.Frame.GoBack();
            }
        }

        private async void BtnOpenAttendanceFileDialog_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            var picker = new Windows.Storage.Pickers.FileOpenPicker();
            picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.List;
            picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Desktop;
            picker.FileTypeFilter.Clear();
            picker.FileTypeFilter.Add(".xls");
            picker.FileTypeFilter.Add(".xlsx");
            picker.FileTypeFilter.Add(".dat");
            picker.FileTypeFilter.Add(".csv");


            Windows.Storage.StorageFile file = await picker.PickSingleFileAsync();
            if (file != null)
            {
                this.txtFileLocation.Text = file.Path;
            }
            else
            {

            }
        }

        private void BtnLoadFile_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            string fileDirectory = Path.GetDirectoryName(txtFileLocation.Text.Trim());
            string fileName = Path.GetDirectoryName(txtFileLocation.Text.Trim());
            string fullDirectory = txtFileLocation.Text.Trim();

            String constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fullDirectory + ";Extended Properties='Excel 12.0 XML;HDR=YES;';";

            OleDbConnection con = new OleDbConnection(constr);
            OleDbCommand oconn = new OleDbCommand("Select * From [" + "2018" + "$]", con);


        }
    }
}
19
  • 2
    Just to doublecheck. You do have a ; after using System.Data.OleDb in your own code? Commented Jan 15, 2019 at 8:05
  • 1
    @newboooooo Okay, 1. have you added oledb as a reference to your project in your solution? 2. Try pressing ctrl + . when you are standing on new OleDbConnection(constr); Does it say anything? 3. Try removing the using clause and do step 2 again. Commented Jan 15, 2019 at 8:16
  • Here it says imgur.com/a/mjyKZAU @Joel Commented Jan 15, 2019 at 8:19
  • @Joel I did the third step again, I also redid the whole project but still the same Commented Jan 15, 2019 at 8:21
  • 1
    Please check if you have these in your project imgur.com/a/mVRyyd0 Commented Jan 15, 2019 at 8:37

1 Answer 1

1

After some digging (which can be found in the comments) I've found that you can only use a small subset of .NET in a UWP application.

Creating a simple console-application will verify that there isn't a problem with your Visual Studio installation. Eg:

namespace App
{
    static void Main(string[] args)
    {
         using(var conn = new System.Data.OleDb.OleDbConnection{connString = "..info.."})
        {
             conn.Open();
             Console.WriteLine("DS:{0} DB: {1}",conn.DataSource,conn.Database);   
        }
    }
}

Its simply that UWP does not support OleDbConnection. Please see more details here on what you can use. Also, have a look at: API's of UWP to verify that you are on the correct version.

Also, this error can be caused by having multiple reference variants to System.Data for everyone else stumbling across this answer.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. I'll try another way to fix this out

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.