Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am not able to insert values from a dataset to a database table.The code is not showing any error but the values are not entering into the table.plkease help me out

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 30-Apr-14 2:49am
v2
Comments
CHill60 30-Apr-14 8:52am    
Where are you committing the transaction?
thatraja 30-Apr-14 8:58am    
Spot on.
Member 10784192 30-Apr-14 9:06am    
i don't know exactly where i should commit the transaction? can you please giude
ZurdoDev 30-Apr-14 20:38pm    
It has already been answered but if you think about it, wouldn't you commit after your last sql statement?
gggustafson 30-Apr-14 9:11am    
Also, you do not need con.Close( ) in the first using. using will close the connection on its exit.

As CHill60 has indicated, you are missing the commit. But you have also not taken advantage of the using statement. In the following, I give the template that I think your method should take.
C#
private void button1_Click ( object    sender,
                             EventArgs e )
    {
    DataSet     data_set = new DataSet ( );
    DataTable   data_table = new DataTable ( );
    string      insert_statement = String.Empty;

    using ( OleDbConnection ole_db_connection =
                                new OleDbConnection ( ) )
        {
        OleDbDataAdapter  ole_db_data_adapter;
        OleDbCommand      ole_db_command ;

        ole_db_connection.ConnectionString =
            "Data Source=XE; " +
            "User ID=ER; " +
            "Password=ER; " +
            "Provider=msdaora";
        ole_db_connection.Open ( );
        ole_db_command = new OleDbCommand ( );
        ole_db_command.Connection = ole_db_connection;
        ole_db_command.CommandText =
            "select * from bill where BILL_NO = '" +
            comboBox1.SelectedValue.ToString ( ) +
            "' ";
        ole_db_command.CommandType = CommandType.Text;
        ole_db_data_adapter = new OleDbDataAdapter (
                                            ole_db_command );
        ole_db_data_adapter.Fill ( data_set );
        }

    data_table = data_set.Tables [ 0 ];

    insert_statement = "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 )";

    using ( OracleConnection oracle_connection =
                             new OracleConnection ( ) )
        {
        OracleDataAdapter oracle_adapter;
        OracleTransaction oracle_transaction;

        oracle_connection.ConnectionString =
            "Data Source=XE; " +
            "Persist Security Info=True; " +
            "User ID=ER; " +
            "Password=ER; " +
            "Unicode=True" );
        oracle_connection.Open ( );
        oracle_transaction =
            oracle_connection.BeginTransaction ( );
        oracle_adapter = new OracleDataAdapter ( );
        oracle_adapter.InsertCommand =
            new OracleCommand ( insert_statement,
                                oracle_connection);

        foreach ( DataRow data_row in data_table.Rows )
            {
            oracle_adapter.InsertCommand.Parameters.Add (
                new OracleParameter (
                    ":BILL_NO",
                    data_row [ "BILL_NO" ] ) );
            // :
            // :
            oracle_adapter.InsertCommand.Transaction =
                oracle_transaction;
            oracle_adapter.InsertCommand.ExecuteNonQuery ( );
            }
        oracle_transaction.Commit ( );
        }

    MessageBox.Show ( "DONE" );
    }
}

Note that the ole_db_connection in the first using need not be closed. And by adding a second using for oracle_connection, there is no need to close it. Just before the end of the oracle_connection using, the oracle_transaction Commit occurs.

Hope that helps.
 
Share this answer
 
Comments
CHill60 30-Apr-14 11:23am    
5'd!
Before Instead of
conn.Dispose();
you need
trans.Commit();


[Edit - corrected as above - with thanks to gggustafson for the comment]
 
Share this answer
 
v2
Comments
Manoj Kumar Choubey 30-Apr-14 9:11am    
I am agree with CHill

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



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