Reputation: 188
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" />
Upvotes: 2
Views: 24619
Reputation: 166
Check that you dont have the fill statement in another part of the code such as in the page load event.
Upvotes: 0
Reputation: 69749
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);
}
Upvotes: 7
Reputation: 13063
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.
Upvotes: 0
Reputation: 13486
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();
Upvotes: 7