0

This is my insert code: I selected an image from xamarin app and try to insert in MySql database after conversion. Kidnly check my code is my conversion is ok? and my code is not inserting in database, where am i wrong? Thanks advance for help.

     private async void Button_Clicked(object sender, EventArgs e)
        {

            await CrossMedia.Current.Initialize();
            if(!CrossMedia.Current.IsPickPhotoSupported)
            {
                await DisplayAlert("info", "Picture not supported", "OK");
            }
            _mediaFile = await CrossMedia.Current.PickPhotoAsync(new PickMediaOptions { });

            if (_mediaFile == null)
                return;
            dir_local = _mediaFile.Path;
            file_name = Path.GetFileName(dir_local);
            _ = DisplayAlert("File Info", file_name, "OK");

            
            Stream fs = _mediaFile.GetStream();
            BinaryReader br = new BinaryReader(fs);
            byte[] bytes = br.ReadBytes((Int32)fs.Length);


            //MYSQL Connection
            MySqlConnection zeeconn = new MySqlConnection(Properties.Resources.db_con);
                       
            MySqlCommand cmd = new MySqlCommand("insert into tblTest (user,pass) values (@user,@pass)", zeeconn);
            

            var user = new MySqlParameter("@user", MySqlDbType.LongBlob, bytes.Length);
            var pass = new MySqlParameter("@pass", MySqlDbType.VarChar, 50);
            
            user.Value = bytes;
            pass.Value = "zero";

            cmd.Parameters.Add(user);
            cmd.Parameters.Add(pass);

            cmd.ExecuteNonQueryAsync();
            zeeconn.Close();
  • 1
    Add `await` in `cmd.ExecuteNonQueryAsync();`. If you have no exception beforehand, this is the issue. – Ergis Nov 16 '21 at 13:34
  • Thanks you so much brother. It worked for me :) – Zikria Rasul Nov 16 '21 at 14:53
  • Before going forward I would recommend reading [Storing images in database](https://stackoverflow.com/questions/3748/storing-images-in-db-yea-or-nay) – JonasH Nov 16 '21 at 15:46

0 Answers0