views:

113

answers:

1

Hi All,

I am inserting values in to Database from a Webform using ADO.NET, C#. DB I am using is Oracle Database. Values are not being inserted and the program gets struck at the cmd.ExecuteNonquery()

Here is my Code below, Please let me know If I am doing any mistake.. I am using some Static Methods will that be any problem ?..

public Boolean AddDivCo(Int32 UserNo,String ID, String Role, String DivName )
{
    Boolean ret = false;


    OracleCommand cmd = new OracleCommand();
    OracleConnection conn = new OracleConnection();
    int i = 0;

    try
    {
        conn.ConnectionString = ConfigurationManager.ConnectionStrings["Conn_RIS"].ConnectionString;
        conn.Open();

        cmd.Connection = conn;
        String mySQL = "INSERT INTO R4CAD_ADMIN (AdminUserNo, AdminID, AdminRole, AdminDivName)VALUES(:AdminUserNo,:AdminID,:AdminRole,:DivName)";

        OracleParameter p1 = new OracleParameter("AdminUserNo", OracleType.Number);
        p1.Value = UserNo;
        cmd.Parameters.Add(p1);

        OracleParameter p2 = new OracleParameter("AdminID", OracleType.VarChar);
        p2.Value = ID;
        cmd.Parameters.Add(p2);

        OracleParameter p3 = new OracleParameter("AdminRole", OracleType.VarChar);
        p3.Value = Role;
        cmd.Parameters.Add(p3);

        OracleParameter p4 = new OracleParameter("DivName", OracleType.VarChar);
        p4.Value = DivName;
        cmd.Parameters.Add(p4);

        cmd.CommandText = mySQL;

        i = cmd.ExecuteNonQuery();

        if (i != 0)
        {
            ret = true;
        }
        else
        {
            ret = false;
        }
    }
    catch (Exception err)
    {
        Console.WriteLine(err.Message.ToString());
    }
    finally
    {
        cmd.Dispose();
        //cmd = null;
        //conn = null;
        conn.Close();
    }
    return ret;
}
+1  A: 

Is there a primary key defined on this table? If so, then my guess is that you have another session that already has inserted a record with this key, but has not yet terminated the transaction with a commit or rollback. I don't see a commit as part of your code - I assume you're doing that somewhere else?

Execute your code above once more, and while it's hung run the following query from another session:

SELECT
      (SELECT username FROM v$session WHERE sid=a.sid) blocker,
       a.sid,
      ' is blocking ',
       (SELECT username FROM v$session WHERE sid=b.sid) blockee,
           b.sid
  FROM v$lock a JOIN v$lock b ON (a.id1 = b.id1 AND a.id2 = b.id2)
 WHERE a.block = 1
   AND b.request > 0;

This should tell you if you're being blocked by another session and what the SID is of that session.

DCookie
Thanks for your response. Yes, I do have a Primary Key on that Table but I am not inserting the values from any other way because when I run select Query on that Table from my Database it doesn't return any rows. As said by you while its hung, I ran the SELECT SQL provided by you. I got the OUTPUT as:121 FOIA is blocking FOIA 129121 FOIA is blocking FOIA 133I did not understand from that OutPut.... Please can you Help ?
msbyuva
121 FOIA is blocking FOIA 129121 FOIA is blocking FOIA 133
msbyuva
Thank You So much Sir... When I ran that Query, I realized that there is one more Instance running. I shutdown my system ran my program again, this time I was able to INSERT Successfully and when I ran the SQL Code, I didn't see any OUTPUT. From that I understood there was another Instance of SQL Developer was opened. Am I right ?..
msbyuva
Hard to say from here what it was. The basic rule is this: If session "A" inserts a row in a table with a primary key, session "A" will block ANY other session attempting to insert the same primary key until session "A" commits or rolls back the insert. All other attempts to insert will "hang" until then.
DCookie
Yeah, Makes sense from your above sentences and the Output I got when I ran the SQL. Thank You!..
msbyuva
You are welcome... glad to help.
DCookie
Hello DCookie, Again I am getting the problem. When I run the SQL it is showing SID 135 BLOCKERS FOIA is blocking BLOCKEE FOIA SID 145.. Please could you say why it being blocked. I am sure this time only one instance is opened. Or else can you what is called this type of behavior so that I can so some RD in Google. Thanks msbyuva.
msbyuva
Session 135 has inserted a row into the table. It has not committed the insert. Session 145 has come along and inserted the same Primary Key value. It must wait for 135 to decide what to do. Apparently, your process is not doing a commit when the transaction is complete. Same userid (FOIA) owns both sessions. Remember, multiple sessions can be opened up through the same connection.
DCookie
Is there any way we can OPEN only one Session a Connection. ? Because I am unable to figure out where I am opening another Session for that connection.
msbyuva
I think you need to figure out why you're not committing your first transaction in a timely manner.
DCookie
Well, I am only Inserting the values through the above Add Method. Any how I will try to use Store Procedures, that might solve the problem. Thank You !!! MsbYuva
msbyuva
Whatever method you use will need to include issuing a COMMIT or a ROLLBACK statement, as appropriate, when your transaction is complete. You are familiar with the concept of commit and rollback, right? In a Web app, you generally need to complete the entire transaction in one click, because of the stateless nature of web transactions. The next click is going to open another session.
DCookie
Yeah, I will do it as Said. thank you so much for your detailed help !!....
msbyuva