Mohammed Adamu

Mohammed Adamu

  • NA
  • 107
  • 46.3k

connecting xamarin android app to local sql server

Apr 13 2017 1:25 PM

Please can any one help me create a workable connection for my application in visual studio
i have this as my connection but it not working for me
public SqlConnection CN = new SqlConnection("Server=DESKTOP-KTFD46J;Database=MailClient;Trusted_Connection=True;");
please this is my C# file and xml file as we
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Data;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Telecom;
using Java.Net;

namespace login
{
[Activity(Label = "Login", MainLauncher = true)]
public class login : Activity
{
public Button strlogin;
private TextView phoneNumberTextView;

 public SqlConnection CN = new SqlConnection("Server=DESKTOP-KTFD46J;Database=MailClient;Trusted_Connection=True;"); protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); // Create your application here SetContentView(Resource.Layout.lonigform);          strlogin = FindViewById<Button>(Resource.Id.btnlogin);          phoneNumberTextView = FindViewById<TextView>(Resource.Id.txtusername); HandleEvents(); } private void HandleEvents() {         strlogin.Click += Strlogin_Click; } string i = "Successfull"; string n = "Failed"; private void Strlogin_Click(object sender, EventArgs e) { //Toast.MakeText(Application.Context, "hello world", ToastLength.Short); //phoneNumberTextView.Text= i; //CN.Open(); //if(CN.State==System.Data.ConnectionState.Open) //{ //    phoneNumberTextView.Text = i; //} //else //{ //    phoneNumberTextView.Text = n; //} //CN.Close(); using (SqlConnection connection = new SqlConnection("Server=DESKTOP-KTFD46J;Database=MailClient;Integrated Security=True;")) {             connection.Open(); if (connection.State == System.Data.ConnectionState.Open) {                 phoneNumberTextView.Text = i; } else {                 phoneNumberTextView.Text = n; } } } //protected void btnlogin_Click(object sender, EventArgs e) //{ //    //using (SqlConnection connection = new SqlConnection(connectionString)) //    //{ //    //    connection.Open(); //    //    // Do work here; connection closed on following line. //    //} //    Toast.MakeText(Application.Context,"hello world",ToastLength.Short); //    // Resource.Id.txtusername.ToString() = "Welcome"; //}  }

}

<?xml version="1.0" encoding="utf-8"?>





this is the error am getting when i run the app

Unhandled Exception:
System.Data.SqlClient.SqlException: Server does not exist or connection refused.
Thank you


Attachment: sent.rar

Answers (4)