I've coded this following a youtube tutorial, on the tutorial the DataSource is an AOD.NET Entity Data Model where I've used an Access Database instead. I've compiled the code but am getting various errors like
The name 'productBindingSource' does not exist in the current context
or
The name 'DB' does not exist in the current context
or
The type or namespace name 'Product' could not be found
I'm not sure if i've missed adding a reference or if these errors are due to the data source being different?
Visual Studio automatically added the //TODO: This line of code etc... and I changed it to how it's showing in the tutorial.
I'm hoping someone can help show me what i'm doing wrong?
Tutorial: https://www.youtube.com/watch?v=-wGzK1vsqS8
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;
namespace ExportWebsiteData
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
List<Product> list = ((DataParameter)e.Argument).ProductList;
string filename = ((DataParameter)e.Argument).FileName;
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
Workbook wb = excel.Workbooks.Add(XlSheetType.xlWorksheet);
Worksheet ws = (Worksheet)excel.ActiveSheet;
excel.Visible = false;
int index = 1;
int process = list.Count;
//Add Column
ws.Cells[1, 1] = "Item Number";
ws.cells[1, 2] = "Model";
ws.cells[1, 3] = "Manufacturer";
ws.cells[1, 4] = "Category";
ws.cells[1, 5] = "Subcategory";
//
foreach(Product p in list)
{
if (!backgroundWorker.CancellationPending)
{
backgroundWorker.ReportProgress(index++ * 100 / process);
ws.Cells[index, 1] = p.ItemNumber.ToString();
ws.Cells[index, 2] = p.Model.ToString();
ws.Cells[index, 3] = p.Manufacturer.ToString();
ws.Cells[index, 4] = p.Category.ToString();
ws.Cells[index, 5] = p.SubCategory.ToString();
}
}
//Save file
ws.SaveAs(filename, XlFileFormat.xlWorkbookdefault, Type.Missing, Type.Missing, true, false, XlSaveConflictResolution.xlLocalSessionChanges, Type.Missing, Type.Missing);
excel.Quit();
}
struct DataParameter
{
public List<Product> ProductList;
public string FileName { get; set; }
}
DataParameter _inputParameter;
private void Form1_Load(object sender, EventArgs e)
{
using (this.inventoryTableAdapter.Fill(this._Wizard_Data_2016_10_17DataSet.Inventory); = new _Wizard_Data_2016_10_17DataSet())
{
productBindingSource.DataSource = DB.Products.ToList();
}
// TODO: This line of code loads data into the '_Wizard_Data_2016_10_17DataSet.Inventory' table. You can move, or remove it, as needed.
//this.inventoryTableAdapter.Fill(this._Wizard_Data_2016_10_17DataSet.Inventory);
}
private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar.Value = e.ProgressPercentage;
lblStatus.Text = string.Format("Processing...{0}", e.ProgressPercentage);
progressBar.Update();
}
private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if(e.Error == null)
{
Thread.Sleep(100);
lblStatus.Text = "Your data has been successfully exported.";
}
}
private void btnExport_Click(object sender, EventArgs e)
{
if (backgroundWorker.IsBusy)
return;
using (SaveFileDialog sfd = new SaveFileDialog() { Filter = "Excel Workbook|*.xls" })
{
if (sdf.ShowDialog() == DialogResult.OK)
{
_inputParameter.FileName = sfd.FileName;
_inputParameter.ProductList = productBindingSource.DataSource as List<product>;
progressBar.Minimum = 0;
progressBar.Value = 0;
backgroundWorker.RunWorkerAsync(_inputParameter);
}
}
}
}
}
UPDATE:
John's answer did fix my errors but the data-grid is now being populated by the cs code instead of a database. I've made a video explaining the problem in more detail if anyone can let me know what they think is the problem.
https://www.dropbox.com/s/1l5iw1j32a6oroj/C%23Excel.wmv?dl=0

Form1_Loadcode, which is where this connection is taking place. If you wish we could set up a chat if it will help.Productclass.