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" />
1
  • Check data is there are not........... you will get data in ds.Tables[0] Commented Oct 30, 2012 at 12:07

4 Answers 4

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();
Sign up to request clarification or add additional context in comments.

1 Comment

Can you explain the difference? "Try this" is not really helpful.
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);
}

Comments

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.

1 Comment

This does not matter here.Connection string property is just to connect to the server and DB.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.