Login android Registration using WebAPI

EnoharaEnohara Member ✭✭✭

I'm creating an application with registration and Login in it, but I'm using SQLite as My database. I try using SQL server as my database and WebApi as the connector. I already fetch the data from my API but I still don't get on how to fetch,put,delete,update the data on my Database. In short, I don't know how to Query on API or SQL server. (Correct me if I'm wrong)

Here's the XML where all of the Data I input on the WPF application GO.

I've already fetch all that and already and put it on my Listview using HttpClient.

But what I want is a log in system with registration where i can put and get data but I don't know how to do it on the SQL Server using WebAPI.
I want to know what are the Queries of API or SQL server on android (Correct me if I'm wrong)

Explain this things on me more detailed.

Correct me with every wrong that i said in here, I'll gladly appreciate it.
And thanks in advance for viewing my post :)

Answers

  • Ahsan_SiddiqueAhsan_Siddique PKMember ✭✭✭✭

    `public class FeedbackController : ApiController
    {
    FeedBackEntities db = new FeedBackEntities();
    [System.Web.Http.HttpPost]
    [System.Web.Http.ActionName("Add_Feedback")]
    //POST: api/Add_Feedback
    public HttpResponseMessage Add_Feedback(User userFeedback)
    {
    try
    {
    User newUser = new User();
    newUser.Email = userFeedback.Email;
    newUser.Subject = userFeedback.Subject;
    newUser.Message = userFeedback.Message;
    db.Users.Add(newUser);
    db.SaveChanges();
    return Request.CreateResponse(HttpStatusCode.Accepted, "Your feedback is saved");
    }
    catch (Exception ex)
    {
    return Request.CreateResponse(HttpStatusCode.ExpectationFailed, ex.ToString());
    }
    }

        [System.Web.Http.HttpPost]
        [System.Web.Http.ActionName("Del_Feedback")]
        //Post : api/Del_Feedback
        public HttpResponseMessage Del_Feedback(int Id)
        {
            try
            {
                db.Users.Remove(db.Users.Where(u => u.ID == Id).FirstOrDefault());
                db.SaveChanges();
                return Request.CreateResponse(HttpStatusCode.Accepted, "Your feedback is removed");
            }
            catch (Exception e)
            {
                return Request.CreateResponse(HttpStatusCode.ExpectationFailed, e.ToString());
            }
        }
    
    
        [System.Web.Http.HttpPost]
        [System.Web.Http.ActionName("Update_Feedback")]
        //Post : api/Update_Feedback
    
        public HttpResponseMessage Update_Feedback(int Id,string Email, string Subject, string Message)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
            User newUser = new User();
            var entry = db.Entry<User>(newUser);
            entry.Entity.ID = Id;
            entry.Entity.Email = Email;
            entry.Entity.Message = Message;
            entry.Entity.Subject = Subject;
            entry.State = System.Data.EntityState.Modified;
            //db.Entry(user).State = EntityState.Modified;
            try
            {
                db.SaveChanges();
            }
            catch(DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }
            return Request.CreateResponse(HttpStatusCode.Accepted, "Your feedback updated");
    
    
            //User newUser = new User();
            //var entry = db.Entry<User>(newUser);
            //entry.Entity.Email = Email;
            //entry.State = System.Data.EntityState.Modified;
    
        }
    
    
        [System.Web.Http.HttpGet]
        [System.Web.Http.ActionName("Get_Feedback")]
        //[Route("get_feedback")]
        //GET: api/Get_Feedback
        public IHttpActionResult Get_Feedback(string Email)
        {
            //takes the first user with the given email
            User user = null;
            try
            {
                user = db.Users.ToList().Where((u) => { return u.Email.ToLower() == Email.ToLower(); }).Last();
    
            }
            catch(Exception e)
            {
                Debug.WriteLine(e.Message);
            }
            if (user == null)
            {
                return Ok("No message found");  
            }
            else
            {
                var json = Newtonsoft.Json.JsonConvert.SerializeObject(user);
                return Json(user);
            }
        }
    
        [System.Web.Http.HttpGet]
        [System.Web.Http.ActionName("Get_Feedback_ID")]
        //GET: api/Get_Feedback_ID
        public IHttpActionResult Get_Feedback_ID(int ID)
        {
            User user = null;
            try
            {
                user = db.Users.ToList().Where((u) => { return u.ID == ID; }).FirstOrDefault();
    
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
            }
            if (user == null)
            {
                return Ok("No record found");
            }
            else
            {
                var json = Newtonsoft.Json.JsonConvert.SerializeObject(user);
                return Json(user);
            }
        }
    }`
    
  • EnoharaEnohara Member ✭✭✭

    @Ahsan_Siddique said:
    `public class FeedbackController : ApiController
    {
    FeedBackEntities db = new FeedBackEntities();
    [System.Web.Http.HttpPost]
    [System.Web.Http.ActionName("Add_Feedback")]
    //POST: api/Add_Feedback
    public HttpResponseMessage Add_Feedback(User userFeedback)
    {
    try
    {
    User newUser = new User();
    newUser.Email = userFeedback.Email;
    newUser.Subject = userFeedback.Subject;
    newUser.Message = userFeedback.Message;
    db.Users.Add(newUser);
    db.SaveChanges();
    return Request.CreateResponse(HttpStatusCode.Accepted, "Your feedback is saved");
    }
    catch (Exception ex)
    {
    return Request.CreateResponse(HttpStatusCode.ExpectationFailed, ex.ToString());
    }
    }

        [System.Web.Http.HttpPost]
        [System.Web.Http.ActionName("Del_Feedback")]
        //Post : api/Del_Feedback
        public HttpResponseMessage Del_Feedback(int Id)
        {
            try
            {
                db.Users.Remove(db.Users.Where(u => u.ID == Id).FirstOrDefault());
                db.SaveChanges();
                return Request.CreateResponse(HttpStatusCode.Accepted, "Your feedback is removed");
            }
            catch (Exception e)
            {
                return Request.CreateResponse(HttpStatusCode.ExpectationFailed, e.ToString());
            }
        }
    
    
        [System.Web.Http.HttpPost]
        [System.Web.Http.ActionName("Update_Feedback")]
        //Post : api/Update_Feedback
    
        public HttpResponseMessage Update_Feedback(int Id,string Email, string Subject, string Message)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
            User newUser = new User();
            var entry = db.Entry<User>(newUser);
            entry.Entity.ID = Id;
            entry.Entity.Email = Email;
            entry.Entity.Message = Message;
            entry.Entity.Subject = Subject;
            entry.State = System.Data.EntityState.Modified;
            //db.Entry(user).State = EntityState.Modified;
            try
            {
                db.SaveChanges();
            }
            catch(DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }
            return Request.CreateResponse(HttpStatusCode.Accepted, "Your feedback updated");
    
    
            //User newUser = new User();
            //var entry = db.Entry<User>(newUser);
            //entry.Entity.Email = Email;
            //entry.State = System.Data.EntityState.Modified;
    
        }
    
    
        [System.Web.Http.HttpGet]
        [System.Web.Http.ActionName("Get_Feedback")]
        //[Route("get_feedback")]
        //GET: api/Get_Feedback
        public IHttpActionResult Get_Feedback(string Email)
        {
            //takes the first user with the given email
            User user = null;
            try
            {
                user = db.Users.ToList().Where((u) => { return u.Email.ToLower() == Email.ToLower(); }).Last();
                
            }
            catch(Exception e)
            {
                Debug.WriteLine(e.Message);
            }
            if (user == null)
            {
                return Ok("No message found");  
            }
            else
            {
                var json = Newtonsoft.Json.JsonConvert.SerializeObject(user);
                return Json(user);
            }
        }
    
        [System.Web.Http.HttpGet]
        [System.Web.Http.ActionName("Get_Feedback_ID")]
        //GET: api/Get_Feedback_ID
        public IHttpActionResult Get_Feedback_ID(int ID)
        {
            User user = null;
            try
            {
                user = db.Users.ToList().Where((u) => { return u.ID == ID; }).FirstOrDefault();
    
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
            }
            if (user == null)
            {
                return Ok("No record found");
            }
            else
            {
                var json = Newtonsoft.Json.JsonConvert.SerializeObject(user);
                return Json(user);
            }
        }
    }`
    

    Sir, can you explain it to me more? i can't understand these set of code because I'm still newbie to Xamarin, Thanks again :)

  • Ahsan_SiddiqueAhsan_Siddique PKMember ✭✭✭✭

    Please tel me your requirement??

  • EnoharaEnohara Member ✭✭✭

    @Ahsan_Siddique said:
    Please tel me your requirement??

    What requirement?

  • Ahsan_SiddiqueAhsan_Siddique PKMember ✭✭✭✭

    I think you should wait for my new video on How to create Web API with Azure SQL Server and consume in Xamarin. I am creating this video in next few days.

  • Ahsan_SiddiqueAhsan_Siddique PKMember ✭✭✭✭

    If you want to perform curd operation using Sq-lite Database then you can visit my article.

    c-sharpcorner.com/article/xamarin-android-sqlite-database/

    If you want to use Azure SQL server then you can visit this web API article.

    c-sharpcorner.com/article/xamarin-android-create-login-with-web-api-using-azure-sql-server-part-one/

    These article maybe help you to understanding of basic curd operations in SQ lite and Azure SQL.

  • EnoharaEnohara Member ✭✭✭

    My Web API is not from Azure SQL server sir, i create a back end in WPF, its a self hosted API. And for the connection, i connect my laptop and mobile in the same network and make my connection static.

Sign In or Register to comment.