我在SQL Server中存储了一个映像。首先,我已经将图像转换为字节,并将其保存在SQL Server中,并且在保存时完成了很多工作.....当我尝试从SQL Server中调用各自图像的字节值时,在C#中,我由用户代码越来越像从数据库调用字节时发生错误
的NullReferenceException一个错误是未处理的未设置为一个对象
下面的实例
对象引用是我的代码:
protected void GetImageButton_OnClick(object sender, EventArgs e)
{
SqlConnection connection = new SqlConnection();
connection.ConnectionString = @"";
connection.Open();
SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "uspgetimage";
command.Parameters.AddWithValue("@imageID", getimagebutton.Text);
DataSet dataset = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = command;
adapter.Fill(dataset);
Byte[] bytes = command.ExecuteScalar() as byte[];
string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
Image.ImageUrl = "data:image/jpeg;base64," + base64String;
}
请看看它,并帮助我摆脱这个朋友。
这个问题的任何值,由Soner格尼尔编辑似乎很很熟悉。我现在有10个Deja-Vus。告诉我你以前是否问过这个问题? – 2013-03-17 09:14:12
阅读这个问题http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net调试你的代码,并找到哪一行抛出_exception_并修复它。可能你的一个变量是'null',并且在初始化之前尝试使用它。 – 2013-03-17 09:18:02
你可以发布stacktrace吗?或更好地发布您的存储过程..可能是这部分返回null:'Byte [] bytes = command.ExecuteScalar()as byte [];' – 2013-03-17 09:21:44