1

I have a procedure in C# code-behind that reads values from several fields in a database and puts each into its own 1 dimensional array. Now I need to take those values and pass them to the client side so that I can print them out on a thermal printer.
In my code-behind I have:

        Public String[] PayType = new string[DetailLines];
        Public String[] PayQuantity = new string[DetailLines];
        Public String[] PayAmount = new string[DetailLines]'
        Public String Location;

       Protected Void PrintReceipt()
       *Miscellaneous code down to this point not shown.* 
        **DetailLines comes from record count**
       Location = "CA";
       PayType = new string[DetailLines];
       PayQuantity = new string[DetailLines];
       PayAmount = new string[DetailLines]'
        foreach (DataRow row in dtPOSdetail.Rows)
        {
           PayType[LineNumber] = row["fldDescription"].ToString();
           PayQuantity[LineNumber] = row["fldQuantity"].ToString();
           PayAmount[LineNumber] = string.Format("{0:C}",
           Decimal.Parse(row["fldAmount"].ToString()));
           LineNumber++;
         }

In my JavaScript I have:

var getLocation = '<%= Location %>'
var getPayType =  '<%= PayType %>'
var getPayQuantity = '<%= PayQuntity %>'
var getAmount = '<%= PayAmount %>'

I want to be able to do loop through the three arrays in my printer setup to establish line items.

request += builder.createTextElement({ data: "Location: " + GetLocation + "\\n" });
for (int i = 0; i < PayType.Length; i++)
{
   request += builder.createTextElement({ data: PayType[i] + "\\x9" + PayQuantity[i] + "\\x9" + "     " + PayAmount[i] + "\\n"});
}

I can print out Location but the value of each of my arrays shows as 'System.String[]. Most topics I have read on this are confusing showing the array being loaded as 'var myarray("apples", "oranges", "peaches"). I have also seen topics talking about serializing the array but that is not clear either. Any examples of how to accomplish what I have shown would be greatly appreciated. Thanks Charles

1 Answer 1

1

This happens because Razor (or your veiw engine) is calling the ToString() method of the object being injected into the view script.

With a string array type this produce what you are seeing: System.String[].

As javascript variable is expecting a JSON formatted object to instantiate variables, you can simply serialize to JSON your array as followed:

var getPayType = <%= Newtonsoft.Json.JsonConvert.SerializeObject(PayType) %>;

Sign up to request clarification or add additional context in comments.

1 Comment

Thank You! This is exactly what I was looking forward. Hopefully by me laying things out and your concise answer this can help someone else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.