I am getting a return string from web service that I am calling. Now I want to look for specific value which should be present in that return string. Return string is something like this:
pg_response_type=A
pg_response_code=A01
pg_response_description=APPROVED
endofdata
Now I want to check in this string that if pg_response_code=A01 is present or not. I know I have to for loop in the string. What should be the code...? Suppose I am storing this string in string Response;
Simply use String.Contains:
bool contains = response.Contains("pg_response_code=A01")
Without writing a parser or looping you can still check for specific values like this:
string response; //response in here.
if(response.contains("pg_response_code=A01"))
{
//do something
}
Related
I am trying to test a call to my one of my functions.
This is called from outside of my website an acts like a Webservice.
To test im trying to pass the parameters though my url.
http://localhost:0000/APIService/UploadValuationDetails?ValuationDetails=[{property_details_address_address1{TagValue:'Test'},{ImageBase64:''}}]?Id=4785
My code in my service:
public void UploadValuationDetails(Dictionary<string, ValuationDetails> JsonResult, int Id)
{
DatabaseHelper DBH = new DatabaseHelper();
foreach (var item in JsonResult)
{ //(ValuationId , TagName , TagValue , ImageBase64)
DBH.WSValuationDetailUpdate(Id, item.Key, item.Value.TagValue, item.Value.ImageBase64);
}
}
ValuationDetails class:
public class ValuationDetails
{
public string TagValue { get; set; }
public string ImageBase64 { get; set; }
}
Edit Changed ? for the second parameter to &:
> http://localhost:0000/APIService/UploadValuationDetails?ValuationDetails={'property_details_address_address1':[{TagValue:'Test',ImageBase64:''}]}&Id=4785
After changing my url to the one above a break point was hit but the values were incorrect.
Edit 2 Trying to get the correct values in the json result.
I think i'm closer:
http://localhost:0000/APIService/UploadValuationDetails?JsonResult={TagName:"property_details_address_address1",ValuationDetails:{TagValue:"Test","ImageBase64:""}}]&Id=4785
But now my jsonResult = 0
You should use an ampersand (&) to separate multiple query string parameters. As you have it, you're using ?, so "?Id=4785" is being interpreted as part of the value for the ValuationDetails parameter.
Corrected:
this is correct ┐
↓
http://localhost:0000/APIService/UploadValuationDetails?ValuationDetails=
[{property_details_address_address1{TagValue:'Test'},{ImageBase64:''}}]&Id=4785
↑
but this should be fixed ┘
I think it is better to Encode the JSON too.
Since the moment you will have for instance in side your data an ? or & you will get an exception too.
Your URL string looks improperly formatted.
For the separator of the URL and the parameters you would use ?.
But to separate parameters use &
http://localhost:0000/APIService/UploadValuationDetails?ValuationDetails=[{property_details_address_address1{TagValue:'Test'},{ImageBase64:''}}]&Id=4785
Your JSON is invalid.
I've worked with it a bit, but it still needs input from you.
[
{
"property_details_address_address1": {
"TagValue": "Test"
},
"needs_a_name_here": {
"ImageBase64": ""
}
}
]
Notice that i've put quotes around the names. And your second object also requires a name.
I used JSONLint to validate and create the proper json
I am trying to convert an object (coming from a SQL server), into a integer so I can format the number to have the correct amount of zero's in front of it.
For example:
If I were to have 25.6, I would need it to be 0025.6.
Now I have looked online on how to do this, but the methods that I have seen people post are not working for me. I am not entirely sure why. I am trying to format GlobalVariables.grossweightafter. I read the value GlobalVariables.grossweight from the SQL server, but then when I TryParse it, it loses its value. The code I have is below:
while (TransferRecord.Read())
{
//Pulling data from the SQL server. getting data for every line of code as specified.
GlobalVariables.baledate = TransferRecord["keyprinter_datetime"];
GlobalVariables.baleline = TransferRecord["pulp_line_id"];
GlobalVariables.baleid = TransferRecord["bale_id"];
GlobalVariables.grossweight = TransferRecord["bale_gross_weight"];
GlobalVariables.grossweightflag = TransferRecord["gross_value_flag"];
GlobalVariables.baleairdrypercent = TransferRecord["bale_airdry_pct"];
GlobalVariables.airdryflag = TransferRecord["airdry_value_flag"];
//Converting the date, and the baleid to fit in the string.
DateTime.TryParse(GlobalVariables.baledate.ToString(), out GlobalVariables.baledateafter);
int.TryParse(GlobalVariables.baleid.ToString(), out GlobalVariables.baleidafter);
int.TryParse(GlobalVariables.grossweight.ToString(), out GlobalVariables.grossweightafter);
GlobalVariables.grossweightafter.ToString("0000.0");
//Calling the WriteData method.
WriteData();
}
So I was wondering if anyone can catch what I am doing wrong, or they can help me out on the correct way to go about this.
What #Hans Passant was saying is that you need to assign the value returned from .ToString. That line should be:
GlobalVariables.grossweightafter = GlobalVariables.grossweightafter.ToString("0000.0");
The last lines should be
if(int.TryParse(GlobalVariables.grossweight.ToString(), out GlobalVariables.grossweightafter))
{
string grossWeightAfter = GlobalVariables.grossweightafter.ToString("0000.0");
//you need to save the string returned from the ToString-method somewhere or it will be lost.
///Alternatively, if GlobalVariables can contain strings aswell:
GlobalVariables.grossweightafter = GlobalVariables.grossweightafter.ToString("0000.0");
}
else
{
//React on value not being an int
}
Maybe you should try to use double.TryParse() method instead of int.TryParse(), because int does not have fractional part?
Also, you need to store ToString() result to a string variable. Your code should be like this:
GlobalVariables.grossweightafterstring = GlobalVariables.grossweightafter.ToString("0000.0");
I have a problem which may look easy but I can't solve it.
I have a function which returns a string and has two arguments:
public string MyFunc(string ID, string TargetID) // ID is 9999999995 and TargetID is 9999999998
{
return ID + TargetID; // Gives me ID only(9999999995).
}
When I try to put text instead of the ID, it works, but not with a number. I've tried to use StringBuilder but I get the same result.
I use it like this:
MessageBox.Show(MyFunc(Settings.Default.ID, ComboBox1.Text));
The function is working as intended. Check that the incoming parameters ID and TargetID actually contain values and they are the values you expect.
Check the values are not null to be safe and do a String.Concat(...)
Thank you everybody, but the problem was in receiving the string from netStream, That was my fault. When sending the string I haven't added the "$" char in the end and so the string maybe was so long or something...
On the client, I have a textarea and I extract its value like this:
var TheURIEncodedString = encodeURI($('#TheTextArea').val());
The string then looks like this:
"This%20is%20a%20test%0AThis%20is%20a%20new%20line"
I send this string via ajax and when I receive it on the server, I need to count the number of actual characters, not the length of the string.
How can I do this?
Use HtppUtility.UrlDecode:
HttpUtility.UrlDecode("This%20is%20a%20test%0AThis%20is%20a%20new%20line").Length;
Result:
This is a test
This is a new line
Use the HtppUtility.UrlDecode method
int numberOfCharacters =
HttpUtility.UrlDecode("This%20is%20a%20test%0AThis%20is%20a%20new%20line").Length;
Is there an easy way to check if a format string is valid? For example the following is code that we use to test a number format string;
public static bool IsValidFormatStringNumber(string FormatString)
{
try
{
const decimal number = 0.056m;
var formattedNumber = number.ToString(FormatString);
return formattedNumber.Length > 0;
}
catch
{
return false;
}
}
We're trying to catch an exception or determine if the resulting string has no length. This test fails however as a format string of "hsibbur" (Any rubbish) results in a string of "hsaibbur", which has length.
We want to do the same test for Percent and Date format string.
If you just want to check for standard format strings, just check that your format strings are part of that list.
If you want to check for custom format strings (that are not "Other" or "Literal strings"), you can probably craft a regex to do it.
Other than that, since format strings can be arbitrary strings, I don't think validation even applies.
If FormatString is equal to formattedNumber, that could be another case for returning false.