【发布时间】:2014-07-07 22:27:53
【问题描述】:
以下是我跟踪志愿者的程序。我的问题是,对于下面的两个函数,当调用fillListBoxData() 时,大约在中途跳转到fillDataGridView 函数。在第 15 行,dataAda1.Fill(datTab3); 程序立即跳转到fillDataGridView(string fullName)。我不知道为什么。
谁能帮助我,让这个程序停止这样做。
public void fillListBoxData()
{
//Open connection
//found in app.config
using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.Database1ConnectionString))//database ONE
{
//open the connection
conn.Open();
// Create new DataAdapter
using (SqlDataAdapter dataAda1 = new SqlDataAdapter("SELECT (firstName + ' ' + lastName) AS NAME FROM dbo.VolunteerContactInfo", conn))
{
// Use DataAdapter to fill DataTable
DataTable datTab3 = new DataTable();
dataAda1.Fill(datTab3);
//assign the dataTable as the dataSource for the listbox
volList.DataSource = datTab3;
//display member needed to show text
volList.DisplayMember = "NAME";
conn.Close();
}
}
}
void fillDataGridView(string fullName)
{
string tableName = "dbo." + fullName;
// Open connection
using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.Database2ConnectionString))//database TWO
{
//open the connection
conn.Open();
// Create new DataAdapter
using (SqlDataAdapter dataAda = new SqlDataAdapter("SELECT * FROM " + @tableName, conn))
{
// Use DataAdapter to fill DataTable
DataTable datTab = new DataTable();
dataAda.Fill(datTab);
volWorkDataGrid.DataSource = datTab;
}
conn.Close();
}
}
【问题讨论】:
-
你怎么知道它跳转到
fillDataGridView? -
您的方法
fillDataGridView可能被注册为网格的事件处理程序,并且在填充期间被网格调用。您需要删除该事件处理程序以防止它被调用。 -
也有可能在调试时调试器在线程之间跳转。
-
@Kevin 我知道它正在跳转到 fillDataGridView,因为我是在 Visual Studio 的调试模式下观看的。该函数正在使我的程序崩溃,因为它没有让 fillListBoxData() 完成。
-
在
fillDataGridView中查看您的堆栈跟踪以了解调用的原因。
标签: c# sql sql-server windows winforms