c# forms closing and exit without saving data

Malik Asad Mahmood 126 Reputation points
2021-12-01T18:20:56.31+00:00

Hi,
Please I need help, as I am saving recording as following into oracle database at the end of code, I want my c# form will close and exit automatically, its closing and exit but without saving data please help

public void SaveUsingOracleBulkCopy(DataTable dt)
{
try
{
using (var connection = new OracleConnection(connectionString))
{
connection.Open();
int[] ids = new int[dt.Rows.Count];
string[] names = new string[dt.Rows.Count];
string[] addresses = new string[dt.Rows.Count];

                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    ids[j] = Convert.ToInt32(dt.Rows[j]["ID"]);
                    names[j] = Convert.ToString(dt.Rows[j]["NAME"]);
                    addresses[j] = Convert.ToString(dt.Rows[j]["ADDRESS"]);
                }

                OracleParameter id = new OracleParameter();
                id.OracleDbType = OracleDbType.Int32;
                id.Value = ids;

                OracleParameter name = new OracleParameter();
                name.OracleDbType = OracleDbType.Varchar2;
                name.Value = names;

                OracleParameter address = new OracleParameter();
                address.OracleDbType = OracleDbType.Varchar2;
                address.Value = addresses;

                // create command and set properties
                OracleCommand cmd = connection.CreateCommand();
                cmd.CommandText = "INSERT INTO BULKINSERTTEST (ID, NAME, ADDRESS) VALUES (:1, :2, :3)";
                cmd.ArrayBindCount = ids.Length;
                cmd.Parameters.Add(id);
                cmd.Parameters.Add(name);
                cmd.Parameters.Add(address);
                cmd.ExecuteNonQuery();

this.close();
}
}
catch (Exception ex)
{
throw ex;
}
}
}

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
9,923 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jack J Jun 24,266 Reputation points Microsoft Vendor
    2021-12-02T02:43:52.397+00:00

    @Malik Asad Mahmood ,Based on my test, I find some problems about your code.

    First, I suggest that you could change the following code:

       cmd.CommandText = "INSERT INTO BULKINSERTTEST (ID, NAME, ADDRESS) VALUES (:1, :2, :3)";  
    

    Into:

    cmd.CommandText = "INSERT INTO Teacher (ID, NAME, ADDRESS) VALUES (@ID, @NAME, @ADDRESS)";  
    

    Second, we can not assign the arr to an OracleParameter.

    Third, It will be better to run the code in the loop.

    Here is a code example you could refer to.

     public void SaveUsingOracleBulkCopy(DataTable dt)  
            {  
                try  
                {  
                    string connstr = "";  
                    using (var connection = new OracleConnection(connstr))  
                    {  
                        connection.Open();  
      
                        for (int j = 0; j < dt.Rows.Count; j++)  
                        {  
                            OracleParameter id = new  OracleParameter();  
                            id.SqlDbType = OracleDbType.Int32;  
                            id.Value = Convert.ToInt32(dt.Rows[j]["ID"]);  
                            id.ParameterName="@ID";  
      
                            OracleParameter name = new OracleParameter();  
                            name.SqlDbType = OracleDbType.Varchar2;  
                            name.Value = Convert.ToString(dt.Rows[j]["NAME"]);  
                            name.ParameterName="@NAME";  
      
                            OracleParameter address = new  OracleParameter();  
                            address.SqlDbType  = OracleDbType.Varchar2;  
                            address.Value = Convert.ToString(dt.Rows[j]["ADDRESS"]);  
                            address.ParameterName="@ADDRESS";  
                            OracleCommand cmd = connection.CreateCommand();  
                            cmd.CommandText = "INSERT INTO Teacher (ID, NAME, ADDRESS) VALUES (@ID, @NAME, @ADDRESS)";  
                            cmd.Parameters.Add(id);  
                            cmd.Parameters.Add(name);  
                            cmd.Parameters.Add(address);  
                            cmd.ExecuteNonQuery();  
                        }  
      
                     
                        this.Close();  
      
                    }  
                }  
                catch (Exception ex)  
                {  
                    throw ex;  
                }  
            }  
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.