Hi Guys,
I have an xml file contains the imagedata attribute. now while reading xml file using dataset i was setting this field to string and insert it to another table that have also image field.
when i am tring to retrive this image it's not a actual image.

<ImageData>/9j/4AAQSkZJRgABAQAAAQABAAD//gA+Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcgSlBFRyB2NjIpLCBkZWZhdWx0IHF1YWxpdHkK</ImageData>

Thanks in advance.

Dani AI

Generated

Short diagnosis: the XML contains a base64 JPEG (the "/9j/4AAQ..." text). The common problem here is twofold: the DataSet was read without its schema so the image column is treated as text, and the code then does ToString() and embeds the long base64 into a SQL string — which often causes truncation/corruption and is unsafe. is also correct to suspect the base64 in the XML may be incomplete.

What to do (practical fixes you can apply immediately):

  • Prefer preserving the binary type when exporting/importing XML. Write the schema with the data: call WriteXml with XmlWriteMode.WriteSchema so the image column is recreated as a byte[] when you ReadXml. That keeps the column as binary instead of a string.
  • If you only have base64 text in the XML, decode it before inserting into SQL and use a parameterized query to write binary data (do not concatenate the base64 into SQL). Example pattern:
byte[] bytes = Convert.FromBase64String(base64String);
using (var cmd = new SqlCommand("UPDATE paper_setting SET imagedata = @img", conn))
{
    cmd.Parameters.Add("@img", SqlDbType.VarBinary, bytes.Length).Value = bytes;
    conn.Open();
    cmd.ExecuteNonQuery();
}

Troubleshooting tips:

  • Make sure you’re reading the correct column (ds.Tables[0].Rows[i][0] may not be the image column). Use the column name and inspect DataTable.Columns to check type.
  • If Convert.FromBase64String throws an error, the base64 is corrupted/truncated. Save the base64 to a file, decode to bytes and try opening it as .jpg to verify completeness.
  • Use varbinary(max) instead of the deprecated SQL Server image type where possible, and always use parameters to avoid truncation and SQL injection.

If the XML snippet decodes only to the "CREATOR: gd-jpeg..." text with nothing after, the source data was truncated before export — re-export with schema and verify the full base64 is present.

Recommended Answers

All 3 Replies

Do you mean after you decode the base-64 text it's still not an image? It looks like the start of a JPEG file, and you get "CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), default quality" in the decoded text, but there's nothing after that.

Hi gusano79,
let me explain in detail whats happen

I have a table name Table1 contains img field which is Image datatype. I have store a image in Table1 Now using dataset i have created xml file.

Now i am reading this file with dataset.Readxml() method and inserted image as a string (i think here i am going wrong) in Table2.

Here is my code snippets

Creating XML File

DataSet ds = new DataSet();
            string strxml = "select * from testing123";
            da = new SqlDataAdapter(strxml, connect.getConnection());
            da.Fill(ds, "testing123");
            ds.WriteXml(Application.StartupPath + "\\ImageFile.xml");
            MessageBox.Show("Done");

Now read this file and insert to table2

string ImageTxt = "";
            DataSet ds = new DataSet();
            ds.ReadXml(Application.StartupPath + "\\ImageFile.xml");
            for (int i = 0; i <= ds.Tables[0].Rows.Count-1; i++)
            {
                ImageTxt = ds.Tables[0].Rows[i][0].ToString();
            }

            string strUpdate = "update paper_setting set imagedata='"+ ImageTxt +"'";
            cmd = new SqlCommand(strUpdate, connect.getConnection());
            cmd.Connection.Open();
            cmd.ExecuteNonQuery();
            cmd.Connection.Close();
            MessageBox.Show("Done");

but when i retrieve image from table2. It's not in proper format.

Any help would be appreciated.

How are you storing the image in the testing123 table?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.