tags:

views:

37

answers:

2

I have a GridView with an Update button. I want to update a field in the database but I think the '@' in the code below is causing the problem in my ASP .NET page. What can be done within the grid or in the update (UpdateCommand) statement? Note, I get an Ora 00936 error.

<asp:SqlDataSource ID="dsBooks" runat="server"
            ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
            ConnectionString="<%$ ConnectionStrings:ConnectionString %>"         
            SelectCommand="SELECT OBJECTID, TRACT, GIS_ACRES, COMMENTS, PDF_STORAGE FROM CampusDev.CU_POLY ORDER BY OBJECTID"
            UpdateCommand="UPDATE CampusDev.CU_POLY SET COMMENTS='just atest' WHERE OBJECTID=@OBJECTID"
A: 

Oracle usually has : instead of @:

WHERE OBJECTID = :OBJECTID

Or perhaps objectid is a reserved word, which you can escape with " in Oracle:

WHERE "OBJECTID" = :OBJECTID

Or perhaps you'd have to specify a parameter to ASP.NET:

 <asp:SqlDataSource ...>
     <UpdateParameters>
         <asp:Parameter Type="Int32" Name="ObjectId" />
     </UpdateParameters>
 </asp:SqlDataSource>
Andomar
Wow! You guys are fast! I just tried your approach but now get:ORA-01036: illegal variable name/number New code:UpdateCommand="UPDATE CampusDev.CU_POLY SET COMMENTS='TEST COMMENT' WHERE 'OBJECTID'=:OBJECTID"
meengla
@meengla: `'OBJECTID'` is a plain string, you'd need `"OBJECTID" `(double quotes) to refer to a database field.
Andomar
Yeah, it probably doesn't like the :OBJECTID now... Any chance you can rename that one?
Justin K
I don't think I can rename that ObjectID. But...let me try your suggestion about the 'specify a parameter'. I know, I can also do this using the Code Behind file if I have to.Thanks for your help!
meengla
Nah, the 'specify a parameter' approach gives error:Attempted to read or write protected memory. This is often an indication that other memory is corrupt. <UpdateParameters> <asp:Parameter Type="Int32" Name=":OBJECTID" /> </UpdateParameters>
meengla
@meengla: Only prefix the name with `:` in SQL. In the `<asp:Parameter>` section, use the regular name. Another idea is to use `OracleType.Integer` as the datatype.
Andomar
Andomar, I tried your solution but get an error: ORA-01036: illegal variable name/numberI think anytime I put the : in front of OBJECTID in the Where clause of SQL I get this error.I will try to make the update work via a Code Behind file instead, per: http://forums.asp.net/t/1171415.aspxThank you guys for your help!
meengla
A: 

The ":OBJECTID" in Oracle parlance is a Bind Variable.

I'm ignorant of asp.net semantics, but you will want to use bind variables here. This link should provide a more complete explanation, but basically it's this:

cmd.Parameters.Add(new OracleParameter(“OBJECTID″, @OBJECTID));
UpdateCommand="UPDATE CampusDev.CU_POLY 
       SET COMMENTS='just atest' WHERE OBJECTID=:OBJECTID"

Then execute your command.

Always use bind variables where possible in production code - it allows Oracle to avoid hard parsing of the SQL statement.

Also, the name of the Bind Variable is unimportant to Oracle. The order in which it appears is the important aspect. You could just as easily say

WHERE OBJECTID=:1

with the same effect.

DCookie
DCookie: Your solution looks promising. While I have now moved to a Code Behind way to program this page I will still try your solution to learn something new--it may get handy one day.Thanks!
meengla
Slightly modified version of DCookie's solution worked in the code behind file: cmd.CommandText = "UPDATE CampusDev.CU_POLY SET COMMENTS = :COMMENTS WHERE OBJECTID = " + Request.QueryString["OBJECTID"];cmd.Parameters.Add(":COMMENTS", TextBox1.Text);Thank you all very much!
meengla