1

I have stored an image in SQL Server. First I have converted the image into bytes and I have saved it in SQL Server and very much done with saving.....when I try to invoke the respective image's bytes value from SQL Server which I saved and when I try to implement that in C#, I am getting a error like

NullReferenceException was unhandled by user code

Object Reference not set to an instance of an object

Here is my code:

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;  
}  

Kindly have a look on it and help me to get out from this friends.

Community
  • 1
  • 1
Rajesh Kumar
  • 21
  • 1
  • 6
  • 2
    This question, the edit by Soner Gonul seems very very familiar. I had like 10 Deja-Vus right now. Tell me if you have asked this question before? – Aniket Inge Mar 17 '13 at 09:14
  • Read this question http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net Debug your code and find which line throws this _exception_ and fix it. Probably one of your variable is `null` and you try to use before you initialize it. – Soner Gönül Mar 17 '13 at 09:18
  • Can you post the stacktrace? or better post your stored procedure..Probably this part is return null: `Byte[] bytes = command.ExecuteScalar() as byte[];` – Iswanto San Mar 17 '13 at 09:21
  • yeah thank u friends :) error was in the line Byte[] bytes = command.ExecuteScalar() as byte[];...error with the procedure returning the value.....now i got the answer.....thanks a lot for your help friends :) – Rajesh Kumar Mar 17 '13 at 09:28
  • Great..can you post your stored procedure? and make sure there is row with id = `imageID`. – Iswanto San Mar 17 '13 at 09:31

3 Answers3

0

Before NullReferanceException, I don't believe this lines don't throw any exception..

SqlConnection connection = new SqlConnection();  
connection.ConnectionString = @"";  
connection.Open();

You can't open a connection before you initialize SqlConnection.ConnectionString property. In your case, your connection string is an empty string. How do you expect this connection connect to your sql server?

My suggestion is, read a book about C# and using it with databases like Begining C# 5.0 Databases

For NullReferanceException, I already mentioned in my comment what you should do.

Community
  • 1
  • 1
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
0

It's likely that you're getting a NullReferenceException from your call to ExecuteScalar. From the documentation:

Return Value

The first column of the first row in the result set, or a null reference ... if the result set is empty.

Emphasis mine.

And for what it's worth, SqlConnection, SqlCommand, DataSet and SqlDataAdapter are all IDisposable. You should call Dispose on them, although the value of disposing DataSet is arguable.

Community
  • 1
  • 1
ta.speot.is
  • 26,914
  • 8
  • 68
  • 96
  • the problem was with procedure only...i cleared it and now the error got done ..... thanks a lot for your help friend :) – Rajesh Kumar Mar 18 '13 at 06:23
0

Try running the stored procedure in SQL Server to see if it returns any value using

EXEC uspgetimage 'COmma seperated Parameter List'
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Sohanit
  • 1
  • 2