Solved

Oracle Sequence/ ASP.NET C# problem

Posted on 2006-11-09
7
6,094 Views
Last Modified: 2011-08-18
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>
0
Comment
Question by:MichaelLasell
  • 3
  • 2
  • 2
7 Comments
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 17909515
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.
0
 

Author Comment

by:MichaelLasell
ID: 17910053
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>
0
 
LVL 1

Expert Comment

by:riyaz404
ID: 17911787
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;
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:MichaelLasell
ID: 17917555
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;
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 17929728
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.
0
 
LVL 1

Accepted Solution

by:
riyaz404 earned 400 total points
ID: 17931996
You are not setting the :new.FILING_NUMBER with the newly retrieved filingid (from sequence)

Once you do that, your problem will be resolved..
create or replace TRIGGER triggername
BEFORE INSERT
ON schema.table
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
DECLARE
  Filingid  NUMBER;
BEGIN
   if :new.Filingid is null then
         SELECT Filing_Seq.NEXTVAL INTO Filingid FROM dual;
         :NEW.FILING_NUMBER := Filingid ;
   end if;  
end;



0
 

Author Comment

by:MichaelLasell
ID: 17933843
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
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now