如何使用Web服务更新数据

时间:2019-04-30 13:16:21

标签: sql-server web-services xamarin.android sql-update

我正在尝试更新Xamarin上的数据。 Android使用Web服务。当我在浏览器上尝试Web服务时,数据在SQL Server中更新,并显示一条消息帐户已成功更新,但是当我将Web服务绑定到xamarin.android中时。消息显示**该帐户不存在**您能帮我修复我的代码吗!

我也尝试过使用Web API,但是数据未更新,而是显示错误内部服务器错误

网络服务

        [WebMethod(MessageName = "PUT")]
        [System.Xml.Serialization.XmlInclude(typeof(ReturnData))]
        public ReturnData PUT(string FullName, string Gender, string ICNo, string Address,
            string PhoneNo, string Email, string Username, string Password, string PayMethod)
        {
            int IsAdded = 1;
            string Message = "";

            // check if this account is already created
            Users myUsers = new Users();
            if (myUsers.IsAvailable(Username, Email) == 0)
            {

                IsAdded = 0;
                Message = "Account is not exist";
            }
            else
            {
                //saving into db
                SqlConnection conn = new SqlConnection(new DBConnection().ConnectionString);

                try
                {
                    SqlCommand cmd = new SqlCommand("UPDATE pmsVehicleRecord SET clFullName=@fullName, clGender=@gen, clICNo=@ic, clAddress=@address, clPhoneNo=@phoneNo, clEmail=@email, clPwd=@pwd, clPaymentMethod=@payMethod WHERE clUsername=@username");

                    cmd.Parameters.AddWithValue("fullName", FullName);
                    cmd.Parameters.AddWithValue("gen", Gender);
                    cmd.Parameters.AddWithValue("ic", ICNo);
                    cmd.Parameters.AddWithValue("address", Address);
                    cmd.Parameters.AddWithValue("phoneNo", PhoneNo);
                    cmd.Parameters.AddWithValue("email", Email);
                    cmd.Parameters.AddWithValue("username", Username);
                    cmd.Parameters.AddWithValue("pwd", Password);
                    cmd.Parameters.AddWithValue("payMethod", PayMethod);
                    cmd.Connection = conn;
                    if (conn.State == System.Data.ConnectionState.Closed)
                        conn.Open();
                    SqlDataReader ds = cmd.ExecuteReader();

                    Message = "Account is successfully updated";
                }
                catch (Exception ex)
                {
                    IsAdded = 0;
                    Message = ex.Message;
                }
            }
            ReturnData rt = new ReturnData();
            rt.Message = Message;
            rt.lstIndex = IsAdded;

            return rt;
        }
    public class Users
    {
        public int IsAvailable(string UserName, string Email)
        {
            //Result result = new WebAppTest.Result();
            int lstIndex = 0;
            SqlConnection conn = new SqlConnection(new DBConnection().ConnectionString);

            try
            {
                SqlDataReader reader;
                {
                    SqlCommand cmd = new SqlCommand("SELECT lstIndex FROM pmsVehicleRecord WHERE clUsername=@username AND clEmail=@email");

                    cmd.Parameters.AddWithValue("username", UserName);
                    cmd.Parameters.AddWithValue("email", Email);
                    cmd.Connection = conn;
                    if (conn.State == System.Data.ConnectionState.Closed)
                        conn.Open();
                    reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        lstIndex = reader.GetInt32(0);
                    }
                }
            }
            catch (Exception ex)
            {

            }
            return lstIndex;
        }
    }
        private void BtnSave_Click(object sender, EventArgs e)
        {
            EditText username = FindViewById<EditText>(Resource.Id.txtUsername);
            EditText fullName = FindViewById<EditText>(Resource.Id.txtFullName);
            EditText gender = FindViewById<EditText>(Resource.Id.txtGender);
            EditText icNo = FindViewById<EditText>(Resource.Id.txtICNo);
            EditText address = FindViewById<EditText>(Resource.Id.txtAddress);
            EditText phoneNo = FindViewById<EditText>(Resource.Id.txtPhoneNo);
            EditText email = FindViewById<EditText>(Resource.Id.txtEmail);
            EditText password = FindViewById<EditText>(Resource.Id.txtPwd);
            EditText payMethod = FindViewById<EditText>(Resource.Id.txtPayMethod);

            PUTWebRef.WebServicePMS webserUpdate = new PUTWebRef.WebServicePMS();
            webserUpdate.PUTAsync(username.Text, fullName.Text, gender.Text, icNo.Text, address.Text, phoneNo.Text, email.Text, password.Text, payMethod.Text);
            webserUpdate.PUTCompleted += WebserUpdate_PUTCompleted;
        }
        private void WebserUpdate_PUTCompleted(object sender, PUTWebRef.PUTCompletedEventArgs e)
        {
                AlertDialog.Builder dialog = new AlertDialog.Builder(this);
                AlertDialog alert = dialog.Create();
                alert.SetTitle("Error");
                alert.SetMessage(e.Result.Message);
                alert.SetButton("Ok", (c, ev) =>
                { });
                alert.Show();
        }

预期输出应为帐户已成功更新。 实际输出为该帐户不存在

0 个答案:

没有答案