Web Service returning a null dataset occasionally
I am having the most frustrating problem, basically I have a website and a webserv开发者_开发技巧ice running on the same server. Both use ADO.net to connect to data tables using a couple of custom calls I have created myself, the website has never had a problem with connecting to a particular proc to return data, however the webservice, once in say every 100 calls to that proc, returns an empty dataset even though it should have come back populated and does in a query in SQL Mgmt Studio. The weird thing is it works most times, but on the odd occasion returns this error:
System.IndexOutOfRangeException: Cannot find table 0. at System.Data.DataTableCollection.get_Item(Int32 index)
Dim SQLCmd As SqlCommand = CreateSPCommand("VerifyCredentialsSP")
SQLCmd.Parameters.AddWithValue("@Password", Credentials.Password)
GetData(SQLCmd)
ds.DataSetName = "Customer"
If ds.Tables(0) IsNot Nothing Then
ds.Tables(0).TableName = "Customer"
End If
One way to do this is to catch the exception being thrown, but the better method is to check for null
or nothing
in your case.
Do not access the index ds.Tables(0)...
.
Do a check if your dataset ds
is null
before accessing it like so:
If ds IsNot Nothing then
'only then can you index ds.
end if
In this way you avoid a lookup on the index of your dataset given that it contains some valid reference. In your method you are accessing Tables(0)
which may or may not exist, without a valid check your code could potentially throw an exception, and in this case it has!
精彩评论