问题描述
我有这个问题,我能够重现.我创建新的Windows窗体,添加一个新的数据源(一个只有一个表的访问数据库文件).然后将表拖到窗体.然后添加一个新按钮和以下代码.
Im having this problem I was able to reproduce. I create new windows form, add a new data source (an access data base file, with only one table). Then drag the table to the form. Then add a new button and the following code.
private void Test()
{
this.table1TableAdapter.GetData();
}
private void SaveDialogTest()
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "Text File|*.txt";
saveFileDialog1.InitialDirectory = Application.StartupPath;
saveFileDialog1.Title = "test";
saveFileDialog1.ShowDialog();:mad:
if (saveFileDialog1.FileName != "")
{ }
}
private void button1_Click(object sender, EventArgs e)
{
Test();
SaveDialogTest();
}
表适配器由设计者创建:
The table adapter is created by the designer:
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the ''testDataSet.Table1'' table. You can move, or remove it, as needed.
this.table1TableAdapter.Fill(this.testDataSet.Table1);
}
当我运行表单并单击按钮时,调用saveFileDialog1.ShowDialog()时会出现AccessViolationException.
如果我在"Form1_Load"方法内移动"Test()"方法,则一切正常.
另外,如果我先运行对话框,则在调用GetData()之前,一切都会正常进行.像这样
When I run the form and click on the button I get an AccessViolationException, when calling saveFileDialog1.ShowDialog().
If i move the "Test()" method inside the "Form1_Load" method everything works fine.
Also if I run the dialog first, before calling GetData() everything works fine afterwards. Like this.
SaveDialogTest();
Test();
SaveDialogTest();
这将毫无例外地调用第二个SaveDialogTest().
另外,如果我将其编译并在XP pc中运行,则一切正常.
数据源:Microsoft Access数据库文件(OLE DB)
我正在使用Visual Studio 2008 SP1. Windows 7
谢谢.
This will call the second SaveDialogTest() without exception.
Also if I compile it and run it in and XP pc everything works fine.
Data Source: Microsoft Access Database File (OLE DB)
Im using Visual Studio 2008 SP1. Windows 7
Thank you.
推荐答案
On windows 7 (32 bit) had same problem when showing the openfiledialog from a modal form loaded for the second time with data from db in it. Solution appeared to be: do not allow autoupgrade of dialog.
Dim sfv As New System.Windows.Forms.SaveFileDialog
With sfv
.AutoUpgradeEnabled = False
[...]
But error came up again. Then I noticed it was apparently randomic till I realized it did not come out ifd I was able to show a saveFileDialog or an OpenfileDialog before loading any data from db.
Thus true workaround is: before load anything on the form you''re going to show, display a dialog asking user to choose a path and file you *might* need after (arrrg!). After that, load data. Now your can let users, if needed, to choose path and file with dialog again...
ie:
Private Sub frmReport_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
txtFilePathName.Text = "Export_" & Now.ToString("yyyy_MM_dd_HH_mm_ss", CultureInfo.GetCultureInfo("it-It")) & ".csv"
txtFilePathName.Text = GetSaveFileName(txtFilePathName.Text, ".csv", "Choose a csv File to save exported data", "csv |*.csv|All |*.*")
''now load data in forms, where you can also have a button to call again the GetSaveFileName
[...]
Private Function GetSaveFileName(ByVal fileName As String,
ByVal defaultExtension As String,
ByVal title As String,
ByVal filter As String) As String
Dim sfv As New System.Windows.Forms.SaveFileDialog
With sfv
.RestoreDirectory = True
.AddExtension = True
.DefaultExt = defaultExtension
.FileName = fileName
.Title = title
.Filter = filter
.CheckPathExists = True
.OverwritePrompt = True
.ShowHelp = False
If (.ShowDialog = DialogResult.OK) Then
fileName = .FileName
End If
End With
Return fileName
End Function
Cimperiali
这篇关于Visual Studio SaveFileDialog TableAdapter问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!