Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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);
 }
}

Posted

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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...