- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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 and by running that, it opens the windows form and shows excel data in Table format `
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;
}
}
}`
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, but after clicking on show button, Revit Does crash and closes.
I appreciate if anyone can help me.
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.
The picture below shows how it works when I have not yet made an external command.
Solved! Go to Solution.