Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

How to work Xamarin with SQL Server using Web API

$
0
0

I am trying to connect to SQL Server with Xamarin using Web API, but I don't know somehow I am doing something wrong here is my code,

Webapi controller

    public class LoginController : ApiController
    {
        UserEntities db = new UserEntities();  

        [HttpPost]
        [ActionName("XAMARIN_REG")]
        // POST: api/Login  
        public HttpResponseMessage Xamarin_reg(string username, string password)
        {
            Login login = new Login();
            login.username = username;
            login.password = password;
            db.Logins.Add(login);
            db.SaveChanges();
            return Request.CreateResponse(HttpStatusCode.Accepted, "Successfully Created");
        }
        [HttpGet]
        [ActionName("XAMARIN_Login")]
        // GET: api/Login/5  
        public HttpResponseMessage Xamarin_login(string username, string password)
        {
            var user = db.Logins.Where(x => x.username == username && x.password == password).FirstOrDefault();
            if (user == null)
            {
                return Request.CreateResponse(HttpStatusCode.Unauthorized, "Please Enter valid UserName and Password");
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.Accepted, "Success");
            }
        }

    }

xamarin Activity

public class MainActivity : Activity
    {

    EditText txtusername;
    EditText txtPassword;
    Button btncreate;
    protected override void OnCreate(Bundle savedInstanceState)
    {

        base.OnCreate(savedInstanceState);
        // Create your application here  
        SetContentView(Resource.Layout.Main);
        txtusername = FindViewById<EditText>(Resource.Id.txtsaveusername);
        txtPassword = FindViewById<EditText>(Resource.Id.txtsavepassword);
        btncreate = FindViewById<Button>(Resource.Id.btnsavecreate);

        btncreate.Click += Btncreate_Click;
    }
    private async void Btncreate_Click(object sender, EventArgs e)
    {
        Login log = new Login();
        log.username = txtusername.Text;
        log.password = txtPassword.Text;
        HttpClient client = new HttpClient();
        string url = "http://localhost:54445/api/Login/";
        var uri = new Uri(url);
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        HttpResponseMessage response;
        var json = JsonConvert.SerializeObject(log);
        var content = new StringContent(json, Encoding.UTF8, "application/json");
        response = await client.PostAsync(uri, content);
        if (response.StatusCode == System.Net.HttpStatusCode.Accepted)
        {
            var errorMessage1 = response.Content.ReadAsStringAsync().Result.Replace("\\", "").Trim(new char[1]
            {
            '"'
            });
            Toast.MakeText(this, errorMessage1, ToastLength.Long).Show();
        }
        else
        {
            var errorMessage1 = response.Content.ReadAsStringAsync().Result.Replace("\\", "").Trim(new char[1]
            {
            '"'
            });
            Toast.MakeText(this, errorMessage1, ToastLength.Long).Show();
        }
    }
}

i want to work on local sql server not with azure, this code should save username and password to my local sql server, but when i click on button to save its give me this error

System.Net.Http.HttpRequestException: An error occurred while sending the request

I have also tested to run webapi in postman tool i am getting this error

{
    "$id": "1",
    "message": "No HTTP resource was found that matches the request URI 'http://localhost:54445/api/Login'.",
    "messageDetail": "No action was found on the controller 'Login' that matches the request."
}

Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>