Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Oracle Sequence/ ASP.NET C# problem

Posted on 2006-11-09
7
Medium Priority
?
6,201 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 77

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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

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 77

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 1200 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

704 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