code_junkie

SqlDataReader throws NullReferenceException! what could cause this and how can I debug?

Nov 12th, 2011
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. 1) Exception Information
  2. *********************************************
  3. Exception Type: System.NullReferenceException
  4. Message: Object reference not set to an instance of an object.
  5. Data: System.Collections.ListDictionaryInternal
  6. TargetSite: Void Bind(System.Data.SqlClient.TdsParserStateObject)
  7. HelpLink: NULL
  8. Source: System.Data
  9.  
  10. StackTrace Information
  11. *********************************************
  12. at System.Data.SqlClient.SqlDataReader.Bind(TdsParserStateObject stateObj)
  13. at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
  14. at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
  15. at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult esult)
  16. at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
  17. at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
  18. at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
  19. at System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)
  20. at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
  21. at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
  22. at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet)
  23. at MyCode.Shared.Data.DataSocket.GetTable(String SPString)
  24. at <rest of stack trace>
  25.  
  26. public DataTable GetTable(string SPString)
  27. {
  28. //Setup the data objects by calling helper
  29. prepareDataAdaptor(SPString,CommandType.Text);
  30.  
  31. dataAdaptor.Fill(dataSet);
  32. dataAdaptor.SelectCommand.Connection.Close();
  33. DataTable dt;
  34. //ensure we dispose okay
  35. using(dataSet)
  36. {
  37.  
  38. if(dataSet.Tables.Count==0)
  39. {
  40. dataSet.Tables.Add(new DataTable("EmptyTable"));
  41. }
  42.  
  43. dt=dataSet.Tables[0];
  44. //because we are disposing we need to remove the table from the dataset
  45. dataSet.Tables.Clear();
  46.  
  47. }
  48. return dt;
  49. }
  50.  
  51. private void prepareDataAdaptor(string SPString,CommandType Type)
  52. {
  53. checkForConnection();
  54. dataSet=new DataSet();
  55. dbCommand.CommandText=SPString;
  56. dbCommand.CommandTimeout = MySettings.CommandTimeout;
  57. dataAdaptor.SelectCommand=dbCommand;
  58. dataAdaptor.SelectCommand.CommandType=Type;
  59. dataAdaptor.SelectCommand.Connection=dbConnection;
  60. dataAdaptor.SelectCommand.Parameters.Clear();
  61. }
  62.  
  63. dataAdaptor.SelectCommand.Connection.Close();
Add Comment
Please, Sign In to add comment