7

I'm building up a website using C#, whenever I try this

SqlConnection con = new SqlConnection ();

I get the error that SqlConnection namespace isn't found, but when I do this

System.Data.SqlClient.SqlConnection con;

it works.

Tried to add reference to System.Data.SqlClient, but couldn't find it the references lists even though System.Data is added assembly in the web.config file.

3 Answers 3

6

First: Ensure you're referencing System.Data not System.Data.SqlClient

Then add

using System.Data.SqlClient;

To your namespaces

Sign up to request clarification or add additional context in comments.

6 Comments

@user2962142 Download ReSharper and things like these you won't even notice.
ops, i got an error, where im supposed to to add the using statement in the webpage ? i mean like inside the code blocks ? since im building a .net webpage
You use it in your c# file, what are you developing the web page in? MVC? Also can you paste the error please
No ordinary ASP.NET Razor webpages with the extension .cshtml, if i do "@using System.Data.sqlclient;" i get error saying "} expected".
Yeah that error is unrelated... Post a new question for it... include the lines of code that is causing you the issue in your new post too... :)
|
1

As the MSDN Documentation states, SqlConnection exists in the System.Data.SqlClient namespace, inside the System.Data assembly.

A class's namespace and assembly name do not always match.

Since System.Data.SqlClient.SqlConnection works for you, you must already have a reference to System.Data. To fix the error, add a using statement for System.Data.SqlClient to the top of your file.

If you're using Visual Studio, you can right-click on SqlConnection and let Visual Studio find the correct namespace and add a using for you. This works for any class where you already have a reference to the assembly that contains it.

1 Comment

I use "Visual Studio Code", dotnet add package System.Data.SqlClient and dotnet add package System.Data, but not found System.Data.SqlClient.SqlConnection
1

if you have this error in .net core , you shoud install System.Data.SqlClient Nuget package

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.