• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6242
  • Last Modified:

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>
0
MichaelLasell
Asked:
MichaelLasell
  • 3
  • 2
  • 2
1 Solution
 
slightwv (䄆 Netminder) Commented:
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
 
MichaelLasellAuthor Commented:
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
 
riyaz404Commented:
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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
MichaelLasellAuthor Commented:
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
 
slightwv (䄆 Netminder) Commented:
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
 
riyaz404Commented:
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
 
MichaelLasellAuthor Commented:
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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now