0

i am trying to connect my xamarin.forms app to sql server. i know that i have to use a webservice to do that. i am using wcf web service. the problem is that i only got async methods after adding it as a connected service to my app. like the problem in this question: Xamarin.Forms Add Connected Service on WCF only generated async method the thing is i can't use its answer because i don't have the generate synchronous operations option, i found another answer in this: WCFclient operation only Async .Net core 2.0 but i really still don't know how to use the async methods. like how do i get the result of the operation to perform my login when clicking a button. this is my code for the wcf webservice: Iuserlogin.cs:

 [ServiceContract]
    public interface Iuserlogin
    {
        [OperationContract]

        [WebInvoke(BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json, Method = "POST")]
        DataTable login(string username, string pass);
    }

userlogin.svc.cs:

 public class userlogin : Iuserlogin
    {
        

        public DataTable login(string username, string pass)
        {
            DataTable dt = new DataTable();
            dt.TableName = "summary_req";
            SqlConnection con = new SqlConnection("server=DESKTOP-CPOJ94O\\MSSQLSERVER1;database=users;integrated security=true");
            con.Open();
            SqlCommand cmd = new SqlCommand("SELECT username, password FROM hjk where CONVERT(VARCHAR, username)=@username and CONVERT(VARCHAR, password)=@password", con);
            cmd.Parameters.AddWithValue("@username", username);
            cmd.Parameters.AddWithValue("@password", pass);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            return dt;
        }
    }
}

this is my trial to use the async functions in my app: MainPage.xaml.cs:

 public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void SignIn_Clicked(object sender, EventArgs e)
        {
            ServiceReference1.IuserloginClient cs = new ServiceReference1.IuserloginClient();
            string user_name = username.Text;
            string pass = password.Text;
            
            ServiceReference1.loginRequest loginreq= new ServiceReference1.loginRequest(user_name, pass);
            ServiceReference1.loginResponse response = cs.loginAsync(user_name, pass).Result;
        
            
          
        }
    }
}

i don't know what to do next. how do i continue to know if the user can login or not. how do i get a datatable result from the wcf functions. thanks in advance.

rana hd
  • 355
  • 3
  • 18
  • I would strongly suggest using RESTful services as opposed to WCF. They are much, much easier to work with. – Jason Sep 08 '21 at 09:40
  • thanks a lot for your reply sir. would you please suggest a tutorial so i can understand them easily. cause i searched about the restful services and didn't quite understand them so i chose wcf instead. – rana hd Sep 08 '21 at 10:20
  • https://learn.microsoft.com/en-us/xamarin/xamarin-forms/data-cloud/web-services/rest – Jason Sep 08 '21 at 10:25

0 Answers0