干杯,
我正在尝试将数据库值插入到我的字符串数组中。
出于某种原因,它说:
"Object reference not set to an instance of an object."
这是我的代码:
if (IsPostBack)
{
if (RadioWords.Checked == true)
{
con = new SqlConnection("Data Source=MICROSOF-58B8A5\\SQL_SERVER_R2;Initial Catalog=Daniel;Integrated Security=True");
con.Open();
string SqlCount = "SELECT COUNT(*) FROM WordGame";
SqlCommand command = new SqlCommand(SqlCount, con);
//Sets an array of the size of the database.
int count = (Int32)command.ExecuteScalar();
arrOfWords = new string[count];
//Initialize the words in the array.
for (int i = 0; i < arrOfWords.Length; i++)
{
int GetRandomNumber = rnd.Next(1, arrOfWords.Length);
string Sqlinsert = "SELECT Word FROM WordGame WHERE ID='"+GetRandomNumber+"'";
SqlCommand commandToRandom = new SqlCommand(Sqlinsert, con);
arrOfWords[i] = commandToRandom.ExecuteScalar().ToString();
}
}
及其引用这一行:
int GetRandomNumber = rnd.Next(1, arrOfWords.Length);
感谢各位的帮助!
最佳答案
rnd 为 null ,添加一行
rnd = new Random();
事件开始时
关于asp.net - 将数据库值插入字符串数组 - 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8694262/