我的代碼是查看在GridView所有數據解決錯誤「ConnectionString屬性尚未初始化。」?
的Web.config代碼
<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>
它在外部類是編碼
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;
}
}
}
它給誤差管線da.Fill(ds)
的用gridview綁定數據源的代碼在頁面加載時就像這樣編碼。
DataSet ds = new ViewAction().GetAllData();
gvLoginInfo.DataSource = ds;
gvLoginInfo.DataBind();
而且conectionstring在數據連接類代碼是
public static SqlConnection GetConnection()
{
if (con == null)
{
con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
con.Open();
}
return con;
}
而一個一個錯誤是
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: }
...並且您有一個空的Exception塊。在解決問題出現的所有麻煩之前檢查一下。 –
你可以從你的web.config文件發佈你的連接字符串嗎? – asafrob
是的,先生我發佈了@asafrob – VJain