简体   繁体   中英

retrieve data from database and insert those values to another table in database c#

I'm getting error in connection open like "The connection is already opened". It is telling to close the opened connection of while loop(reading data from table). But any way the data's have to inserted to data base while reading. How to solve?

private bool iscompanyExist(string comname)
{
    string query = "select * from Ageingrpt where Companyname='" + comname + "' group by Companyname having sum(current)>0 or sum('a1-30')>0 or sum('a31-60')>0 or sum('a61-90')>0 or sum('a90g')>0 ;";
    MySqlCommand cmd = new MySqlCommand(query, cnn);
    MySqlDataReader reader1;
    cnn.Open();
    reader1 = cmd.ExecuteReader();
    if (reader1.HasRows)
    {
        cnn.Close();
        return true;
    }
    else
    {
        cnn.Close();
        return false;
    }
}

private void Ageingcalc()
{
    string duedate = null;
    string compname = null, outstanding = null;
    //90<
    string query = "select c.address1,sum(p.OutstandingAmt) from invoice i,payment p,customer c where p.invoiceNo=i.invoiceNo and i.cid=c.cid and p.OutstandingAmt>0 and i.dueDate<'" + DateTime.Now + "'   group by c.address1 ;";
    MySqlCommand cmd = new MySqlCommand(query, cnn);
    MySqlDataReader reader;
    cnn.Open(); //open1
    reader = cmd.ExecuteReader();
    while (reader.Read())
    {
        compname = reader[0].ToString();
        outstanding = reader[1].ToString();
        if (iscompanyExist(compname) == true)
        {
            cnn.Open();
            query = "update Ageingrpt set a90g='" + outstanding + "';";
            cmd = new MySqlCommand(query, cnn);
            MySqlDataReader reader2 = cmd.ExecuteReader(); // query will be executed and data saved to the db
            cnn.Close();
        }
        else
        {
            cnn.Open();
            query = "insert into Ageingrpt  values('" + compname + "',0,0,0,0,'" + outstanding + "',0); ";
            cmd = new MySqlCommand(query, cnn);
            cmd.ExecuteReader(); // query will be executed and data saved to the db
            cnn.Close();
        }
    }

    cnn.Close();
}
    private void Ageingcalc()
            {
                string duedate = null;
                string compname = null, outstanding = null;

                //90<
                string query = "select c.address1,sum(p.OutstandingAmt) from invoice i,payment p,customer c where p.invoiceNo=i.invoiceNo and i.cid=c.cid and p.OutstandingAmt>0 and i.dueDate<'" + DateTime.Now + "'   group by c.address1 ;";
                MySqlCommand cmd = new MySqlCommand(query, cnn);
                MySqlDataReader reader;
                cnn.Open();         //open1
                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    compname = reader[0].ToString();
                    outstanding = reader[1].ToString();
                    if (iscompanyExist(compname) == true)
                    {
                        query = "update Ageingrpt set a90g='" + outstanding + "';";
                        cmd = new MySqlCommand(query, cnn);
                        MySqlDataReader reader2 = cmd.ExecuteReader(); // query will be executed and data saved to the db
                        cnn.Close();
                    }
                    else
                    {
                        query = "insert into Ageingrpt  values('" + compname + "',0,0,0,0,'" + outstanding + "',0); ";
                        cmd = new MySqlCommand(query, cnn);
                        cmd.ExecuteReader(); // query will be executed and data saved to the db
                        cnn.Close();
                    }

                }
                cnn.Close();
            }


once connection is open and need to close it..we should not open again..

This is worked for me.I have used DataTable to store the retrieved data.

 private void Ageingcalc()
            {
                string duedate = null;
                string compname = null, outstanding = null;


                //90<
                string query = "select c.address1,sum(p.OutstandingAmt) as outstanding from invoice i,payment p,customer c where p.invoiceNo=i.invoiceNo and i.cid=c.cid and p.OutstandingAmt>0 and i.dueDate<'" + DateTime.Now + "'   group by c.address1 ;";
                MySqlCommand cmd = new MySqlCommand(query, cnn);
                MySqlDataReader reader;
                cnn.Open();
                MySqlDataAdapter da = new MySqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
                da.Dispose();
                cnn.Close();

                foreach (DataRow row in dt.Rows)
                {

                    if (iscompanyExist(row["address1"].ToString()) == true)
                    {
                        cnn.Open();
                        MessageBox.Show(row["address1"].ToString());
                        query = "update Ageingrpt set a90g='" + row["outstanding"].ToString() + "';";
                        cmd = new MySqlCommand(query, cnn);
                        MySqlDataReader reader2 = cmd.ExecuteReader(); // query will be executed and data saved to the db
                        cnn.Close();
                    }
                    else
                    {
                        cnn.Open();
                        MessageBox.Show(row["address1"].ToString());
                        query = "insert into Ageingrpt  values('" + row["address1"].ToString() + "',0,0,0,0,'" + row["outstanding"].ToString() + "',0); ";
                        cmd = new MySqlCommand(query, cnn);
                        MySqlDataReader reader2 = cmd.ExecuteReader(); // query will be executed and data saved to the db
                        cnn.Close();
                    }


                }

            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM