Reputation: 127
I am using Visual Studio 2013. Created Web Forms Project. Added some stuff and needed database. Also, added database using Add Item. Added some stuff to a database. Use connection string from Web Config. When connection is made, i got some errors.
Here is my connection string >
<connectionStrings>
<add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-mywebsite-20150813211505;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-mywebsite-20150813211505.mdf" />
</connectionStrings>
C# code >
SqlConnection con = new SqlConnection(@"Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-mywebsite-20150813211505;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-mywebsite-20150813211505.mdf");
SqlCommand cmd = new SqlCommand("select * from Users;");
con.Open();
DataSet ds = new DataSet(cmd, con);
SqlDataAdapter da = new SqlDataAdapter();
da.Fill(ds);
con.Close();
here is the errors
Error 1
The best overloaded method match for 'System.Data.DataSet.DataSet(System.Runtime.Serialization.SerializationInfo, System.Runtime.Serialization.StreamingContext)' has some invalid arguments c:\users***\documents\visual studio 2013\Project\Default.aspx.cs 30 26 mywebsite
Error 2
Argument 1: cannot convert from 'System.Data.SqlClient.SqlCommand' to 'System.Runtime.Serialization.SerializationInfo' c:\users***\documents\visual studio 2013\Projects\Default.aspx.cs 30 38 Administration Website
Error 3
Argument 2: cannot convert from 'System.Data.SqlClient.SqlConnection' to 'System.Runtime.Serialization.StreamingContext' c:\users***\documents\visual studio 2013\Projects\Default.aspx.cs 30 43 mywebsite
When error is clicked it's focus on DataSet.
Help ?? Some better idea to connect to my database ?
Thank you in advance !
Upvotes: 0
Views: 134
Reputation: 216313
You have made some errors in initializing your DataSet and your SqlDataAdapter, the correct way is
using(SqlConnection con = new SqlConnection(....))
{
SqlCommand cmd = new SqlCommand("select * from Users;");
cmd.Connection = con;
con.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
}
It is the SqlDataAdapter
instance that needs the SqlCommand
as parameter for one of its constructors, as you can easily spot on MSDN page. Then the connection is bound the the SqlCommand.Connection
property and you should enclose the creation of the SqlConnection
in a using statement to be sure that it is closed and disposed at the end of the using block
You can also remove two lines from the code above passing the connection directly to the constructor of the SqlCommand (again MSDN pages are a treasure of information) and remove the opening of the connection because, if the Adapter finds the connection closed, it opens and closes it automatically
About a different topic, you have correctly stored your connection string in the config file of your application. This allows you to easily change it (if the need arises) But you have used an hard coded connection string. This should be changed to something like this
string connString2 = WebConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
Upvotes: 1