I am trying to show an Excel spreadsheet as a table on windows form in Revit, First I wrote the code in VisualStudio, I created a windows form(That I enter the path and sheet number) and by running that, it opens the windows form and shows excel data in Table format `
namespace ShowDataWindowsForm { public partial class Form1 : Form { public Form1() { InitializeComponent(); }
private void button1_Click(object sender, EventArgs e)
{
string PathConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
textBoxPath.Text + ";Extended Properties=Excel 12.0;";
OleDbConnection conn = new OleDbConnection(PathConn);
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter("SELECT * FROM
[" + textBoxSheet.Text + "$]", conn);
DataTable dt = new DataTable();
myDataAdapter.Fill(dt);
dataGridView1.DataSource = dt;
}
}
Then I created an External command, added a windows form for that and did exactly the same process, and this is the code for external command :
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.ReadOnly)]
public class iShowDataWindowsForm : IExternalCommand { static AddInId appId = new AddInId(new Guid("DB98294F-89D9-4EAE-A990-63A0694F91CC")); public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elementSet) {
using (Form1 thisform = new Form1())
{
thisform.ShowDialog();
}
return Result.Succeeded;
}
my problem is when I open Revit and run the plugin, it opens a windows form and like the one in visual studio, I enter the path and sheet number, but after clicking on show button, Revit Does crash and closes. I appreciate if anyone can help me. This picture shows how it works in Visual studio, but in revit it does crash. See picture
I just put the path and sheet number on the code itself, now it's working by just clicking on Show bottom in Visual Studio, But still does crash in revit final code for windows form : `namespace ExcelVisualStudio { public partial class Form1 : Form { public Form1() { InitializeComponent(); }
private void button1_Click(object sender, EventArgs e)
{
string PathConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + @"C:\Users\farshidmoosavi\Desktop\Schedule3.xls" + ";Extended Properties=Excel 12.0;";
OleDbConnection conn = new OleDbConnection(PathConn);
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter("SELECT * FROM [" + "Sheet1" + "$]", conn);
DataTable dt = new DataTable();
myDataAdapter.Fill(dt);
dataGridView1.DataSource = dt;
}
}
}` I debugged the code and I found that this is the error that causes the program to crash: Step into: Stepping over non-user code 'System.Data.Common.DbDataAdapter.Fill' 'Revit.exe' (Managed (v4.0.30319)): Loaded 'C:\windows\Microsoft.Net\assembly\GAC_64\System.Transactions\v4.0_4.0.0.0__b77a5c561934e089\System.Transactions.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. Exception thrown: 'System.AccessViolationException' in System.Data.dll The program '[168] Revit.exe: Managed (v4.0.30319)' has exited with code -1073741819 (0xc0000005) 'Access violation'.
STILL don't know how to fix that.