Reputation: 2207
i am trying to display data from a database to the text box and keep getting the following error. Error 1 The type or namespace name 'DataTable' could not be found (are you missing a using directive or an assembly reference?)
SqlConnection sqlConnection = new SqlConnection("joanton7865org7272_youthpodcastConnectionString");
sqlConnection .Open();
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter("Select * from States", sqlConnection);
DataTable dt = new DataTable();
sqlDataAdapter.Fill(dt);
txtClubName.Text = (dt.Rows.Count > 0) ? Convert.ToString(dt.Rows[0]["ColumnName"]) : "";
sqlConnection .Open();
Upvotes: 2
Views: 16275
Reputation: 94
Struggled with the same error. After googling found the solution :
The System.Data namespace handles all of the major data-related objects like the DataTable.
You'll need to ensure that you have included it within a using statement at the top of your page (with the rest of your using statements) :
using System.Data;
If that doesn't work (which I am confident that it will), you will need to also check that the System.Data namespace is included within the References area of the application that it is being used in. If you don't see it listed under the References section in the Solution Explorer then you'll need to add it.
Steps For adding System.Data in References :
1. Right Click on References and then choose edit references.
2. Find System.Data and then select it.
3. Click OK.
Now, System.Data; should work and DataTable will also get recognised.
Link : https://forums.asp.net/t/1911684.aspx?Need+help+with+DataTable+in+class+C+.
Upvotes: 1
Reputation: 291
Add System.Data
Namespace
and Close sqlConnection at the end
sqlConnection.Close();
instead of sqlConnection.Open()
;
Upvotes: 2
Reputation: 48547
Click on the DataTable
and right mouse click and then click Resolve
. This will give you two options. One will add using System.Data;
and the other will do System.Data.DataTable
.
You could use the shortcut as well:
Alt+Shift+F10
Upvotes: 10