Why check for null reference in session variables results in ArgumentOutOfRangeException? - asp.net

First line generating an exception when run at remote server.
No error when running the code via IIS Express at my development notebook.
if (Session["LdapData"] == null) {
// do something
}
Exception:
[ArgumentOutOfRangeException: Der Index lag außerhalb des Bereichs. Er
darf nicht negativ und kleiner als die Sammlung sein. Parametername:
index] System.Collections.ArrayList.get_Item(Int32 index) +14539412
System.DirectoryServices.ResultPropertyValueCollection.get_Item(Int32
index) +93
Microsoft does it exactly the same way:
https://msdn.microsoft.com/de-de/library/03sekbw5(v=vs.100).aspx
if (Session["City"] == null)
// No such value in session state; take appropriate action.
Any ideas for debugging?
Edit:
Also read this articles, but didn´t get the point:
Difference between Session and HttpContext.Current.Session
How to use sessions in an ASP.NET MVC 4 application?

The exception was not related to the quoted line.
Don´t know why, but the displayed line number generated by the IIS is incorrect. Indeed the exception was caused by an ldap query some lines below in the code.

Related

Strange ZQuery behavior

I'm using Zeos and SQLite3 DB in Delphi
ZQuery2.Close;
ZQuery2.SQL.Clear;
ZQuery2.SQL.Add('SELECT * FROM users WHERE un = ' + QuotedStr( UserName ) );
ZQuery2.Open;
OutputDebugString(PWideChar( ZQuery2.FieldDefList.CommaText )); // log : id,un,pw
OutputDebugString(PWideChar(ZQuery2.FieldByName('pw').AsString)); //causes error sometimes
the code is working but sometimes I get the following error message
Exception class EDatabaseError with message 'ZQuery2:Field'pw' not found'.
This is odd because a field of a dataset shouldn't just disappear while the app is in the middle of running, especially if other fields are still operating normally. So, I would suspect something like a memory overwrite being the cause.
Memory overwrites usually happen when something is written to the wrong place in memory, overwriting what is there, usually because of an incorrect pointer value or a so-called "buffer overrun" where the writing operation carries on beyond where is should stop. Usually, the pointer value is so wildly wrong that the OS can detect it and raise an AV, but sometimes it is less obvious.
Delphi's memory manager has a 'full debug mode' which adds special checks for this condition, see here.
I suggest you enable full debug mode as per the linked document and wait for the exception to occur.

Email from lookup field throwing error

We recently updated our CRM 2013 update to SP1. Afterwards the lookup field on the email form taking care of the from field, throws an error when clicked on.
When running diagnostics tool and reading with trace log viewer i see following error.
'<', hexadecimal value 0x3C, is an invalid attribute character. Line 7, position 58.
at System.Xml.XmlTextReaderImpl.Throw(String res, String[] args)
RequestUrl: http://mypage.com/company/AppWebServices/LookupService.asmx
I can see that different users have created emails and used the from field after i updated to SP1. So does anyone have an idea what might cause this?
Update: If i click on the field itself and delete the user the lookup works without problem. It is only if there is allready a user in the field and i try to lookup that an error is shown.

How to get error code returned by query using MySQL in Asp.Net

I am working with MySQL in my .net web app with the help of MySQL connector 5.0.9.0.
I am getting data from a web service and inserting it in local database. I only want to insert unique rows in my local database. I came to know that when an duplication occurs, MySQL returned an error code: 1062.
All i want to know that how i could i get this code in a variable and check it against my condition like:
if(errorCode == "1062")
{
Response.Write("Record already exists");
}
else
{
// add the record to database
}
Thanx in advance
Have a look at MySqlException class members. There is a Number property - Gets a number that identifies the type of error.
MySqlException Class.

CreateIndexBuffer() crashes,some kind of pointer error.What could be causing this?

I get
Unhandled exception at 0x004687b4 in D3DTest.exe: 0xC0000005: Access violation reading location 0x00000000.
the error is at:
m_d3dDevice->CreateIndexBuffer(sizeof(short)*CHUNK_PRIMITIVES*3,D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &m_ib, NULL);
Now I checked m_d3dDevice and it's all OK,everything works properly if I don't create the buffer.
m_ib is also propery created before being used in that function:
LPDIRECT3DVERTEXBUFFER9 m_vb;
m_vb = NULL;
I don't think anything else could be causing the problem.I'm confused.
Try enabling the debug runtime from the DirectX control panel that will show you warnings and errors if you have any and always check the return codes from DX functions.
Showing the code for the whole function that creates the index buffer would help too.

ASP.NET Unexpected and Different Behavior in Different Environments

I have an ASP.NET site (VB.NET) that I'm trying to clean up. When it was originally created it was written with no error handling, and I'm trying to add it in to improve the User Experience.
Try
If Not String.IsNullOrEmpty(strMfgName) And Not String.IsNullOrEmpty(strSortType) Then
If Integer.TryParse(Request.QueryString("CategoryID"), i) And String.IsNullOrEmpty(Request.QueryString("CategoryID"))
MyDataGrid.DataSource = ProductCategoryDB.GetMfgItems(strMfgName, strSortType, i)
Else
MyDataGrid.DataSource = ProductCategoryDB.GetMfgItems(strMfgName, strSortType)
End If
MyDataGrid.DataBind()
If CType(MyDataGrid.DataSource, DataSet).Tables("Data").Rows.Count > 0 Then
lblCatName.Text = CType(MyDataGrid.DataSource, DataSet).Tables("Data").Rows(0).Item("mfgName")
End If
If MyDataGrid.Items.Count < 2 Then
cboSortTypes.Visible = False
table_search.Visible = False
End If
If MyDataGrid.PageCount < 2 Then
MyDataGrid.PagerStyle.Visible = False
End If
Else
lblCatName.Text &= "<br /><span style=""fontf-size: 12px;"">There are no items for this manufacturer</span>"
MyDataGrid.Visible = False
table_search.Visible = False
End If
Catch
lblCatName.Text &= "<br /><span style=""font-size: 12px;"">There are no items for this manufacturer</span>"
MyDataGrid.Visible = False
table_search.Visible = False
End Try
Now, this is trying to avoid generating a 500 error by catching exceptions. There can be three items on the query string, but only two matter here. In my test environment and in Visual Studio when I run this site, it doesn't matter if that item is on the query string. In production, it does matter. If that third item isn't present (SubCategoryID) on the query string, then the "There are no items for this manufacturer" displays instead of the data from the database.
In the two different environments I am seeing two different code execution paths, despite the same URLs and the same code base.
The site is running on Server 2003 with IIS 6.
Thoughts?
EDIT:
In response to the answer below, I doubt it's a connection error (though I see what you're getting to), as when I add the SubCategoryID to the query string, the site works correctly (displaying data from the database).
Also, if please let me know if you have any suggestions for how to test this scenario, without deploying the code back to production (it's been rolled back).
I think you should try to print out the exception details in your catch block to see what the problem is. It could anything for example a connection error to your database.
The error could be anything, and you should definitely consider printing this out or logging it somewhere, rather than making the assumption that there's no data. You're also outputting the same error message to the UI for two different code paths, which makes things harder to debug, especially without knowing if an exception occurred, and if so, what it was.
Generally, it's also better not to have a catch for all exceptions in cases like this, especially without logging the error. Instead, you should catch specific exceptions and handle these appropriately, and any real exceptions can get passed up the stack, ideally to a global error handler which can log it and/or send out some kind of error notification.
I discovered the reason yesterday. In short it was because when I copied my files from my computer into my dev-test environment, I missed a file, which ironically caused it to work, rather than not. So in the end it would have functioned the same in both environments.

Resources