Android Question Problem in INSERT query using okhttputils

Sofiya

Member
Licensed User
i facing problem in inserting rows in database table through download2 or poststring method,
can anyone help me to find the mistake in the fallowing code, thanks in advance
B4X:
Dim job_5 As HttpJob
    job_5.Initialize("job_5", Me)
    job_5.Download2("http://192.168.1.7/posapi/insertKot/addkot", _
    Array As String("kotno","'1'","itemcode","abc","itemcategory","food","unqno",2,"reason","","rate",21,"amount",21,"quantity",1,"taxrate",3,"preferencepckey",123,"kotdate",DateTime.Now,"status","A","itempckey",1234))
 

MarkusR

Well-Known Member
Licensed User
Longtime User
about ErrorMessage returned from server if you use wait for instead of event
B4X:
    Dim HttpJob1 As HttpJob
    HttpJob1.Initialize("",Me)
        
    HttpJob1.PostString("https://abc.def.com/command1",JSON)
    HttpJob1.GetRequest.SetContentType("application/json")

    Wait For (HttpJob1) JobDone(HttpJob1 As HttpJob)
    If HttpJob1.Success Then
        Log(HttpJob1.GetString)
        ' = HttpJob1.GetString
    Else
        ' = HttpJob1.ErrorMessage
        ToastMessageShow(HttpJob1.ErrorMessage,True)
        Return
    End If
    HttpJob1.Release
 
Upvote 0

Sofiya

Member
Licensed User
following C# code working fine and its inserting values correctly in table, i personally feel problem in b4a side,

B4X:
 public class insertKOTController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
        public class KotDetail1
        {
            public string AddKot(KotDetail kd)
            {
                DateTime dt = DateTime.Now;
                string sqlFormattedDate = dt.Date.ToString("yyyy-MM-dd HH:mm:ss");
                string cs = ConfigurationManager.ConnectionStrings["Model8"].ConnectionString;
                using (SqlConnection con = new SqlConnection(cs))
                {
                    SqlCommand com = new SqlCommand("DKTinsertKotProc", con);
                    com.CommandType = CommandType.StoredProcedure;
                    com.Parameters.AddWithValue("@KoTNo", kd.KotNo);
                    com.Parameters.AddWithValue("@KotDate", dt);
                    com.Parameters.AddWithValue("@UnqNo", kd.unqno);
                    com.Parameters.AddWithValue("@ItemPcKey", kd.ItemPcKey);
                    com.Parameters.AddWithValue("@ItemCode", kd.ItemCode);
                    com.Parameters.AddWithValue("@Quantity", kd.Quantity);
                    com.Parameters.AddWithValue("@Rate", kd.Rate);
                    com.Parameters.AddWithValue("@TaxRate", kd.TaxRate);
                    com.Parameters.AddWithValue("@Amount", kd.Amount);
                    com.Parameters.AddWithValue("@Status", kd.Status);
                    com.Parameters.AddWithValue("@Reason", kd.Reason);
                    com.Parameters.AddWithValue("@PreferencePckey", kd.preferencePckey);
                    com.Parameters.AddWithValue("@ItemCategory", kd.itemcategory);
                    con.Open();
                    int i = com.ExecuteNonQuery();
                    con.Close();
                    if (i >= 1)
                    {
                        return "Record Added Successfully";

                    }
                    else
                    {
                        return "Record Not Added";

                    }
                }
            }
        }
        static KotDetail1 controller = new KotDetail1();
        public JsonResult AddKot(KotDetail kd)
        {
            var response = controller.AddKot(kd);
            return Json(response, JsonRequestBehavior.AllowGet);
        }
    }
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
make try catch in AddKot and return exception message.
also log .ErrorMessage from request in b4a.

C#
B4X:
 try
 {
 
 }
 catch (Exception e)
 {
   return e.ToString();
 }
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
about ErrorMessage returned from server
Unless something major goes wrong on the server side, the server is probably still going to return a status code of 200 (meaning, Job.Success = True) and the error message is going to be in Job.GetString. Note: Rename Job to whatever variable name you gave your HTTPJob.
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
.ErrorMessage is informative if the content type is wrong.
if you use error handling in web api you should check return value at success.
 
Last edited:
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
a row below .SetContentType
if you will use json add in web api a sub
B4X:
public KotDetail TemplateKot()
it will give you a template :)
json tree tool online is available here: http://basic4ppc.com:51042/json/index.html
 
Upvote 0
Top