Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 89864

How to insert multiple selected images to database

$
0
0

Hi xamarin forum

how can I insert / upload my images to database im currently using the plugin of xamboy's multiple images here is my code

XAML

<StackLayout>
            <Button Text="Pick image" Clicked="Select_Pic"/>
            <!--<Image x:Name="imageView" Aspect="AspectFit" />-->
            <flv:FlowListView FlowColumnCount="3" x:Name="listItems" 
                        SeparatorVisibility="None"
                        HasUnevenRows="false" RowHeight="100" >
                <flv:FlowListView.FlowColumnTemplate>
                    <DataTemplate >
                        <ffimageloading:CachedImage  DownsampleToViewSize="true" AbsoluteLayout.LayoutFlags="All" HeightRequest="100" AbsoluteLayout.LayoutBounds="0,0,1,1" Source="{Binding .}"  Aspect="AspectFill" HorizontalOptions="FillAndExpand">
                        </ffimageloading:CachedImage>
                    </DataTemplate>
                </flv:FlowListView.FlowColumnTemplate>
            </flv:FlowListView>
            <Controls:CustomEditor Placeholder="Write a Description.." x:Name="postContent" HeightRequest="100"/>
            <Button Text="UPLOAD" Clicked="Upload_File"/>
        </StackLayout>

Codebehind

public partial class PostCreationPage : ContentPage
{
    public PostCreationPage()
    {
        InitializeComponent();

    }
    private MediaFile _mediaFile;
    private string URL { get; set; }
    ImageSource ImageSrc;
    List<string> _images = new List<string>();

    private async void Select_Pic(object sender, EventArgs e)
    {
        string[] fileTypes = null;

        if (Device.RuntimePlatform == Device.Android)
        {
            fileTypes = new string[] { "image/jpeg" };
        }

        if (Device.RuntimePlatform == Device.iOS)
        {
            fileTypes = new string[] { "public.image" };
        }
        await DependencyService.Get<IMediaService>().OpenGallery();
    }

  private async void Upload_File(object sender, EventArgs e)
    {
        if (_images == null)
        {
            await DisplayAlert("Error", "There was an error occured when trying to get your image.", "OK");
            return;
        }
        else
        {
            UploadImage(_mediaFile.GetStream());
        }
    }

    private async void UploadImage(Stream stream)
    {

        string userfirstname = App.FirstName;
        Pipeline databaseConnect = new Pipeline();

        var page = new LoadingPage();

        try
        {

            await PopupNavigation.Instance.PushAsync(page);



            SqlCommand sqlInsert = new SqlCommand("INSERT INTO t_Post(Post_Content, Photo, Photo_2, Photo_3, Photo_4, Uploader, Date_Uploaded) " +
                "VALUES(@PostContent, @Photo, @Photo2, @Photo3, @Photo4, @Uploader, @DateUploaded)", databaseConnect.connectDB());
            sqlInsert.Parameters.AddWithValue("@PostContent", postContent.Text);
            sqlInsert.Parameters.AddWithValue("@Photo", SqlDbType.Image);
            sqlInsert.Parameters.AddWithValue("@Photo2", SqlDbType.Image);
            sqlInsert.Parameters.AddWithValue("@Photo3", SqlDbType.Image);
            sqlInsert.Parameters.AddWithValue("@Photo4", SqlDbType.Image);
            sqlInsert.Parameters.AddWithValue("@Uploader", userfirstname);
            sqlInsert.Parameters.AddWithValue("@DateUploaded", DateTime.Now);
            int i = sqlInsert.ExecuteNonQuery();
            databaseConnect.connectDB().Close();

            if (_images == null)
            {
                await DisplayAlert("Error", "There was an error when trying to get your image.", "OK");
                return;
            }
            else
            {
                await DisplayAlert("Success", "Uploaded to Database.", "OK");
                await PopupNavigation.Instance.PopAsync();
                await Navigation.PopAsync();
            }
        }
        catch (Exception ex)
        {

        }
    }

    protected override void OnAppearing()
    {
        base.OnAppearing();

        MessagingCenter.Subscribe<App, List<string>>((App)Xamarin.Forms.Application.Current, "ImagesSelected", (s, images) =>
        {
            listItems.FlowItemsSource = images;
            _images = images;

        });
    }

}


Viewing all articles
Browse latest Browse all 89864

Trending Articles