How to execute two queries simultaneously

Posted on 2010-08-25
Last Modified: 2012-05-10
please see the attached image and the error there.
Now please suggest me how to execute the two queries simultaneously.

Apart from my attempt in this if there is a better way to execute the same then please let me know .
Thanking you,
private void display_companyName_atUpdateMode()
            Int32 comID = 0;
            using (SqlConnection con = new SqlConnection(Connectionstring.Connection))
                string query = "select CompanyID from Table_Interaction where InteractionID='" +Convert.ToInt32( InteractionID.ToString() )+ "' ";
                using (SqlCommand cmd = new SqlCommand(query, con))
                    SqlDataReader reader = cmd.ExecuteReader();
                    if (!reader.HasRows) return;
                    while (reader.Read())
                        string query2 = "select CompanyName from Table_Client where CompanyID=@param";
                        SqlCommand cmd2 = new SqlCommand(query2, con);
                        cmd2.Parameters.AddWithValue("@param", comID);
                        SqlDataReader reader1 = cmd2.ExecuteReader();
                        if (!reader1.HasRows) return;
                        while (reader.Read())
                            comboBox_client.Text = string.Format("{0}", reader1[0]);


Open in new window

Question by:ANINDYA

Expert Comment

ID: 33528577
I think you'll find you can only execute 1 select command at a time per connection object. Fire up another connection and use that for your second query.
LVL 16

Accepted Solution

Vikram Singh Saini earned 250 total points
ID: 33528617

Author Comment

ID: 33528619
Expert celdridgeMadman
is it the only method used by all the developers or there is any other better way is there .
I am asking as I am fresher so I would like to know what is the best way to do that.
Thanking you
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.


Assisted Solution

celdridgeMadman earned 250 total points
ID: 33528663
Certainly not the "Best" way. It's a bit wasteful to use 2 connections when they're not needed. The "multiple active result set" (see vs00saini's links above)  seems ideal for this situation, but without knowing what version of ADO.NET you're using I can't say if they will work.

Another alternative is to copy the data locally into an array of some sort and loop through that, keeping in mind to only have 1 connection/command going at once.

Author Closing Comment

ID: 33529136
Experts thanks for finding time and answering.
Anindya Chatterjee

Expert Comment

by:Akin Delu
ID: 33529238
SqlDataReader will not let you use its connection with another SqlDataReader while its still open, why not try using a DataTable for the first query then loop through the rows of the DataTable obj then execute your second query.

This is a sample code
string connstr = ConfigurationManager.ConnectionStrings["dbconn"].ConnectionString;
SqlConnection c = new SqlConnection(connstr);
SqlDataAdapter adapter1 = new SqlDataAdapter("select * from table1", c);
DataTable dt1 = new DataTable();
foreach (DataRow dr in dt1.Rows)
           int id = int.Parse(dr["acc_class_fk"].ToString());
           SqlDataAdapter adapter2 = new SqlDataAdapter("select * from table2 where id = "+id+"", c);
           DataTable dt2 = new DataTable();

note dbconn is a connectionstring entry in the webconfig

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Format Data Field - SQL 11 37
Syntax for query to update table 2 29
Input parameteres to DragOver 2 20
What can the "OPTION" in the GROUP help me in my query? 4 25
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question