Hi Xamarin forum
is this correct syntax for multiple uploading mages to database
XAML
<ScrollView>
<StackLayout>
<StackLayout Orientation="Horizontal">
<Image x:Name="imagePreview" HeightRequest="200" WidthRequest="200" />
<Button Text="Pick image" Clicked="Select_Pic" HeightRequest="5" WidthRequest="100"/>
</StackLayout>
<StackLayout Orientation="Horizontal">
<Image x:Name="imagePreview2" HeightRequest="200" WidthRequest="200" />
<Button Text="Pick image" Clicked="Select_Pic_2" HeightRequest="5" WidthRequest="100"/>
</StackLayout>
<StackLayout Orientation="Horizontal">
<Image x:Name="imagePreview3" HeightRequest="200" WidthRequest="200" />
<Button Text="Pick image" Clicked="Select_Pic_3" HeightRequest="5" WidthRequest="100"/>
</StackLayout>
<StackLayout Orientation="Horizontal">
<Image x:Name="imagePreview4" HeightRequest="200" WidthRequest="200" />
<Button Text="Pick image" Clicked="Select_Pic_4" HeightRequest="5" WidthRequest="100"/>
</StackLayout>
<Controls:CustomEditor Placeholder="Write a Description.." x:Name="postContent" HeightRequest="100"/>
<Button Text="UPLOAD" Clicked="Upload_File"/>
</StackLayout>
</ScrollView>
Codebehind
public partial class PostCreationPage : ContentPage
{
public PostCreationPage()
{
InitializeComponent();
}
private MediaFile _mediaFile;
private MediaFile _mediaFile2;
private MediaFile _mediaFile3;
private MediaFile _mediaFile4;
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 CrossMedia.Current.Initialize();
if (!CrossMedia.Current.IsPickPhotoSupported)
{
await DisplayAlert("Error", "Image file type not support.", "OK");
return;
}
else
{
var mediaOption = new PickMediaOptions()
{
PhotoSize = PhotoSize.Medium
};
_mediaFile = await CrossMedia.Current.PickPhotoAsync();
if (_mediaFile == null) return;
imagePreview.Source = ImageSource.FromStream(() => _mediaFile.GetStream());
var memoryStream = new MemoryStream();
await _mediaFile.GetStream().CopyToAsync(memoryStream);
byte[] imageAsByte = memoryStream.ToArray();
}
}
private async void Select_Pic_2(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 CrossMedia.Current.Initialize();
if (!CrossMedia.Current.IsPickPhotoSupported)
{
await DisplayAlert("Error", "Image file type not support.", "OK");
return;
}
else
{
var mediaOption = new PickMediaOptions()
{
PhotoSize = PhotoSize.Medium
};
_mediaFile2 = await CrossMedia.Current.PickPhotoAsync();
if (_mediaFile2 == null) return;
imagePreview2.Source = ImageSource.FromStream(() => _mediaFile2.GetStream());
var memoryStream = new MemoryStream();
await _mediaFile2.GetStream().CopyToAsync(memoryStream);
byte[] imageAsByte2 = memoryStream.ToArray();
}
}
private async void Select_Pic_3(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 CrossMedia.Current.Initialize();
if (!CrossMedia.Current.IsPickPhotoSupported)
{
await DisplayAlert("Error", "Image file type not support.", "OK");
return;
}
else
{
var mediaOption = new PickMediaOptions()
{
PhotoSize = PhotoSize.Medium
};
_mediaFile3 = await CrossMedia.Current.PickPhotoAsync();
if (_mediaFile3 == null) return;
imagePreview3.Source = ImageSource.FromStream(() => _mediaFile3.GetStream());
var memoryStream = new MemoryStream();
await _mediaFile3.GetStream().CopyToAsync(memoryStream);
byte[] imageAsByte3 = memoryStream.ToArray();
}
}
private async void Select_Pic_4(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 CrossMedia.Current.Initialize();
if (!CrossMedia.Current.IsPickPhotoSupported)
{
await DisplayAlert("Error", "Image file type not support.", "OK");
return;
}
else
{
var mediaOption = new PickMediaOptions()
{
PhotoSize = PhotoSize.Medium
};
_mediaFile4 = await CrossMedia.Current.PickPhotoAsync();
if (_mediaFile4 == null) return;
imagePreview4.Source = ImageSource.FromStream(() => _mediaFile4.GetStream());
var memoryStream = new MemoryStream();
await _mediaFile4.GetStream().CopyToAsync(memoryStream);
byte[] imageAsByte4 = memoryStream.ToArray();
}
}
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(_mediaFile2.GetStream());
}
}
private async void UploadImage(Stream stream)
{
Pipeline databaseConnect = new Pipeline();
string userfirstname = App.FirstName;
var selectedImage = _mediaFile.GetStream();
var selectedImage2 = _mediaFile2.GetStream();
var selectedImage3 = _mediaFile3.GetStream();
var selectedimage4 = _mediaFile4.GetStream();
var page = new LoadingPage();
BinaryReader br = new BinaryReader(stream);
byte[] bytes = br.ReadBytes((Int32)stream.Length);
await stream.ReadAsync(bytes, 0, (int)stream.Length);
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", selectedImage);
sqlInsert.Parameters.AddWithValue("@Photo2", selectedImage2);
sqlInsert.Parameters.AddWithValue("@Photo3", selectedImage3);
sqlInsert.Parameters.AddWithValue("@Photo4", selectedimage4);
sqlInsert.Parameters.AddWithValue("@Uploader", userfirstname);
sqlInsert.Parameters.AddWithValue("@DateUploaded", DateTime.Now);
int i = sqlInsert.ExecuteNonQuery();
databaseConnect.connectDB().Close();
if (_mediaFile != null || _mediaFile2 !=null || _mediaFile3 != null || _mediaFile4 != null)
{
await DisplayAlert("Success", "Uploaded to Database.", "OK");
await PopupNavigation.Instance.PopAsync();
await Navigation.PopAsync();
}
}
catch
{
}
}
}
for me it successfully writes to database using this code but it does not render all the images from the records
and gives me this error System.InvalidCastException: Specified cast is not valid when I tried to call the Photo_2 etc etc