Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When I try to add, I get the error vshost32.exe has stopped working.

But when excel is open, there is no problem

I am working on the excel file shared on the network


1. If I Share the Workbook I get the error

2. Can't add data when Excel is closed

What I have tried:

OleDbConnection baglanti12 = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\DESKTOP-G9FITLN\paylasim\" + label2.Text.ToString() + ";Extended Properties='Excel 12.0 Xml;HDR=YES;'");
                    baglanti12.Open();
                    OleDbCommand komut = new OleDbCommand("insert into [Sheet1$B" + (dataGridView1.CurrentCell.RowIndex + 1).ToString() + ":B" + (dataGridView1.CurrentCell.RowIndex + 1).ToString() + "]  values (@p1)", baglanti12);
                    komut.Parameters.AddWithValue("@p1", textBox2.Text);
                    komut.ExecuteNonQuery();
                    baglanti12.Close();

                    OleDbConnection baglanti13 = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\DESKTOP-G9FITLN\paylasim\" + label2.Text.ToString() + ";Extended Properties='Excel 12.0 Xml;HDR=YES;'");
                    baglanti13.Open();
                    OleDbDataAdapter da = new OleDbDataAdapter("Select * From [Sheet1$] where [Bilgi]='" + label1.Text + "'", baglanti13);
                    dt = new DataTable();
                    ds = new DataSet();
                    da.Fill(dt);
                    dataGridView1.DataSource = dt;
                    baglanti13.Close();
Posted
Updated 27-Aug-20 9:33am
v4
Comments
PIEBALDconsult 27-Aug-20 12:58pm    
Dunno, but you have more serious issues anyway.
You need to learn to use parameters with OleDb -- which is by position, not name.
Member 14924462 27-Aug-20 13:09pm    
But when excel is open, there is no problem
Member 14924462 27-Aug-20 13:18pm    
If I Share the Workbook I get the error

1 solution

First thing, you need to learn about parameterized query[^]. Your current code is open for SQL Injection[^] and thus it can destroy your database.

Second thing, it looks like you are not using any Interop assembly to work with excel. If it's working with open and not with close, it could be that the process is taking time. If process is busy for a long time, W7 will shut it down, even though if it is still ongoing. So, try the entire operation on a backgroundworker thread and that should not save UI.

Rest, a sample app here to give a quick try and see if anything differs: Read Write Excel file with OLEDB in C# (without Interop)[^]

Though it might not be needed (but do try if above all fails), vshost32 error at times happens when certain APIs get affected by enabled VS hosting process. Turn it off and see: How to: Disable the Hosting Process - Visual Studio 2015 | Microsoft Docs[^]

More details: "vshost32.exe has stopped working" - Learn by Insight...[^]
 
Share this answer
 
v3

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