public partial class datasetDataGrid : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dgrdTest;
protected void Page_Load(object sender, EventArgs e)
{
datagrid_load(sender, e);
}
protected void datagrid_load(object sender, EventArgs e)
{
string strConn = "server=localhost;uid=sa;password=987654321;database=testDB";
SqlConnection conn = new SqlConnection(strConn);
string strSql = "select * from test";
SqlCommand myCommand = new SqlCommand(strSql, conn);
conn.Open();
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = myCommand;
DataSet myDs = new DataSet();
adapter.Fill(myDs, "Test");
conn.Close();
dgrdTest.DataSource = myDs.Tables[0].DefaultView;
dgrdTest.DataBind();
}
}
异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。