Android Question base64string problem

Imran_Farooq

Member
Licensed User
Longtime User
hi

the supplied code not worked for update MS Sql update query for PICS2 data field, how ever when it set any static string so it update it, but not update for Base64string.



base64string:
Private Sub Button2_Click
    Try
        If TK_IMAGE=True Then
            Dim sf As Object = xui.Msgbox2Async("Update Image?", "Image Update Confirmation", "Yes", "", "No",  Null)
            Wait For (sf) Msgbox_Result (Result As Int)
            If Result = xui.DialogResponse_Positive Then
                Log(TK_IMAGE)
                Dim base As String = Base64EncodeDecodeImage.Base64ImageToString(File.DirInternal,"Test.png")
                Log(base)
                If Base64EncodeDecodeImage.ValidBase64(base) Then
                Log("valid string")
                End If
                ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
                ProgressDialogShow2("Waiting..",False)
                Private MainUrl As String = B4XPages.MainPage.db_server & "/schoolapp/NewRegistration.aspx"
                Dim mianjob As HttpJob
                mianjob.Initialize("", Me)
                'Dim su As StringUtils

                   'Log(su.EncodeUrl(base, "UTF-8"))

'''''''''''''''''''''''''''''''''''''it worked''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
                mianjob.PostString(MainUrl, "UPDATE STUDENT_PROFILE SET PICS2='testing string',PICS2_UPDATED=1  WHERE CELL='" & B4XPages.MainPage.CELL_num & "' AND 
SNO=" & B4XPages.MainPage.SNO & " AND IDNO=" & B4XPages.MainPage.IDNO  )
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

              
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''not worked '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 'mianjob.PostString(MainUrl, "UPDATE STUDENT_PROFILE SET PICS2='" & base & "',PICS2_UPDATED=1  WHERE CELL='" & B4XPages.MainPage.CELL_num & "' AND SNO=" & B4XPages.MainPage.SNO & " AND IDNO=" & B4XPages.MainPage.IDNO  )
        
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''



                Dim body As String = "query=" & su.EncodeUrl(sql, "UTF-8")
                mianjob.PostString(MainUrl, body)
                mianjob.GetRequest.Timeout=9000
                Wait For (mianjob) JobDone(mianjob As HttpJob)
                If mianjob.Success Then
                    Dim parser2 As JSONParser
                    Dim response2 As String = mianjob.GetString
                    parser2.Initialize(response2)
                    File.Copy(File.DirInternal, "Test.png", File.DirInternal, upd_img_fn)
                    ToastMessageShow("Updated - OK, Please Restart the APP",True)
                    Sleep(3000)
                    ExitApplication
                Else
                    ToastMessageShow("Not Updated,Please Try Again !",True)
                End If
                mianjob.Release
                ProgressDialogHide
                Sleep(3000)
            End If
            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            If File.Exists(File.DirInternal, "untitled.jpg") = True Then
                Dim bmp As B4XBitmap = xui.LoadBitmap(File.DirInternal,"untitled.jpg")
                ImageView1.SetBitmap(bmp)
            End If
            TK_IMAGE=False
            B4XPages.ClosePage(Me)
        Else
            ToastMessageShow("Empty Image Not Allowed!",True)
        End If
    Catch
        ToastMessageShow(LastException.Message,True)
    End Try
End Sub
 

MarcoRome

Expert
Licensed User
Longtime User
Maybe the problem is the following:

If I execute the query using a simple string:
PICS2='testing string' the PICS2 column is updated correctly.

However, if I replace "testing string" with the Base64 string:
PICS2='" & base & "' the update does not work.

I have already checked the Base64 string with:
If Base64EncodeDecodeImage.ValidBase64(base) Then
Log("valid string")
End If
and it is reported as a valid Base64 string.

Therefore, the SQL query itself seems to work when the value is short, but it fails when I send the Base64 image string.
Could the problem be related to:

1) the SQL Server datatype/size of the PICS2 column;
ALTER TABLE STUDENT_PROFILE ALTER COLUMN PICS2 VARCHAR(MAX);

2) URL/HTTP encoding of the Base64 string
Dim su As StringUtils
Dim body As String = "query=" & su.EncodeUrl(sql, "UTF-8")
mianjob.PostString(MainUrl, body)
mianjob.GetRequest.SetContentType("application/x-www-form-urlencoded")

3) the ASP.NET NewRegistration.aspx page/request size limit;
If NewRegistration.aspx is classic ASP.NET (.NET Framework), httpRuntime maxRequestLength has a default value of 4096 KB.
Additionally, IIS has maxAllowedContentLength, which defaults approximately 28.6 MB.
B4X:
<httpRuntime
and:
<requestLimits

For example, you might have:
<configuration>
<system.web>
<httpRuntime
maxRequestLength="20480"
executionTimeout="300" />
</system.web>
<system.webServer>

<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="20971520" />
</requestFiltering>
</security>

</system.webServer>
</configuration>

Here, we have set the limit to approximately 20 MB:
maxRequestLength="20480"
is expressed in KB.
Whereas:
maxAllowedContentLength="20971520"
is expressed in bytes.

If ASP.NET exceeds maxRequestLength, the request may be blocked before NewRegistration.aspx even processes the code. IIS, on the other hand, may return a "413.1 Content Length Too Large" error (among others) when the content limit is exceeded.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Last edited:
Upvote 0

aeric

Expert
Licensed User
Longtime User
There are many code smell in the code.

You should not post SQL query String in http job. Use jRDC3 or Web API server.

You should use parameterized query.

Try execute SELECT query at the backend to see if you can return a result using the condition you use for UPDATE.
e.g:
B4X:
Dim strSQL As String = "SELECT * FROM STUDENT_PROFILE WHERE CELL = ? AND SNO = ? AND IDNO = ?"
sql1.ExecQuery2(strSQL, Array As String(CELL_Num, SNO, IDNO)

As suggested by other members above, check the column length of the column PICS2.
 
Upvote 0
Top