Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi!
I am new on C#.net so i don't have enough knowledge to solve the problem. I tried to retrive 3 images of database "Image" using stored procedure "ReadImage" which is like this

SQL
CREATE proc [dbo].[ReadImage]
@Regid int
as
SELECT [Citizenimage],[Otherimage],[Othersimage] FROM [Image]
WHERE [Reg_id]= @Regid


and code goes like this

C#
private void btnSearch_Click(object sender, EventArgs e)
        {
            SqlConnection con = SqlConnect.DBConnect();
            SqlCommand cmd = new SqlCommand("ReadImage", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@Regid", txtRegid.Text);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet dataSet = new DataSet();
            da.Fill(dataSet);
            if (dataSet.Tables[0].Rows.Count == 1)
            {
                        Byte[] data = new Byte[0];
                        data = (Byte[])(dataSet.Tables[0].Rows[0]["Citizenimage"]);
                        MemoryStream mem = new MemoryStream(data);
                        PbCitizen.Image = Bitmap.FromStream(mem);

                        Byte[] data1 = new Byte[0];
                        data1 = (Byte[])(dataSet.Tables[0].Rows[0]["Otherimage"]);
                        MemoryStream mem1 = new MemoryStream(data1);
                        pbBack.Image = Bitmap.FromStream(mem1);

                        Byte[] data2 = new Byte[2];
                        data2 = (Byte[])(dataSet.Tables[0].Rows[0]["Othersimage"]);
                        MemoryStream mem2 = new MemoryStream(data2);
                        pbOthers.Image = Image.FromStream(mem2);
                }
                con.Close();
            }


There occurs error on "mem1" as no valid parameter is passed.So anyone please help me in this topic you will be highly appreciated...
Thank You!
Posted
Updated 14-Aug-12 21:47pm
v2
Comments
Kenneth Haugland 15-Aug-12 5:30am    
Did you run your code through debug, maybe you have spelled the Columname worng or something like that?

1 solution

Hey did some test(ie writing the binary returned from DB to File), I did some modifications to work it for all scenarios( ie check for any Image is Null):

C#
private static void Search(int Regid)
 {
     SqlConnection con = new SqlConnection();
     con.ConnectionString = "Data Source=RKUTHUPARA;Initial Catalog=Ops;Integrated Security=SSPI;Connect Timeout=60;";
     SqlCommand cmd = new SqlCommand("ReadImage", con);
     cmd.CommandType = CommandType.StoredProcedure;
     cmd.Parameters.AddWithValue("@Regid", Regid);
     SqlDataAdapter da = new SqlDataAdapter(cmd);
     DataSet dataSet = new DataSet();
     da.Fill(dataSet);
     con.Close();
     if (dataSet.Tables[0].Rows.Count == 1)
     {
         Byte[] data = new Byte[0];
         data = (Byte[])(dataSet.Tables[0].Rows[0]["Citizenimage"] is DBNull? null : dataSet.Tables[0].Rows[0]["Citizenimage"]);
         if (data != null)
         {
             //MemoryStream mem = new MemoryStream(data);
             File.WriteAllBytes(@"C:\test\Images\DB" + Regid + ".1.png", data);
         }



         Byte[] data1 = new Byte[0];
         data1 = (Byte[])(dataSet.Tables[0].Rows[0]["Otherimage"] is DBNull ? null : dataSet.Tables[0].Rows[0]["Otherimage"]);
         if (data1 != null)
         {
             //MemoryStream mem1 = new MemoryStream(data1);
             File.WriteAllBytes(@"C:\test\Images\DB" + Regid + ".2.png", data1);
         }


         Byte[] data2 = new Byte[2];
         data2 = (Byte[])(dataSet.Tables[0].Rows[0]["Othersimage"] is DBNull ? null : dataSet.Tables[0].Rows[0]["Othersimage"]);
         if (data2 != null)
         {
             //MemoryStream mem2 = new MemoryStream(data2);
             File.WriteAllBytes(@"C:\test\Images\DB" + Regid + ".3.png", data2);
         }
     }

 }

So you can try now, also request you flush memorystream before using it.
like :
C#
if (data != null)
                {
                    MemoryStream mem = new MemoryStream(data);
                    mem.Flush();
                    PbCitizen.Image = Bitmap.FromStream(mem);
                }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900