Newbie Xamarin

Newbie Xamarin

  • NA
  • 15
  • 3.6k

Xamarin - Android Connect to SQL Server via WIFI

Apr 28 2020 11:56 AM
Hi everybody,
 
I am a newbie to XAMARIN and working in XAMARIN (VS-2019) on a sample android app to connect to an SQL Server 2014 database to perform CRUD actions using direct ADO SqlClient connection, and did all required remote connection configurations on my server such as: Enabling TCP/IP - Setting Exception rules on the Firewall (1433 for TCP and 1434 for UDP - Program Exception for SQL Server, and Program exception for SQL Browser), so all fine.
 
Actually my application seemed to be working on the emulator on debug, but not when published on a real android device, my application just ignores my request and goes back to the Main activity.
 
Could someone help me out please so this is my code:
  1. public void InsertUser()  
  2. {  
  3.   
  4.     try  
  5.     {  
  6. // Connect using Server IP Adress instead of Server Name  
  7.         string dbstring = @"data source=xxx.xx.xx.xx,1433;initial catalog=MyDatabase;user id=XamarinUser;password=myPassword;Connect Timeout=10";  
  8.         SqlConnection Con = new SqlConnection(dbstring);  
  9.         if (Con.State == ConnectionState.Open) { Con.Close(); }  
  10.         Con.Open();  
  11.         SqlCommand cmd = new SqlCommand("INSERT INTO tblUSERS (Username, Password, Email, Active)  VALUES ('" + txtUserName.Text + "','" + password1.Text + "','" + txtEmail.Text + "',0);", Con);  
  12.         cmd.ExecuteNonQuery();  
  13.         Con.Close();  
  14.         Con = null;  
  15.         cmd = null;  
  16.         this.Dismiss();  
  17.     }  
  18.     catch (Exception)  
  19.     {  
  20.   
  21.         throw new Exception("Application run into errors. Please contact us.");  
  22.   
  23.     }  
  24.   
  25. }  
 And thanks for your help.