【问题标题】:Retrieving image from sql table using a file path, code error使用文件路径从 sql 表中检索图像,代码错误
【发布时间】:2009-12-02 11:07:15
【问题描述】:

一个名叫 Darin 的好心人为我提供了一些代码,以便我通过文件路径检索图像。

但是,当我尝试执行代码时,我在 var 的第一行收到“NullReferenceException 未被用户代码处理;使用 'new' 关键字创建对象实例”。

代码如下:

   var connectionString = ConfigurationManager.ConnectionStrings["SomeCN"].ConnectionString;
        using (var cn = new SqlConnection("Data Source=STRSQL04;Initial Catalog=PDC;Integrated Security=True"))
        using (var cmd = cn.CreateCommand())
        {
            cn.Open();
            cmd.CommandText = "Select imageID from accounts where MemberID = FM00012";
            cmd.Parameters.AddWithValue("FM00012",5);
            using (var reader = cmd.ExecuteReader())
            {
                if (reader.Read())
                {
                    var filepath = reader.GetString(0);
                Image1.ImageUrl = filepath;
                }
            }
        }

有人可以指出我的错误吗?

一如既往地向我提出一些我怀疑是荒谬的问题表示歉意。

【问题讨论】:

    标签: c# asp.net image


    【解决方案1】:

    您的应用配置中没有名为“SomeCN”(或您实际使用的任何内容)的连接字符串,因此当您尝试访问 ConnectionString.ConnectionString 参数时,它会抛出一个 nullref。

    您能否发布您的app.config 或至少ConnectionStrings 元素的内容,以便我们查看?

    【讨论】:

    • 好吧,我需要在我的配置中设置我的连接字符串。我会去研究如何做到这一点!
    • 已排序,无论如何它都在我的 webconfig 文件中。最重要的东西,感谢您指出/推动我朝着正确的方向前进!
    猜你喜欢
    • 2012-05-22
    • 2019-01-05
    • 2017-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-28
    相关资源
    最近更新 更多