SSRS how to decode foreign language characters - reporting-services

I'm passing this string to a report: Economia e Administração
But the report displays the following: Economia e Administração
In the URL it gets encoded as:
Economia%20e%20Administra%C3%83%C2%83%C3%82%C2%A7%C3%83%C2%83%C3%82%C2%A3o%20
I tried using URLDecode, but it doesn't work.
Any ideas?
Thanks!

It looks like it's being converted into UTF-8 twice, ie, an encoded string is being encoded again. Is the original string being passed as Unicode or UTF-8 or something else?
CORRECTION: it's converted into UTF-8 three times!

Here's my solution...In parent report convert the string to a Byte array string and pass that to the child report:
Function GetStringBytes(ByVal theString As String) As String
Dim bytes() As Byte = System.Text.Encoding.UTF8.GetBytes(theString, 0, theString.Length)
Dim builder As New System.Text.StringBuilder
For Each i As Integer In bytes
builder.Append(i & "|")
Next i
Return builder.ToString().TrimEnd("|")
End Function
In the Child report pass the byte array string to the GetString function below to convert it back to the original string:
Function GetString(ByVal theBytes As String) As String
Dim byts() As Byte = New Byte(theBytes.Split("|").Length) {}
Dim count As Integer = 0
For Each i As String In theBytes.Split("|")
byts(count) = Convert.ToInt32(i)
count += 1
Next i
Return UTF8ByteArrayToString(byts)
End Function
Function UTF8ByteArrayToString(ByVal theChars As Byte()) As String
Dim aEncoding As System.Text.UTF8Encoding = New System.Text.UTF8Encoding()
Dim aConstructedString As String = aEncoding.GetString(theChars)
Return aConstructedString
End Function
Works perfect for me.

Related

Streamreader to String not working properly

I am getting HttpWebResponse encoded in Base64
following lines get the webresponse from API.
Dim myResp As HttpWebResponse = myReq.GetResponse()
Dim myreader As New System.IO.StreamReader(myResp.GetResponseStream)
the response which i get is something like following, however actual response is too long and i cannot paste here so i have manually stripped the actual response.
{"status":"1","data":"eyJiMmIiOlt7ImludiI6W3siaXRtcyI6W3sibnVtIjoxODAxLCJpdG1fZGV0Ijp7ImNzYW10IjowLCJzYW10Ijo4MDkuOTEsInJ0IjoxOCwidHh2YWwiOjg5OTksImNhbXQiOjgwOS45MX19XSwidmFsIjoxMDYxOC44MiwiaW52X3R5cCI6IlIiLCJwb3MiOiIyNCIsImlkdCI6IjExLTA3LTIwMTgiLCJyY2hyZyI6Ik4iLCJpbnVtIjoiUldHSjA3LzE4LzAwMDU4NCIsImNoa3N1bSI6IjVjMjNiY2M1ZTQ3ZDI0NjU5YWQzNTEzNTM1YjhiNTAzNmM4NGU0MzU5NWJiMTVjYzA4M2VkYzBiNTQzZTQ1MzcifSx7Iml0bXMiOlt7Im51bSI6MTgwMSwiaXRtX2RldCI6eyJjc2FtdCI6MCwic2FtdCI6NDE4LjUsInJ0IjoxOCwidHh2YWwiOjQ2NTAsImNhbXQiOjQxOC41fX1dLCJ2YWwiOjU0ODcsImludl90eXAiOiJSIiwicG9zIjoiMjQiLCJpZHQiOiIyNS0wNy0yMDE4IiwicmNocmciOiJOIiwiaW51bSI6IlJXR0owNy8xOC8wMDEyNjEiLCJjaGtzdW0iOiJjOGEyMjNmNmMzYjY5ODZiYzE2MmNjYjdmMDhlZTYxMTdjYTdkOWZhNmEzYTExMWY1MmVjNzllYmExMGM5MWQ3In1dLCJjZnMiOiJZIiwiY3RpbiI6IjI0QUFCQ1I3MTc2QzFaSiJ9LHsiaW52IjpbeyJpdG1zIjpbeyJudW0iOjEsIml0bV9kZXQiOnsiY3NhbXQiOjAsInNhbXQiOjMzNzUsInJ0IjoxOCwidHh2YWwiOjM3NTAwLCJjYW10IjozMzc1fX1dLCJ2YWwiOjQ0MjUwLCJpbnZfdHlwIjoiUiIsInBvcyI6IjI0IiwiaWR0IjoiMzEtMDctMjAxOCIsInJjaHJnIjoiTiIsImludW0iOiJULTAxNzcvMjAxOC0xOSIsImNoa3N1bSI6ImYzNzFmYjA0N2FjNTRlOTkwYzZjNzM5Zjk0NTgwMzZlMWQxNjE0N2IxYmQ0ZTkxY2FlNmEwN2IyOGVlYzE0YWUifV0sImNmcyI6IlkiLCJjdGluIjoiMjRBQURDSTIwMzJFMVo5In1dfQ=="}
I am not sure why above Base64 Encoded message starts with {"status":"1","data":" and then ends with "}.
Actual Base64 data starts after {"status":"1","data":"
Due to those unsupported characters at starting and ending of the stream , i first try to convert actual response to string as shown below.
Dim myResp As HttpWebResponse = myReq.GetResponse()
Dim myreader As New System.IO.StreamReader(myResp.GetResponseStream)
Actual stream response returns around 248000 characters (as per response received in POSTMAN with same API). Streamreader information in Debug mode also shows same 248000 number. But when i convert them into string with following code line, string gets slimmed to around only 32000 characters. I don't know why this is happening?
Dim myText As String = myreader.ReadToEnd
'''Then following code will remove all those unwanted characters from starting string, which are {"status":"1","data":"
Dim Final_text As String = myText.Substring(myText.Substring(0, myText.LastIndexOf("""")).LastIndexOf("""") + 1)
'''Following code will remove two characters "} from end of the string.
Final_text = Final_text.Trim().Remove(Final_text.Length - 2)
''' Now Decode this proper Base64 String to JSON format
Dim data As Byte() = Convert.FromBase64String(Final_text)
Dim decodedString As String = Encoding.UTF8.GetString(data)
Dim JsonP As JObject = JObject.Parse(decodedString)
Dim SetPointerOut As JToken = JsonP("b2b")
Two things: why converting from Stream to String cut down actual response? 248000 charters to just apprx. 32000 characters. In debug mode if i type in ?mytext.length it returns 248000 as value. But When i hover mouse and brows what is in mytext variable, it shows me around 32000 charters only.
Service provider says Response which i get from API is Base64 encoded and i have to decode it before using it as JSON. Then why do i get unsupported characters at starting of the stream (even in Postman), is it Base64 Encoded message in serialized manner?
Am I doing right process to first convert the stream to string, remove unwanted characters and then Decode it? or there is some other way around.
Ok, issue of 32768 character in debug mode of Visual Studio is it self.
VS2015 had bug in which it does not support more than 32768 characters. Read
Why strings are shown partially in the Visual Studio 2008 debugger?
and
Visual Studio Text Visualizer missing text
The method which i was using to remove extra unwanted characters from "mytext" string, still works and give result. But as #Steve suggested in comment to the question, I should parse the JSON string. I find that idea much better and correct method.
so final code is like below:
Dim myResp As HttpWebResponse = myReq.GetResponse()
Dim myreader As New System.IO.StreamReader(myResp.GetResponseStream)
Dim myText As String = myreader.ReadToEnd
Dim json As String = myText
Dim jsonResult = JsonConvert.DeserializeObject(Of Dictionary(Of String, Object))(json)
Dim jsonObject As Newtonsoft.Json.Linq.JObject = Newtonsoft.Json.Linq.JObject.Parse(json)
Dim jsonValue As JValue = jsonObject("data")
Dim Final_text As String = jsonValue.ToString
''' No need of following code as doing JSON parse above
''' Dim Final_text As String = myText.Substring(myText.Substring(0, myText.LastIndexOf("""")).LastIndexOf("""") + 1)
'''Final_text = Final_text.Trim().Remove(Final_text.Length - 2)
Dim data As Byte() = Convert.FromBase64String(Final_text)
Dim decodedString As String = Encoding.UTF8.GetString(data)
Dim JsonP As JObject = JObject.Parse(decodedString)
Dim SetPointerOut As JToken = JsonP("b2b")

How to retrieve the value of "Quotes" in this json format?

I'm trying to build an app where I can retrieve live Currency values online.
I was using apilayer and I successfully retrieved the data I wanted except for the final currency value.
I'm not sure how to read the value inside the "quotes" node.
Jason Result from API:
{
"success":true,
"terms":"https:\/\/currencylayer.com\/terms",
"privacy":"https:\/\/currencylayer.com\/privacy",
"timestamp":1514567346,
"source":"USD",
"quotes":{
"USDPHP":49.950001
}
}
Using this:
Dim req As HttpWebRequest
Dim res As HttpWebResponse = Nothing
Dim rdr As StreamReader
req = DirectCast(WebRequest.Create("http://apilayer.net/api/live?access_key=xxKeyRemovedxx&currencies=PHP&format=1"), HttpWebRequest)`
res = DirectCast(req.GetResponse, HttpWebResponse)
rdr = New StreamReader(res.GetResponseStream)
Dim jsonresp As String = rdr.ReadToEnd
Dim jResuldict = JsonConvert.DeserializeObject(Of Dictionary(Of String, `Object))(jsonresp)`
Dim qts = jResuldict.Item("quotes").ToString
MsgBox(qts)
Result is:
{
"USDPHP":49.950001
}
I wanted to retrieve only the value inside USDPHP which is 49.95.
So that I can use that value for conversion.
What am I missing?
It looks like quotes is a nested dictionary of name/decimal pairs. To extract it, you can parse your JSON string to a JToken hierarchy, pick out the "quotes" property with SelectTokens(), then deserialize its value with JToken.ToObject(of Dictionary(of String, Decimal))(). Having done so you can work with as you would with any dictionary, for instance by looping through its key/value pairs with For Each:
' Extract and deserialize quotes dictionary
Dim quotes as Dictionary(of String, Decimal) = JToken.Parse(jsonresp) _
.SelectTokens("quotes") _
.Select(Function(d) d.ToObject(of Dictionary(of String, Decimal))()) _
.SingleOrDefault()
' Show quotes to the user
Console.WriteLine(If(quotes.Count = 1, "There is 1 quote: ", string.Format("There are {0} quotes", quotes.Count)))
For Each pair in quotes
Dim name as String = pair.Key
Dim quote as Decimal = pair.Value
Console.WriteLine(" Quote for {0} is {1}.", name, quote)
Next
Which outputs
There is 1 quote:
Quote for USDPHP is 49.950001.
If you are sure that one and only one "quotes" token will be present, you can simplify that a bit by using SelectToken() rather than SelectTokens():
' Extract and deserialize quotes dictionary
Dim quotes as Dictionary(of String, Decimal) = JToken.Parse(jsonresp) _
.SelectToken("quotes") _
.ToObject(of Dictionary(of String, Decimal))()
Finally, if you happen to know the name USDPHP in advance you can pick out its specific value with SelectToken() then cast it to Decimal with an explicit cast:
Dim quote as Decimal = CType(JToken.Parse(jsonresp).SelectToken("quotes.USDPHP"), Decimal)
Sample working .Net fiddle.
Try this, i have not tested it but it should work
Dim qts = jResuldict.Item("quotes.USDPHP").ToString
MsgBox(qts)

Get json value in VB.NET

How can I get the name of a key in a json file? I have a json that I'm parsing in VB.NET and one of the 'fields' have a dynamic name (it changes). What could I do to get the key name?
For example:
...
"one":{
"two":{
"example":[
{
"aaa":"test",
"bbb":"test",
"ccc":"test"
},
...
I'm getting correctly all the values (test, test, test...) and the keys 'one', 'two', have always the same name. But the key 'example' changes the name according the json file information. How could I identify the key text?
I wrote a piece of code that converts JSON into a XDocument here: https://github.com/dday9/.NET-JSON-Transformer
If you were to use that code, then you could get the node that represents your "two" object and then get the first child node in to. By doing this, you're essentially getting the array by an Index instead of by a name.
Here is a quick example of what I mean:
Dim literal As String = "{""two"":{""example"":[{""aaa"":""test"",""bbb"":""test"",""ccc"":""test""}]}}"
Dim xJSON As XDocument = JSON.Parse(literal)
Dim object_two As XElement = xJSON.Descendants("two").FirstOrDefault()
If object_two IsNot Nothing Then
Dim first_descendent As XElement = object_two.Descendants().Skip(1).FirstOrDefault()
If first_descendent IsNot Nothing Then
Console.WriteLine(first_descendent)
End If
End If
Fiddle: Live Demo
This piece will allow to get data from an unknown JSON structure, without having to define a class.
Sample
Dim serializer As System.Web.Script.Serialization.JavaScriptSerializer
serializer = New JavaScriptSerializer()
' {"elements":[{"handle~":{"emailAddress":"myself#example.com"},"handle":"urn:li:emailAddress:348955221"}]}
dim json as string
Dim obj As System.Collections.Generic.IDictionary(Of String, Object)
obj = serializer.Deserialize(Of System.Collections.Generic.IDictionary(Of String, Object))(json)
dim email as string=string.empty
email = If(GetJsonValue(obj, {"elements", "handle~", "emailAddress"}.ToList()), email)
The Function, very self descriptive:
''' <summary>decode json data </summary>
Public Function GetJsonValue(ByVal obj As Object,
ByVal key As List(Of String)) As String
GetJsonValue = Nothing
' If the object is an array, assume any element can contain the key
If obj.GetType Is GetType(Object()) Then
For Each newObj As Object In CType(obj, Object())
Dim tmp As String = GetJsonValue(newObj, key)
If Not String.IsNullOrEmpty(tmp) Then Return tmp
Next
Else
Dim objEle As System.Collections.Generic.IDictionary(Of String, Object)
Dim keyName As String
Dim objKey As String
'
keyName = key(0)
objEle = CType(obj, System.Collections.Generic.IDictionary(Of String, Object))
objKey = objEle.Keys.ToArray()(0)
If objEle.ContainsKey(keyName) Then
Dim temp As Object = objEle.Item(keyName)
If key.Count > 1 Then
' if the element is array, we need to get the array element and move to the next
key.RemoveAt(0)
Return GetJsonValue(temp, key)
Else
Return temp.ToString()
End If
End If
End If
End Function
I see this is solved but would like to suggest another solution for future readers. The JavaScriptSerializer can return a nested dictionary collection (Of String, Object). I find it easier to explore the result in debug while coding. The code below shows an example of how to navigate the collections.
Dim deserializer As New System.Web.Script.Serialization.JavaScriptSerializer
Dim text As String = "{""two"":{""example"":[{""aaa"":""test"",""bbb"":""test"",""ccc"":""test""}]}}"
Dim dict As Dictionary(Of String, Object) = deserializer.DeserializeObject(text)
Dim keys As Dictionary(Of String, Object).KeyCollection
keys = dict("two")("example")(0).Keys
Dim aaaName As String = keys(0)
Dim aaaValue As String = dict("two")("example")(0)(aaaName)

Web API Contoller - What is the proper format of the data to be returned

I have Web API controller that retrieves ticket information. At the start -- The API is called and the request is routed to the proper controller function. The controller passes a request to the database. From there the retrieved data is ran through a dictionary block where the field name is associated with the data. Next the data is serialized. Then the data is passed back to the controller. At this point I know the Json string looks good. But, when the properly formatted json data is passed back to the caller a bunch slashes are added to the output.
My understanding is that Web API is supposed to automatically format the return data. I suspect I am formatting the data for the controller correctly before it is returned.
Public Function GetTicketSearch(ByVal SourceTktNum As String) As Object
'GET api/outage/SourceTktNum
Dim strFullName As String = MethodBase.GetCurrentMethod().ReflectedType.FullName
Dim strMethodName As String = MethodBase.GetCurrentMethod().Name
Dim strClassRoutine As String = strMethodName & " / " & strFullName
Try
Dim objJsonRptRtn As Object = GetReportData_bllBLL.BLL__DataSet__GetReportData__GetData(strMARCLSysId, strLogonSysId, SourceTktNum)
'AT THIS POINT I KNOW THE JSON STRING LOOKS AS IT SHOULD.
Return objJsonRptRtn
'AFTER THE ABOVE STATEMENT SOMETHING HAPPENS TO THE DATA / SLASHES ARE ADDED TO THE OUTPUT TO BE RETURNED BY THE API
Catch ex As Exception
Dim strExMessage As String = ex.Message
Dim strStackTrace As String = ex.StackTrace
Dim strMsg As String = strExMessage & ControlChars.CrLf & ControlChars.Lf & strStackTrace & ControlChars.CrLf & ControlChars.Lf
MailLogEvent.BLL__Process__MailAndLogEvent__AddLogEntry(strMARCLSysId, strLogonSysId, 901020, dteTime_Start, 0, strMsg, strClassRoutine)
Throw New HttpResponseException(HttpStatusCode.InternalServerError)
End Try
End Function
Code to create JSON object to be passed back to the controller...
'--------------------------------------------------------- Create Json String
Dim dctDataDictionary As New Dictionary(Of String, String)
dctDataDictionary.Add("sourceTktNum", strSourceTktNumKey)
dctDataDictionary.Add("incidentTime", strIncidentTime)
dctDataDictionary.Add("incidentEndTime", strIncidentEndTime)
dctDataDictionary.Add("recordTimeStamp", strRecordTimeStamp)
dctDataDictionary.Add("outageReasonCd", strOutageReasonCd)
dctDataDictionary.Add("numDS3", strNumDS3)
dctDataDictionary.Add("numBlocked", strNumBlocked)
dctDataDictionary.Add("numVOIP", strNumVOIP)
dctDataDictionary.Add("numWireline", strNumWireline)
dctDataDictionary.Add("numEndUserCircuits", strNumEndUserCircuits)
dctDataDictionary.Add("stateCd", strStateCd)
dctDataDictionary.Add("city", strCity)
dctDataDictionary.Add("incidentDescription", strIncidentDescription)
dctDataDictionary.Add("causeDesc", strCauseDesc)
dctDataDictionary.Add("equipFailedDesc", strEquipFailedDesc)
dctDataDictionary.Add("networkPartDesc", strNetworkPartDesc)
dctDataDictionary.Add("restoreMethodDesc", strRestoreMethodDesc)
objJsonRptRtn = New System.Web.Script.Serialization.JavaScriptSerializer().Serialize(dctDataDictionary)
Return objJsonRptRtn
This could be happening because you are again trying to convert a json data into json, which results in extra slashes.
Can you show the code before you return 'objJsonRptRtn'.
As it turned out I was double serializing. I removed the statement that serialized the dictionary output. Then, simply passed the dictionary back to the controller then let the controller return the dictionary. Everything works great...
'--------------------------------------------------------- Create Dictionary
dctDataDictionary.Add("sourceTktNum", strSourceTktNumKey)
dctDataDictionary.Add("incidentTime", strIncidentTime)
dctDataDictionary.Add("incidentEndTime", strIncidentEndTime)
dctDataDictionary.Add("recordTimeStamp", strRecordTimeStamp)
dctDataDictionary.Add("outageReasonCd", strOutageReasonCd)
dctDataDictionary.Add("numDS3", strNumDS3)
dctDataDictionary.Add("numBlocked", strNumBlocked)
dctDataDictionary.Add("numVOIP", strNumVOIP)
dctDataDictionary.Add("numWireline", strNumWireline)
dctDataDictionary.Add("numEndUserCircuits", strNumEndUserCircuits)
dctDataDictionary.Add("stateCd", strStateCd)
dctDataDictionary.Add("city", strCity)
dctDataDictionary.Add("incidentDescription", strIncidentDescription)
dctDataDictionary.Add("causeDesc", strCauseDesc)
dctDataDictionary.Add("equipFailedDesc", strEquipFailedDesc)
dctDataDictionary.Add("networkPartDesc", strNetworkPartDesc)
dctDataDictionary.Add("restoreMethodDesc", strRestoreMethodDesc)
Return dctDataDictionary
Public Function GetTicketSearch(ByVal SourceTktNum As String) As Object
'GET api/outage/SourceTktNum
Dim strFullName As String = MethodBase.GetCurrentMethod().ReflectedType.FullName
Dim strMethodName As String = MethodBase.GetCurrentMethod().Name
Dim strClassRoutine As String = strMethodName & " / " & strFullName
Try
Dim dctDataDictionary As Object = GetReportData_bllBLL.BLL__DataSet__GetReportData__GetData(strMARCLSysId, strLogonSysId, SourceTktNum)
If dctDataDictionary Is Nothing Then
Throw New HttpResponseException(HttpStatusCode.PartialContent)
Else
Return dctDataDictionary
End If
Catch ex As Exception
Dim strExMessage As String = ex.Message
Dim strStackTrace As String = ex.StackTrace
Dim strMsg As String = strExMessage & ControlChars.CrLf & ControlChars.Lf & strStackTrace & ControlChars.CrLf & ControlChars.Lf
MailLogEvent.BLL__Process__MailAndLogEvent__AddLogEntry(strMARCLSysId, strLogonSysId, 901020, dteTime_Start, 0, strMsg, strClassRoutine)
Throw New HttpResponseException(HttpStatusCode.InternalServerError)
End Try
End Function

Send RDLC in an email using vb.net

Okay my website generates thousands of PDFs using RDLCs but my problem is sometimes I want to email them but I don't want to attach a PDF to an email. So what I need is a way to generate the report then either convert it into text or html so I can send it as the body of an email.
Also I am using reportviewr version 11
Also I have tried exporting it as a .doc then trying to convert it to text and i have tried to export it to an excel document then tried to convert it and none of it works.
Dim warn() As Warning = Nothing
Dim streamids() As String = Nothing
Dim mimeType As String = String.Empty
Dim encoding As String = String.Empty
Dim extension As String = String.Empty
Dim bytes() As Byte
bytes = rv.LocalReport.Render("MHTML", Nothing, mimeType, encoding, extension, streamids, warn)
'Only one copy of the notice is needed
'If Not Directory.Exists(strFilePath) Then Directory.CreateDirectory(strFilePath)
Dim fs As New FileStream(strFilePath, FileMode.Create)
fs.Write(bytes, 0, bytes.Length)
fs.Close()
here is the code i'm using but it gives me an error : Specified argument was out of the range of valid values. Parameter name: format
Also i know this code works because I use the exact same thing to export the rdlc to a PDF
Ok so I solved my own problem with some Research about bytes.
Here is the code that I used to solve my problem.
What I did was exported the reportviewr as a word document and then converted all bytes to text. Then from that you end up with a whole bunch of gibberish but eventually you will find the text from your RDLC. So what I did was split the string up to where I was only left with the wording from my RDLC.
Review the code below:
Function GetRDLCText(ByVal rv As ReportViewer) As String
Dim warn() As Warning = Nothing
Dim streamids() As String = Nothing
Dim mimeType As String = String.Empty
Dim encoding As String = String.Empty
Dim extension As String = String.Empty
Dim bytes() As Byte
Dim msg() As String
bytes = rv.LocalReport.Render("WORD", Nothing, mimeType, encoding, extension, streamids, warn)
'Word is the only export that contains text from the rdlc
Dim content As String = System.Text.Encoding.Unicode.GetString(bytes)
msg = content.Split("Ù")
msg = msg(1).Split("Ѐ")
Return msg(0)
End Function
This solution is not for everyone, but it works for what I need it to do.