Showing posts with label Asp.Net Error. Show all posts
Showing posts with label Asp.Net Error. Show all posts

Friday, June 29, 2012

System.ArgumentException: Invalid postback or callback argument


This error occurs when the mapping between the controls in the .aspx page and the corresponding events in the code-behind file is changed.

For example if you try to fire the Button1_Click event while clicking Button2 then this error will occur.

Invalid postback or callback argument.  Event validation is enabled using &ltlpages enableEventValidation="true"/> in configuration or &ltl%@ Page EnableEventValidation="true" %> in a page.  For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.  If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.

This is done explicitly by Asp.net to make sure that there are no breach is security, to prevent some hacking script to trigger the code-behind events.

Solution
1. If your application is not a very critical or if it a restricted intranet application then you can set the
enableEventValidation attribute in the @Page directive to false, this will prevent Asp.net from checking the control-event mappings

EnableEventValidation="false"

2. If your application is critical then you can use the ClientScriptManager.RegisterForEventValidation method to register the postback.

Tuesday, June 19, 2012

ORA-01460: unimplemented or unreasonable conversion requested


While trying to INSERT a BLOB type to Oracle from .Net the following error will be thrown, in you try to INSERT the BLOB as part of a procedure or a Batch statement BEGIN .. END;

[System.Data.OracleClient.OracleException] = {"ORA-01460: unimplemented or unreasonable conversion requested\n"}

Make sure that the statement used to insert the BLOG type is an individual SQL statement, it should not be a Procedure or a Batch statement BEGIN … END;

The below code used a batch statement BEGIN .. END to insert a BLOB and will fail with the above exception.

// Get the File name and Extension
strFileName = Path.GetFileName(file.FileName);
strFileExtension = Path.GetExtension(file.FileName);
objDAL = new DataAccessLayer();
//
// Extract the content of the Document into a Byte array
int intlength = file.ContentLength;
Byte[] byteData = new Byte[intlength];
file.InputStream.Read(byteData, 0, intlength);
//
// Save the file to the DB
string strConn = objDAL.strConnection;
objConn = new OracleConnection(strConn);
//
strQuery = "BEGIN";
strQuery += " DELETE FROM DOCUMENTS WHERE PROJECT_ID = '" + ProjectID.ToString() + "';";
strQuery += " INSERT INTO DOCUMENTS(PROJECT_ID, DOCUMENT_ID, DOCUMENT_NAME, DOCUMENT_TYPE, DOCUMENT) VALUES (";
strQuery += "'" + ProjectID.ToString() + "', ";
strQuery += "'" + intDocID.ToString() + "', ";
strQuery += "'" + strFileName + "', ";
strQuery += "'" + strFileExtension + "', ";
strQuery += ":Document);";
strQuery += " END;";
//
OracleParameter blobParameter = new OracleParameter();
blobParameter.ParameterName = "Document";
blobParameter.OracleType = OracleType.Blob;
blobParameter.Direction = ParameterDirection.Input;
blobParameter.Value = byteData;

objCmd = new OracleCommand(strQuery, objConn);
objCmd.Parameters.Add(blobParameter);
//
objConn.Open();
objCmd.ExecuteNonQuery();
objConn.Close();

SOLUTION
Split the Batch into individual statement, make sure that the INSERT statement used to insert the BLOG statement is an individual SQL Statement. Replacing the code as follows solves the issue.

string strConn = objDAL.strConnection;
objConn = new OracleConnection(strConn);
//
strQuery = "DELETE FROM DOCUMENTS WHERE PROJECT_ID = '" + ProjectID.ToString() + "'";
objDAL.ExcuteQuery(strQuery);
//
strQuery = "INSERT INTO DOCUMENTS (PROJECT_ID, DOCUMENT_ID, DOCUMENT_NAME, DOCUMENT_TYPE, DOCUMENT) VALUES (";
strQuery += "'" + ProjectID.ToString() + "', ";
strQuery += "'" + intDocID.ToString() + "', ";
strQuery += "'" + strFileName + "', ";
strQuery += "'" + strFileExtension + "', ";
strQuery += ":Document)";
//
OracleParameter blobParameter = new OracleParameter();
blobParameter.ParameterName = "Document";
blobParameter.OracleType = OracleType.Blob;
blobParameter.Direction = ParameterDirection.Input;
blobParameter.Value = byteData;

objCmd = new OracleCommand(strQuery, objConn);
objCmd.Parameters.Add(blobParameter);
//
objConn.Open();
objCmd.ExecuteNonQuery();

RELATED POST

Monday, June 4, 2012

ORA-00911: invalid character\n in Asp.Net


I got the error ORA-00911: invalid character\n while trying to execute the following Oracle query from Asp.Net

string strQuery = "SELECT * FROM PROJECTS;";

Tried to figure out what the \n meant, but surprisingly the reason for the error had nothing to do with the \n

Cause:
The additional ";" at the end of the SELECT query

Resolution:
Remove the ";" and the query will start working
string strQuery = "SELECT * FROM PROJECTS";
 
Additional Information:
The query will still work even with the ";" in Oracle editors like Oracle SQL Developer, but will throw the exception ORA-00911: invalid character\n in .Net, this made it even difficult to debug the issue, since the same query works fine in Oracle, but throws an exception when called from .Net, finally removed the ";" and resolved the issue.

The ";" is required only while running the query inside a code block like BEGIN.... END;

string strQuery = "BEGIN UPDATE PROJECTS SET Name = 'Dashboard Project' WHERE PROJECT_ID = 100; END;";

The above query works fine without any issues sicne it is placed in a code block, but a single SQL statement will throw the exception ORA-00911: invalid character\n, when a ";" is placed at the end.



Tuesday, May 29, 2012

Asp.Net designer file not updating (VS.Net 2008)

Problem
In Visual Studio 2008, i came across a strange error (The name 'xxxxx' does not exist in the current context), any additions/changes done to the control names in the .aspx/.ascx files were not getting updated to the designer.cs file as a result the following error was thrown while building the project

The name 'xxxxx' does not exist in the current context
D:\\Source Code\Programming\ucSysInfo.ascx.cs 226 53

This was because there was a mismatch between the names of the controls name in the .ascx and the .ascx.cs.designer files, ideally the designer file should get updated once the name is changed in the .aspx/.ascx files, but at times this is not happening leading to the issue. 

Solution
The following workaround helped me overcome this issue

1. Close the .aspx/ascx files and the .cs files, if it is opened in the Editor
2. Delete the designer file corresponding to the .aspx/.ascx file (in my case it was ucSysInfo.ascx.cs.designer)
3. Right click on the .aspx/.ascx file in the Solution Explorer
4. Select the option Convert to Web Application.
5. That's it the designer file will now get recreated with the new/updated control names.

The Project should now BUILD without any errors.