Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 459
  • Last Modified:

How to execute two queries simultaneously

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
ANINDYA
Asked:
ANINDYA
2 Solutions
 
celdridgeMadmanCommented:
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
 
ANINDYAAuthor Commented:
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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
celdridgeMadmanCommented:
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
 
ANINDYAAuthor Commented:
Experts thanks for finding time and answering.
Anindya Chatterjee
Bangalore
India
0
 
Akin DeluI’m a software engineer, tech enthusiast and UI/UX lover. Commented:
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

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now