2

I'm using VS2012 and SQL Server Express 2008. I've boiled down my connection/query to try and find out why my DataSet is not being filled. The connection is completed successfully, and no exceptions are thrown, but the adapter does not fill the DataSet. The database that this is pulling from is on the same PC and using localhost\SQLEXPRESS does not change the result. Thanks for any input!

SqlConnection questionConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
questionConnection.Open();
String sql = "SELECT * FROM HRA.dbo.Questions";
SqlDataAdapter adapter = new SqlDataAdapter();
DataSet ds = new DataSet();
SqlCommand command = new SqlCommand(sql, questionConnection);
adapter.SelectCommand = command;
adapter.Fill(ds);
adapter.Dispose();
command.Dispose();
questionConnection.Close();

Connection String:

<add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=CODING-PC\SQLEXPRESS;Initial Catalog=HRA;User ID=sa; Password= TEST" />
MarioDS
  • 12,895
  • 15
  • 65
  • 121
Tucker
  • 188
  • 2
  • 4
  • 15

4 Answers4

7

try this:

SqlConnection questionConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
questionConnection.Open();
DataSet ds = new DataSet();
String sql = "SELECT * FROM HRA.dbo.Questions";
SqlDataAdapter adapter = new SqlDataAdapter(sql, questionConnection);
adapter.Fill(ds);

adapter.Dispose();
command.Dispose();
questionConnection.Close();
AnandPhadke
  • 13,160
  • 5
  • 26
  • 33
7

It appears the answer has already been found, however, I am posting one anyway to advocate the use of using blocks, and also letting the .NET framework do the hardwork for you. Your 11 lines of code can be rewritten in 3:

DataSet ds = new DataSet();
using (var adapter = new SqlDataAdapter("SELECT * FROM HRA.dbo.Questions", ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
{
    adapter.Fill(ds);
}
GarethD
  • 68,045
  • 10
  • 83
  • 123
0

Is "HRA.dbo.Questions" an actual table?

Your connectionstring contains an Initial Catalog with value "HRA", and the SQL should only contain dbo.Questions as far as I know.

MarioDS
  • 12,895
  • 15
  • 65
  • 121
0

Check that you dont have the fill statement in another part of the code such as in the page load event.