Link to home
Start Free TrialLog in
Avatar of MichaelLasell
MichaelLasell

asked on

Oracle Sequence/ ASP.NET C# problem

I created an Oracle table in Toad, then made an ASP.NET page with a gridview and detailsview.

I got inserts, deletes and updates to work from the detailsview (very easily). But it was using an inputted number to update the ID.
Since the ID is the Primary Key I need to use an Oracle sequence to increment.
However when I created a Sequence in Oracle and then tried to substitute

NEWS_SEQ.NEXTVAL for :ID in the Insert command, I got the  error:"ORA-01036: illegal variable name/number"

I can run the query in Toad:

INSERT INTO NEWS (ID, TITLE) VALUES(NEWS_SEQ.NEXTVAL, 'test')

So I think I have zeroed in on the problem.

Any Oracle/.NET Gurus out there? There seems to be a conflict between using an Oracle Sequence nextval and the way ASP.NET writes the parameter.
Do I have to look up the sequence.nextval before I do the Insert, and use that value in the insert command? If so, How would I do it?

InsertCommand

='INSERT INTO "NEWS" ("ID", ...") VALUES (NEWS_SEQ.NEXTVAL, ...)'

<InsertParameters>

<asp:Parameter Name="ID" Type="Decimal" />
...

</InsertParameters>
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

I haven't made the leap to .Net 2.0 yet so I'm not all that familiar with gridview.  I am vary familiar with datagrids and Oracle parameters so I think I know what the problem is.  I'm just not sure exactly how to implement it with a gridview.

You don't need ID as a parameter to the insert statement.  NEWS_SEQ.NEXTVAL will just be passed to the DB as part of the insert string and evaluated by the DB at execution time.
Avatar of MichaelLasell

ASKER

Thanks,
Interesting idea. I tried it before and now have tried it again.
The INSERT is happening through the DetailsView.

Maybe I should try a datagrid, but they don't seem to be encouraging that with 2.0

 So the code now goes like this: But it gives the same error as before.
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
        ConnectionString="<%$ ConnectionStrings:NEWWEB %>"
        ProviderName="<%$ ConnectionStrings:NEWWEB.ProviderName %>"
        ...
        InsertCommand="INSERT INTO LEGAL_FILINGS (FILING_NUMBER,STATUS,TYPE_CODE,ID)
                                            VALUES (:FILING_NUMBER,:STATUS,:TYPE_CODE,FILING_SEQ.nextval)"
     
 <InsertParameters>
        <asp:Parameter Name="FILING_NUMBER" Type="String" />
        <asp:Parameter Name="STATUS" Type="String"  />
        <asp:Parameter Name="TYPE_CODE" Type="String" />
    </InsertParameters>
Well, the solution, I like to apply is a pure oracle solution. It will work regardless of weather you insert the row using C#, VB, Java, etc.

Write an BEFORE INSERT trigger in oracle, then apply the following change

INSIDE THE TRIGGER
---------------------------------

SELECT sequence.nextval into oraclevaribale from dual;

NEW.ID = oraclevariable;

return;
Hello,
This looks promising since everything else I have tried does not work.
However I have never written a trigger for a database.
I tried the code below, took out any reference to ID in the ASP.NET, but I still get the same error...

In Toad, I was in Triggers
I have selected the table
Fire When: Before
Fire On: Insert
For Each: Statement (Not row?)
Referencing New as New Old as Old( Don't know what this means)
In the body:
DECLARE
tmpVar NUMBER;

BEGIN
   tmpVar := 0;

   SELECT Filing_Seq.NEXTVAL INTO tmpVar FROM dual;
   :NEW.ID := tmpVar;
 

   EXCEPTION
     WHEN OTHERS THEN
       -- Consider logging the error and then re-raise
       RAISE;
END FIND_NEXT_ID;
If you still get the same error by using the trigger then the problem doesn't appear to be with the sequence.  It must be somewhere else in the code.

I would stick with the gridview becuase like you already mentioned, datagrids are pretty much dead in 2.0.  I wish I could be more help but I really don't know a lot about 2.0.

I did come across a post on another site that sounds exactly like what you are experiencing.  The solution there was basically to create another page as simple as possible and make small changes until you figure out where it's breaking:

http://www.velocityreviews.com/forums/t300168-problem-gridview-oracle.html

I would probably do something similar.  I might remove all the parameters and hard-code the values in the insert statement.  If that runs, replace the parameters 1 by 1 until it fails.
ASKER CERTIFIED SOLUTION
Avatar of riyaz404
riyaz404

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The trigger I made worked as I can now input data in Toad without explicitly adding the ID.
In one of my DetailViews, the Insert is working by removing all reference to the ID.
So THANKS.
My other DetailView is still broken, so there must be something else wrong with it.
By the way the Oracle expert at my company says that triggers are not recommended for high transaction sites, but since my Inserts are once a week at most, it is OK.
Michael