【发布时间】:2012-11-29 04:52:35
【问题描述】:
我正在尝试使用以下代码将我的 sql 表与网格视图绑定:
string connectionString = WebConfigurationManager.ConnectionStrings["Gen_Lic"].ConnectionString; //<-- error
string selectSQL = "SELECT * FROM Gen_Lic";
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(selectSQL, con);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapter.Fill(ds, "Gen_Lic");
Gen_Lic_Grid.DataSource = ds;
Gen_Lic_Grid.DataBind();
但我不知道我哪里出了问题,但我收到了一个错误:
Object reference not set to an instance of an object.
有什么建议吗?
这是我的 web.config 文件:
<connectionStrings>
<add name="Gen_LicConnectionString" connectionString="Data Source=ESHA\SQLEXPRESS;Initial Catalog=Gen_Lic;User ID=sa;Password=sa@" providerName="System.Data.SqlClient" />
</connectionStrings>
【问题讨论】:
-
设置断点并确定出错的行
-
web.config 中存在连接字符串“Gen_Lic”?使用 SQL Server 管理工作室检查它
-
在第一行用这个“Gen_LicConnectionString”替换这个“Gen_Lic”
-
@Syrion 不客气。
标签: c# asp.net data-binding gridview