SQLite in Xamarin Forms Android - c#

When I try to create a SQLite Database in the Xamarin Android Project, I get the following Exception.
System.Exception: Something went wrong in the build configuration.
This is the bait assembly, which is for referencing by portable
libraries, and should never end up part of the app. Reference the
appropriate platform assembly instead.
One thing I found already out is that I need SQLite.Net.SQLitePlatform for Android. I dont have the SQLite.Net Namespace.
I used the SQLite.net PCL NuGet package
https://www.nuget.org/packages/sqlite-net-pcl/
I am creating the DB Path in the MainActivity.cs of the Droid Project
var sqliteFilename = "timesheet.db3";
string libraryPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
var path = Path.Combine(libraryPath, sqliteFilename);
In the Forms app I am trying to create the Database
database = new SQLiteConnection(databasePath);
database.CreateTable<TimeRecordEntity>();

Related

Running code based on application platform

Trying to make a cross-platform application library that will be able to work on UWP, Winforms, Wpf, Android and iOS.
Using NetStandard2.0
I've been using Xamarin Forms to test the library because of the cross-platform option but something I want to do is to get hardware information and emulate the devices such as Audio output.
I can't find a way that works across all platforms/devices only ones that target specific. And I can't find a way to run specific code or use a specific library based on the platform that is running.
Targeting multiple frameworks don't work because they cause errors in all the other files.
Tried creating separate libraries that are specifically targeting a platform like UWP, Android and iOS separately but when referencing them, I get a not supported errors:
Project ****.UWP is not compatible with netstandard2.0 (.NETStandard,Version=v2.0).
Project ****.UWP supports: uap10.0.17763 (UAP,Version=v10.0.17763)
stuff like that and for each project (UWP, Android and iOS)
What can I do?
Targeting multiple frameworks don't work because they cause errors in all the other files. Tried creating separate libraries that are specifically targeting a platform like UWP,
The platform specific code can't be used into share library directly. for calling native api for each platforms. the better way is using DependencyService to approach. And here is official document that you could refer to.
And the other way is using Conditional Compilation
For example
public static string DatabaseFilePath
{
get
{
var filename = "TodoDatabase.db3";
#if SILVERLIGHT
// Windows Phone 8
var path = filename;
#else
#if __ANDROID__
string libraryPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
#else
#if __IOS__
// we need to put in /Library/ on iOS5.1 to meet Apple's iCloud terms
// (they don't want non-user-generated data in Documents)
string documentsPath = Environment.GetFolderPath (Environment.SpecialFolder.Personal); // Documents folder
string libraryPath = Path.Combine (documentsPath, "..", "Library");
#else
// UWP
string libraryPath = Windows.Storage.ApplicationData.Current.LocalFolder.Path;
#endif
#endif
var path = Path.Combine(libraryPath, filename);
#endif
return path;
}
}

Getting an embedded resource from a shared project

I'm working on a Xamarin Forms app which has two projects, an Android App and an iOS app. All the other code is stored in shared projects.
Solution looks like this:
MyApp.Android (Android project), references MyApp.Base
MyApp.iOS (iOS project), references MyApp.Base
MyApp.Base (Shared project)
I am using the following code to read an SVG image from the shared project:
using (var stream = GetType().Assembly.GetManifestResourceStream("MyApp.Base.image.svg"))
{
// do work here...
}
This works perfectly when the image is in the Android or iOS project, but I want the image to be shared so I put it in the shared project.
GetType().Assembly return MyApp.Android, thus it cannot find the image. I suppose I'm overlooking something but I haven't been able to find a solution.
Can anyone point me in the right direction? Thanks!
You are using the Shared Project NOT the PCL or .Net Standard library. So, all of your Shared Project contents will get merged into the Platform specific project when you Compile them. That means - even if you Embed your resource in your MyApp.Base that will get merged into .iOS/.Droid project.
I suggest you to learn more about Shared Project vs PCL or .Net Standard library.
Below code; I did't tested but this is the direction that you should follow:
#if __IOS__
var resourcePrefix = "MyApp.iOS.";
#endif
#if __ANDROID__
var resourcePrefix = "MyApp.Android.";
#endif
var assembly = IntrospectionExtensions.GetTypeInfo(typeof(SharedPage)).Assembly;
Stream stream = assembly.GetManifestResourceStream
(resourcePrefix + "image.svg");
For more information please look into this page : https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/files?tabs=macos#embedding-in-shared-projects

Error while using ExecuteNonQuery c#

I have a problem while using ExecuteNonQuery.
This is my code :
var connString = #"Data Source=serwer01;Initial Catalog=PolsatCyfrowy;Integrated Security=True";
FileInfo file = new FileInfo("C:\\Users\\azbudniewek\\source\\repos\\UM2 V2\\UM2 V2\\scripts.sql");
string script = file.OpenText().ReadToEnd();
SqlConnection conn = new SqlConnection(connString);
Server server = new Server(new ServerConnection(conn));
server.ConnectionContext.ExecuteNonQuery(script);
And this is the error:
System.IO.FileNotFoundException: The file or set 'Microsoft.SqlServer.BatchParser, version = 14.100.0.0, Culture = neutral, PublicKeyToken = 89845dcd8080cc91' or one of its company can not be loaded. The file can not be downloaded.
File name: 'Microsoft.SqlServer.BatchParser, version = 14.100.0.0, Culture = neutral, PublicKeyToken = 89845dcd8080cc91'
in System.Reflection.RuntimeAssembly.GetType (RuntimeAssembly set, string name, Boolean throwOnError, Boolean ignoreCase, ObjectHandleOnStack)
in System.Reflection.RuntimeAssembly.GetType (string name, Boolean throwOnError, Boolean ignoreCase)
I read a lot of solutions with problem but can't do anything .
Should I just install another SQL Server? I have installed SQL Server 2014 on my computer and folder with assembly looks like this:
Screenshot
What should I do to avoid this problem? I think I should have version 14.100.0.0 in my assembly folder but don't know how to update it. Maybe anyone know it?
I had a similar issue. I have an SMO application which uses the Microsoft.SqlServer.SqlManagementObjects nuget package.
The app all runs perfectly on my p.c however when it came to deploying it and running it on our app server i was getting the error.
Could not load file or assembly 'Microsoft.SqlServer.BatchParser.dll' or one of its dependencies. The specified module could not be found.
To fix the issue I needed to build the project again targeting x86 platform.
You should add the missing dll in your project and if you publish your project, you must put this dll in to the bin folder.
Install SMO using NuGet, then install VC++ 2013 Redistributables
SMO is now deployed via NuGet. The NuGet page is here.
Noted under "System Requirements" -- "Some native binaries installed with the NetFx SMO libraries also require the VC 2013 runtime to be installed; that runtime is not included in the package."
Thus, after installing SMO via NuGet, manually install the VC++ Redistributables for the architecture of your code (x86 / x64); this is the dependency that Microsoft.SqlServer.BatchParserClient.dll relies upon.
I had faced a similar issue. In my setup, I have a WPF application which uses a class library which in turn refers the Microsoft.SqlServer.SqlManagement Objects (SMO) (version 140.17283.0) via NuGet package.
After trying lot of things, finally figured out that in addition to the class library, the WPF application also needs to refer the SMO library via NuGet.
So I just added the SMO library via NuGet at the solution level and install it for class library as well as WPF application. Voila, it worked !!! (Looks like the Microsoft.SqlServer.BatchParserClient.dll is not properly referenced if it is referred only in the class library which is weird but it solved the issue)

How to access to the ServiceManager with Xamarin Android

I'm developing an Android app using Xamarin.Android. I have to access to the ServiceManager in order to retrieve a specific IBinder. The ServiceManager is accessible and visible by Java applications, but I cannot see it on my Visual Studio project with the referenced Mono.Android dll.
Relative Java code:
IBinder b = ServiceManager.getService("<service-name>");
How can I get the same behaviour on C# with Xamarin.Android library?
try in your activity this: var a = Application.GetSystemService ("name-service");
Good luck

Xamarin.Forms using SQLite.Net.Async (the NUGET part)

I'm trying to make a connection to a SQLite database using Visual Studio 2015 the target platform is Android.
I've found this SO question but I can not get it to work. My problem is the part of installing packagde.
I want to have my database stuff placed in a Class Library so I've created a new Application, and added a Class Library to the solution.
To install SQLite I've written the following in the packagde manager console:
Install-Package SQLite.Net.Platform.XamarinAndroid -Version 2.3.0
But installs SQLLite into my application, not my Class Library.
My Class Library is very very simple:
public class SQLiteDataService
{
private SQLiteAsyncConnection connectionFlexCheckDemo;
private ISQLitePlatform sqlitePlatform;
private string FlexCheckDBNameDemo = "FlexCheckDemo.db";
private void Test()
{
var platform = new SQLitePlatformAndroid();
}
}
but it doesn't compile:
Error CS0234 The type or namespace name 'Net' does not exist in the
namespace 'SQLite' (are you missing an assembly reference?) ...
What am I doing wrong? And more important how to make it work.
You will have to install the Nuget or the dll to reference the SQLite-Async in your Shared or PCL project so that you can write your common code there like having common CRUD operations or having Unit Of Work coupled with Repository pattern. A simple example can be found here.
Then in your platform specific projects you need to have the nuget installed as well, which will get resolved and install the corresponding version based on your platform. I don't think there is a nuget available for WP, so you might have to look for other choices.
This is the nuget package for SQLite.Net.Async PCL

Categories

Resources