嗨,我正在尝试在树视图中实现数据库记录,如父子(表)。在运行时,我的连接字符串出现错误..
错误行:-
string connectionstring = ConfigurationManager.ConnectionStrings["Data Source=ASDF;Initial Catalog=dbtree;Integrated Security=True"].ConnectionString;
错误是:-
System.NullReferenceException was unhandled by user code
Object reference not set to an instance of an object.
注意:-在我的数据库中没有字段为空
代码:-
string connectionstring = ConfigurationManager.ConnectionStrings["Data Source=ASDF;Initial Catalog=dbtree;Integrated Security=True"].ConnectionString;
SqlConnection mycon = new SqlConnection(connectionstring);
mycon.Open();
SqlCommand mycmd = new SqlCommand("Select * from tbl_Parent", mycon);
SqlDataReader dr = mycmd.ExecuteReader();
mycmd.Dispose();
string[,] ParentNode = new string[100, 2];
int count = 0;
while (dr.Read())
{
ParentNode[count, 0] = dr.GetValue(dr.GetOrdinal("Parent_id")).ToString();
ParentNode[count++, 1] = dr.GetValue(dr.GetOrdinal("Parent_name")).ToString();
}
dr.Close();