Pratik
Pratik

Reputation: 171

Error retrieving image from database

I have image table in my sql database with three column name id, name and images. when I try to retrieve the image it shows an error on the first line:

"Object reference not set to an instance of an object."

I want to see the name of image in the dropdownlist and image in image control.

SqlConnection con = new SqlConnection("Data Source=LOCALHOST\\SQLEXPRESS;Initial Catalog=testdb;Integrated Security=True");
        //SqlConnection con = new SqlConnection("Data Source=(localdb)\v11.0;Initial Catalog=tempdb;Integrated Security=True");
        protected void Page_Load(object sender, EventArgs e)
        {
            con.Open();

        SqlCommand cm = new SqlCommand("select * from image where name='" + DropDownList1.SelectedItem.ToString() + "'", con);
        SqlDataAdapter da = new SqlDataAdapter(cm);
        SqlDataReader dr = cm.ExecuteReader();
        try
        {
            if (dr.Read())
            {

                string image1 = Convert.ToString(DateTime.Now.ToFileTime());
                FileStream fs1 = new FileStream(image1, FileMode.CreateNew, FileAccess.Write);
                byte[] bimage1 = (byte[])dr["name"];
                fs1.Write(bimage1, 0, bimage1.Length - 1);
                fs1.Flush();
                Image1.ImageUrl = "~/Images/" + DropDownList1.SelectedItem.ToString();
                Image1.Visible = true;
            }
            dr.Close();
            con.Close();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

Upvotes: 0

Views: 254

Answers (4)

Dhaval
Dhaval

Reputation: 2861

I think you need to replace follwoing

 SqlCommand cm = new SqlCommand("select * from image where name='" + DropDownList1.SelectedItem.ToString() + "'", con);

to

SqlCommand cm = new SqlCommand("select * from image where name='" + Convert.ToString(DropDownList1.SelectedValue) + "'", con);

this might not give you the record you expect but it wont give you error .. try it!!

Upvotes: 0

you should change the code :

var index= DropDownList1.SelectedItem!=null?DropDownList1.SelectedItem.ToString():defaultitem;

then in that query u can use index.

Upvotes: 1

John
John

Reputation: 3546

You are doing something quite strange here. Looks like you are attempting to write an image to disk, but the bytes you specified are casted from the name column?

byte[] bimage1 = (byte[])dr["name"];
fs1.Write(bimage1, 0, bimage1.Length - 1);

if the url of the image is saved in the database you need to use WebClient to first download the bytes of the image before you can write the image out. It also looks like you are trying to show the image by assigning the url to Image control. However the exception is probably because there was no selected item in your dropdown list.

Upvotes: 0

Jordy van Eijk
Jordy van Eijk

Reputation: 2766

If you get a NullReferenceException on your SqlCommand line than your DropDownList1.SelectedItem is probably null. And calling ToString on a null object gives this NullReferenceException Please try and debug your program and hover over your SelectedItem to see if its null

Upvotes: 1

Related Questions