My code is view all the data in the gridview
Web.config code is
<configuration>
<connectionStrings>
<add name="ConStr" connectionString="DataSource=.;Integrated Security=SSPI;Initial catalog=sshopping"/>
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.5"/>
<httpRuntime targetFramework="4.5"/>
</system.web>
</configuration>
It is coded in external class
namespace DBAction
{
public class ViewAction
{
public DataSet GetAllData()
{
SqlCommand cmd = DataConnection.GetConnection().CreateCommand();
cmd.CommandText = "Select UserName,Password,RoleName,EmailID,SecurityQuestion,SecurityAnswer,LastLogin from LoginInfo";
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
cmd.Dispose();
DataConnection.CloseConnection();
return ds;
}
}
}
it is giving error in line da.Fill(ds)
The code to bind data source with gridview is coded on page load like this.
DataSet ds = new ViewAction().GetAllData();
gvLoginInfo.DataSource = ds;
gvLoginInfo.DataBind();
And conectionstring code in data connection class is
public static SqlConnection GetConnection()
{
if (con == null)
{
con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
con.Open();
}
return con;
}
And one one error is
Exception Details: System.ArgumentException: Keyword not supported: 'datasource'.
Source Error:
Line 19: {
Line 20: con = new SqlConnection();
Line 21: con.ConnectionString =ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
Line 22: con.Open();
Line 23: }
Solution 2string abc = ConfigurationSettings. AppSettings["databasepath"]; now pass it into sqlconnection. all the best.
Re: ConnectionString property has not been initialized- issue. Hi, This may happen if you are not populating the SQL connection string parameters (i.e. Server, Username, Password, etc) at install time into your config file.
config file in the Views folder.) Find the <connectionStrings> element: Add the following connection string to the <connectionStrings> element in the Web. config file.
The value of the ConnectionString property is a connection string that includes the source database name and the parameters you need to establish the connection. The default value of the ConnectionString property is an empty string. The Server attribute is mandatory in all situations.
The error is in the Web.Config only. Please put one space between DataSource in connectionString as: Data Source. Thus your connection String will become:
"Data Source=.;Integrated Security=SSPI;Initial catalog=sshopping".
From the examples i see online, in your connection string replace "DataSource" with "Data Source" (with a space between the two words). http://msdn.microsoft.com/en-us/library/ms156450.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With