0

I want to save images into my mysql database.

This is my code:

The class:

    public async void SIE()
    {
        TrialClass trialClass = new TrialClass(ImagesPaths1);
        BlogRestClient<TrialClass> restClient = new BlogRestClient<TrialClass>();
        await restClient.PostAsync(trialClass);
    }

    public class TrialClass
    {
        public List<ImageFormatClass> ImagesPath2;

        public TrialClass(List<ImageFormatClass> imagespath)
        {
            ImagesPath2 = imagespath;
        }
    }

    public class ImageFormatClass
    {
        public Image SavedImage;
        public int Format;

        public ImageFormatClass()
        {
        }
    }

    private void Post_Clicked(object sender, EventArgs e)
    {
            SIE();
    }

Web API controller:

public void Post([FromBody] TrialClass value)
{
        foreach (ImageFormatClass s in value.ImagesPath2)
        {
            string sqlstring = "server=; port= ; user id =;Password=;Database=;";
            MySqlConnection conn = new MySqlConnection(sqlstring);

            try
            {
                conn.Open();
            }
            catch (MySqlException ex)
            {
                throw ex;
            }

            string Query = "INSERT INTO test.blogimagestable (ImagesId)values( ?str);";

            MySqlCommand cmd = new MySqlCommand(Query, conn);
            cmd.Parameters.Add("?str", MySqlDbType.VarString, 256).Value = s.SavedImage;

            cmd.ExecuteReader();

            conn.Close();
        }
}

Anytime I press the post button to fire the Post_Clicked event, I get this error

Newtonsoft.Json.JsonSerializationException: 'Self referencing loop detected for property 'ManifestModule' with type 'System.Reflection.RuntimeModule'. Path 'ImagesPath2[0].SavedImage.Source.Stream.Method.Module.Assembly'.'

at `await restClient.PostAsync(trialClass);

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
LBG
  • 51
  • 2
  • 8
  • https://stackoverflow.com/questions/13510204/json-net-self-referencing-loop-detected – SushiHangover Jul 15 '19 at 16:24
  • What is Image? Xamarin.Forms.Image is a UI element, not bitmap data that you can transfer over the wire. – Jason Jul 15 '19 at 16:43
  • @Jason So what should I do? – LBG Jul 15 '19 at 16:45
  • you need to use the underlying image data was was used to create the Image's ImageSource – Jason Jul 15 '19 at 16:46
  • @Jason Can you give me an example, I tried what you said but it wasn't working. I don't want to save the ImageSource but the image itself, so should I convert the image to bitmap? – LBG Jul 15 '19 at 21:34
  • @Jason I feel there is something small I'm missing here. – LBG Jul 15 '19 at 21:35
  • 1
    Let's assume you're getting the image from a file. Then all you need to do is read that file as a byte[] and send it to your service. – Jason Jul 15 '19 at 21:48

0 Answers0