Solved

How to execute two queries simultaneously

Posted on 2010-08-25
6
444 Views
Last Modified: 2012-05-10
Experts
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,
Anindya
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))

                {

                    con.Open();

                    SqlDataReader reader = cmd.ExecuteReader();

                    if (!reader.HasRows) return;

                    while (reader.Read())

                    {

                        comID=Convert.ToInt32(string.Format("{0}",reader[0]));

                        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

error.JPG
error.JPG
0
Comment
Question by:ANINDYA
6 Comments
 
LVL 3

Expert Comment

by:celdridgeMadman
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.
0
 
LVL 16

Accepted Solution

by:
Vikram Singh Saini earned 250 total points
ID: 33528617
0
 

Author Comment

by:ANINDYA
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
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 3

Assisted Solution

by:celdridgeMadman
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.
0
 

Author Closing Comment

by:ANINDYA
ID: 33529136
Experts thanks for finding time and answering.
Anindya Chatterjee
Bangalore
India
0
 
LVL 5

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);
c.Open();
SqlDataAdapter adapter1 = new SqlDataAdapter("select * from table1", c);
DataTable dt1 = new DataTable();
adapter1.Fill(dt1);
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();
           adapter2.Fill(dt2);
}
c.Close();

note dbconn is a connectionstring entry in the webconfig
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
MS SQL export CSV & schedule It 9 42
SQL Select Query help 3 31
How to calculate iops? 12 27
Hide Tab Page 3 18
In this article I will describe the Backup & Restore 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.
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now