Click here to Skip to main content
16,020,378 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
private void button1_Click(object sender, EventArgs e)
{
   DataSet ds = new DataSet();
   using (OleDbConnection con = new OleDbConnection())
   {
      con.ConnectionString = "Data Source=XE; User ID=ER; Password=ER;Provider=msdaora";
      con.Open();
      OleDbCommand cmd = new OleDbCommand();
      cmd.Connection = con;
      cmd.CommandText = "select * from bill where BILL_NO = '" + Convert.ToString(comboBox1.SelectedValue) + "' ";
      cmd.CommandType = CommandType.Text;
      OleDbDataAdapter adp = new OleDbDataAdapter(cmd);
      adp.Fill(ds);
      con.Close();
   }
   string insqry = "INSERT INTO BILL2 (BILL_NO, CUSTOMER__NAME, ITEM_TAKEN, PRICE, MODE_OF_PAYMENT, TOTAL, DATE_PUR, SALE_FROM) VALUES (:BILL_NO, :CUSTOMER__NAME, :ITEM_TAKEN, :PRICE, :MODE_OF_PAYMENT, :TOTAL, :DATE_PUR, :SALE_FROM)";

   OracleConnection conn = new OracleConnection("Data Source=XE;Persist Security Info=True;User ID=ER;Password=ER;Unicode=True");
   conn.Open();
   OracleTransaction trans = conn.BeginTransaction();
   OracleDataAdapter ad = new OracleDataAdapter();
   ad.InsertCommand = new OracleCommand(insqry, conn);
   //int A = 0;
   foreach (DataRow drrow in ds.Tables["A"].Rows)
   {
      ad.InsertCommand.Parameters.Add(new OracleParameter(":BILL_NO", drrow["BILL_NO"]));
      ad.InsertCommand.Parameters.Add(new OracleParameter(":CUSTOMER__NAME", drrow["CUSTOMER__NAME"]));
      ad.InsertCommand.Parameters.Add(new OracleParameter(":ITEM_TAKEN", drrow["ITEM_TAKEN"]));
      ad.InsertCommand.Parameters.Add(new OracleParameter(":PRICE", drrow["PRICE"]));
      ad.InsertCommand.Parameters.Add(new OracleParameter(":MODE_OF_PAYMENT", drrow["MODE_OF_PAYMENT"]));
      ad.InsertCommand.Parameters.Add(new OracleParameter(":TOTAL", drrow["TOTAL"]));
      ad.InsertCommand.Parameters.Add(new OracleParameter(":DATE_PUR", drrow["DATE_PUR"]));
      ad.InsertCommand.Parameters.Add(new OracleParameter(":SALE_FROM", drrow["SALE_FROM"]));
      ad.InsertCommand.Transaction = trans;
      ad.InsertCommand.ExecuteNonQuery( );
   }
   conn.Dispose();
   MessageBox.Show("DONE");
}
Posted
Updated 29-Apr-14 23:55pm
v5
Comments
OriginalGriff 30-Apr-14 5:25am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
So at least tell us which line is causing the exception?
Use the "Improve question" widget to edit your question and provide better information.

1 solution

As per your sql statements you are trying to insert selected rows from one table to another.

With INSERT ... SELECT, you can quickly insert many rows into a table from one or many tables. For example:
SQL
INSERT INTO tbl_temp2 (fld_id)
  SELECT tbl_temp1.fld_order_id
  FROM tbl_temp1 WHERE tbl_temp1.fld_order_id > 100;


you can insert all the record by single ExecuteNonQuery as below

C#
using (OracleConnection connection = new OracleConnection(connectionString))
{
    OracleCommand command = new OracleCommand(myExecuteQuery, connection);
    command.Connection.Open();
    command.ExecuteNonQuery();
}
 
Share this answer
 
Comments
Member 10784192 30-Apr-14 6:11am    
i need to know why the above mentioned error is occurring in the for each loop every time tried running the program.can anyone explain this please
[no name] 30-Apr-14 7:49am    
It's for exactly the same reason that everyone else on the planet gets that error. You are trying to use an object that is null.
Member 10784192 30-Apr-14 8:01am    
Exactly i have read about this error and you are very true but the thing is that i couldn't find the object which is basically declared as null.The code is there above can you help me out Wes Aday

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900