joe_pool_is Posted January 31, 2008 Posted January 31, 2008 Using C# 2005 with SQL Server 2000: I'm getting this error:There is already an open DataReader associated with this Command which must be closed first.The solution on forums.microsoft.com is to add "MultipleActiveResultSets=True" for SQL Server 2005. Since I only have SQL Server 2000, the solution appears to be that I need to close my DataReader. My question is, How is a DataReader closed in the following code?string fmt = [color=red]"SELECT DISTINCT [{0}] FROM dbo."[/color] + employeeInfo + [color=red]" WHERE [GROUP]='"[/color] + cboGroup.Text + [color=red]"'"[/color]; string[] strCbo = { [color=red]"DEPT"[/color], [color=red]"JOBTITLE"[/color], [color=red]"SHIFT"[/color] }; cboDept.Items.Clear();[color=green] // clear ComboBox Items on form[/color] cboTitle.Items.Clear();[color=#008000] // clear ComboBox Items on form[/color] cboShift.Items.Clear();[color=#008000] // clear ComboBox Items on form[/color] for (int i = 0; i < 3; i++) { string sql = string.Format(fmt, strCbo[i]); try { DataSet ds = new DataSet(strCbo[i]); [color=green]// TableAdapter taEmpInfo[/color] SqlDataAdapter da = new SqlDataAdapter(sql, taEmpInfo.Connection); da.Fill(ds, strCbo[i]); foreach (DataRow dr in ds.Tables[strCbo[i]].Rows) { string item = dr.ItemArray.GetValue(0).ToString().Trim(); if (item != [color=red]""[/color]) { if (i == 0) cboDept.Items.Add(item); else if (i == 1) cboTitle.Items.Add(item); else if (i == 2) cboShift.Items.Add(item); } } } catch (Exception err) { Console.WriteLine(err.Message); } } Quote Avoid Sears Home Improvement
Administrators PlausiblyDamp Posted January 31, 2008 Administrators Posted January 31, 2008 Are you using a DataReader anywhere else in your code as I can't see any explicit mention of them in that snippet? If so you are better off making sure that it is closed in the routine that uses it. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
joe_pool_is Posted January 31, 2008 Author Posted January 31, 2008 Ok. Found it. When the form was loading, it was filling the TableAdapters, which were bound to the DataGridView. Further, whenever data is selected on the DataGridView, I was populating my ComboBoxes with that information. Fix was to add a simple Boolean value that gets set to true only after the data has been loaded. Now the ComboBox updating routine only runs if the value is true. Quote Avoid Sears Home Improvement
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.