Home > OS >  Xamarin with Microsoft SQL Server database
Xamarin with Microsoft SQL Server database

Time:12-29

I am trying to code an Android and iOS application for my thesis for school. It is the first time I am actually working with database and I don't have a clue what I am doing.

My teacher won't (because he doesn't have a clue either) help, so I am coming here. I am running Android Studio on my PC, along with the Microsoft SQL Server database. From what I read online, I would need to develop a REST API or something like that but as I said I don't have a clue what I am doing and I just want to have my application running on my PC for testing.

I tried this and a couple of other things but the main problem I am phasing is that I am getting a connection error. The IP of my PC is 192.168.1.41 I am running Android studio with a pixel phone running android 13. The name of the database is personeelgegevens. The name in SQL Server of my pc is HP_PROBOOK_G8\MSSQLSERVER02.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace ShowMeYourTools
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class LoginUI : ContentPage
    {
        public LoginUI()
        {
            InitializeComponent();
        }

        //Inlog knop
        private void Button_Clicked(object sender, EventArgs e)
        {
            if (txbEmail.Text == "ADMIN" && txbWachtwoord.Text == "ADMIN")
            {
                Navigation.PushModalAsync(new ShowMeYourTools.HomePage());
            }
            else
            {
                DisplayAlert("Foutive ingave", "De ingave komt niet overeen met bij ons gekende accounts.", "OK");
            }
        }

        //Wordt niet gebruikt
        private void Button_Clicked_1(object sender, EventArgs e)
        {
            string constring = @"Server=192.168.1.41;Database=personeelgegevens;Trusted_Connection=true";

            using (SqlConnection con = new SqlConnection(constring))
            {
                try
                {
                    con.Open();
                    DisplayAlert("OK", "OK I am Connected", "OK");
                }
                catch (Exception ex)
                {
                    DisplayAlert("Error", ex.ToString(), "OK");
                }
            }
        }

        //Wachtwoord vergeten lijntje text
        private void TapGestureRecognizer_Tapped(object sender, EventArgs e)
        {
            Navigation.PushModalAsync(new ShowMeYourTools.PasswordReset());
        }
    }
}

Hope that anyone can help

The line of code when it crashes is when it is trying to connect. con.Open();

As asked the Error thrown Image of Error this is only a part of it

Update I fixt it by not using "proximus" my internet provider but "telenet"

CodePudding user response:

This is clearly not a complete answer, but just try to get you started.

Three tier system

What you found from the internet is correct, that for this system you will need 3 tiers,

  • An Android app running on the phone or emulator
  • A SQL Server instance that stores the data
  • A REST API web app hosting somewhere in between.

REST API with ASP.NET Core 7

Microsoft does have a guide on how to write REST API with .NET 7, so that you can follow it,

https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-7.0&tabs=visual-studio

Let ASP.NET Core Connect with SQL Server

Then you should prepare the SQL Server instance following Microsoft documentation or books, and let your web app connects to it via ADO.NET,

https://learn.microsoft.com/en-us/sql/connect/ado-net/microsoft-ado-net-sql-server?view=sql-server-ver16

Note that you can also use Entity Framework if you like, but ADO.NET is more than enough for simple projects.

Let Android App Consume REST API

This is the simplest part, as you can use HttpClient and tons of examples are out there.

However, instead of using legacy Xamarin.Android you should use .NET MAUI from .NET 7 now to build your Android app,

https://devblogs.microsoft.com/dotnet/dotnet-maui-dotnet-7/

BTW, if you don't know networking of emulator very well, read this,

https://halfblood.pro/how-to-let-android-emulator-access-iis-express-f6530a02b1d3

  • Related