Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi guys i am trying to get the signature image from sqlserver like....


C#
private void button1_Click(object sender, EventArgs e)
     {
         textBox5.Text = "";
         textBox4.Text = "";
         textBox3.Text = "";
         textBox2.Text = "";
         DataConnection data = new DataConnection();
         SqlConnection con;
         con = data.GetConnection();
         con.Open();
         SqlCommand cmd = new SqlCommand();
         cmd.CommandText = "select*from tblInbound where consignmentNo=('" + textBox1.Text + "')";
         cmd.Connection = con;
         SqlDataReader dr = cmd.ExecuteReader();
         if (dr.Read())
         {
             textBox2.Text = dr[7].ToString();
             textBox5.Text = dr[12].ToString();
             textBox3.Text = dr[10].ToString();
             textBox4.Text = dr[11].ToString();
             //textBox6.Text = dr[46].ToString();
             Byte[] imagedata = new Byte[0];



             imagedata = (byte[])dr[45];
             System.Drawing.Image b = null;
             MemoryStream mm = new MemoryStream(imagedata);
             mm.Write(imagedata, 0, imagedata.Length);
             //pictureBox1.Image = Image.FromStream(mm);
             b = System.Drawing.Image.FromStream(mm);//giving the error invalid parameter

but its giving the error at b = System.Drawing.Image.FromStream(mm); invalid parameter..can u please tell me whats wrong with this code..
thanks...

C#
    //using (MemoryStream mm = new MemoryStream())
    //{
    //    System.Drawing.Image b;
    //    Bitmap bitmap = null;
    //    mm.Write(imagedata, 0, imagedata.Length);
    //    b = System.Drawing.Image.FromStream(mm);
    //    pictureBox1.Image = b;
    // }
}



[edit]Code block added, "Treat my content as plain text..." option disabled - OriginalGriff[/edit]
Posted
Updated 3-Feb-12 22:06pm
v2

For Pete's sake, don't do things like that!

1) Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
SQL
cmd.CommandText = "select*from tblInbound where consignmentNo=@CN";
cmd.Parameters.AddWithValue("@CN", textBox1.Text);


2) Don't use SELECT * FROM... it wastes bandwidth, and memory, particularly when one of the fields is an image. List only the fields you are going to use.

3) Don't ever use SqlDateReader as an array with a numeric index, unless the list of fields is very, very short, and specifically specified. 45 of them or more is just plain stupid. Use the name of the field instead - it makes your code more readable, and reliable. What happens to your code if someone decides next month that column 2 is not needed and deletes it from your database?

Fix that lot, and then fix your problem - which is that you have added a single line of code you do not need...


...get rid of the line:
C#
mm.Write(imagedata, 0, imagedata.Length);
 
Share this answer
 
Comments
Rajendra Koranga 5-Feb-12 3:13am    
thanks a lot sir, but may i know how can i get these byte into the image format..
OriginalGriff 5-Feb-12 3:22am    
Read the above...
From the first glance, the problem is this: you are trying to write to the memory stream and read it immediately using System.Drawing.Image.FromStream. How could it be possible? You are trying to read from the point of the end of the stream, as the current position of the stream is left there by the last write.

That said, you need to rewind the memory stream to its zero position before reading using the property System.IO.MemoryStream.Position, please see http://msdn.microsoft.com/en-us/library/system.io.memorystream.aspx[^].

Maybe there are other problem, but this is the immediate one.

—SA
 
Share this answer
 
v2

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