asp.net - 将数据库值插入字符串数组 - 不起作用

标签 asp.net sql-server database

干杯,

我正在尝试将数据库值插入到我的字符串数组中。

出于某种原因,它说:

"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/

相关文章:

数据库设计?

MySQL 查询数据库大小

c# - 在 asp.net 网站中动态添加项目到菜单

c# - 从数据库中处理 100 万条记录的技术是什么

asp.net - 如何从 SQL 数据库流式传输 .flv 文件

sql - 计算过期的凭证,给定一个固定的窗口

sql-server - 从 MS SQL Server 使用 sp_send_dbmail 时如何设置自定义电子邮件 header ?

php - MySQL 表 - ID 字段中存在较大间隙

asp.net - 有人能够在 Visual Studio 2015 中使用 Angular 4 Quickstart 吗?

JQuery AJAX方法在JSONP请求上调用 "error"函数而不是 "success"函数