5

Hello is there a way to use two sqldatareader at same time ?

i was coding a program

and i got error because i use 2 readers at same

Sample of code

SqlCommand LoadSilk = new SqlCommand("SELECT silk_own FROM SK_Silk WHERE JID = (SELECT JID FROM TB_User WHERE StrUserID = '"+ comboBox1.Text +"')", Connection);
SqlDataReader SilkReader = LoadSilk.ExecuteReader();
SqlCommand LoadCharacter = new SqlCommand("SELECT * FROM SRO_VT_SHARD.DBO._Char WHERE CharID IN (SELECT CharID FROM SRO_VT_SHARD.DBO._User WHERE UserJID = "+ JID +")", Connection);
SqlDataReader CharacterReader = LoadCharacter.ExecuteReader();
try
{
 SilkReader.Read();
 textBox5.Text = SilkReader[0].ToString();
 Silk = SilkReader[0].ToString();
 dataGridView1.Enabled = true;
 button2.Enabled = true;
 while (CharacterReader.Read()) {
 dataGridView1.Rows.Add(CharacterReader["CharID"].ToString(), CharacterReader["CharName16"].ToString(), CharacterReader["CurLevel"].ToString());
 }
 log(comboBox1.Text + " account data loaded");
}
catch (Exception ex) {
 log(ex.Message);
 MessageBox.Show("Error");
} finally {
 SilkReader.Close();
 CharacterReader.Close();
}

and its gave me that error

There is already an open DataReader associated with this Command which must be closed first.

asked Aug 3, 2012 at 20:44
0

5 Answers 5

14

The error message is misleading. You must have MultipleActiveResultSets=True set in your connection string to be able to send two seperate commands with two different readers.

answered Aug 3, 2012 at 20:48
0
5

It is possible when you use something called Multiple Active Result Sets, or MARS. Check out this helpful article that details all possible pitfalls.

answered Aug 3, 2012 at 20:47
3

You'll have to enable Multiple Active Recordsets (MARS) on your connection.

http://msdn.microsoft.com/en-us/library/h32h3abf%28v=vs.80%29.aspx

answered Aug 3, 2012 at 20:47
1

1.Just add MultipleActiveResultSets=True into your connection string:

private string _ConnectionString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=yourdbpath\Database.mdf;Integrated Security=True;MultipleActiveResultSets=True;Connect Timeout=30";
answered Feb 4, 2018 at 18:42
0

Declare those DataReader in the block public partial class Form1 : Form Example:

namespace GoodFood_1_
{
 public partial class Autentificare_client : Form
 {
 SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\edi_b\Documents\visual studio 2013\Projects\GoodFood(1)\GoodFood(1)\GOOD_FOOD.mdf;
 Integrated Security=True;MultipleActiveResultSets=True");
 SqlConnection con2 = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=c:\users\edi_b\documents\visual studio 2013\Projects\GoodFood(1)\GoodFood(1)\GOOD_FOOD.mdf;
 Integrated Security=True;MultipleActiveResultSets=True");
 SqlDataReader dr;
 SqlDataReader cc;
 public Autentificare_client()
 {
 InitializeComponent();
 }
Kurt Van den Branden
13.1k10 gold badges82 silver badges92 bronze badges
answered Nov 15, 2016 at 9:09

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.