0

Coming from PHP background, I often used this:

How to POST data as an indexed array of arrays (without specifying indexes)

This way I could have item id's in the post data as array keys. Loop through the array and insert into DB the id's using the keys.

Now I'm trying to achieve the same in C# but cannot find a way. I tried searching for answers but nothing seems to suggest C# solution:

Get posted value In C# ASP.NET

Submitting form elements with the same name

I can't even get to the POST part. C# complains about this already:

<asp:TextBox ID="txt[1][1]" runat="server" />
<asp:TextBox ID="txt[1][2]" runat="server" />
<asp:TextBox ID="txt[1][3]" runat="server" />

Parser Error Message: 'txt[1][1]' is not a valid identifier.

Is there a better way in C# for me to do this?

5
  • special characters aren't allowed in variable names [ and ] Commented May 18, 2016 at 7:25
  • the POST data will consist of name-value-pairs based on the name attribute of form elements, not the ID Commented May 18, 2016 at 7:26
  • You should change your ID to something like: txt_1_1 instead of using [ and ] Commented May 18, 2016 at 7:29
  • Thanks David. I assumed that C# will give it the name attribut ethe same when i use ID. I changed the "ID" to "NAME" and managed to post data, It now complains about my loop. var postedValues = Request.Form.GetValues("txt"); foreach (var value in postedValues) { Response.Write(value); } Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. Commented May 18, 2016 at 7:30
  • @Ian, I thought of that, but then I would have to split the name to get id's ?? The numbers for ID / NAME of the field is not just to give it uniqueness but also holds item id's that i need. I like the way PHP automatically put it into array you can loop through. Can C# do this, or am i thinking about it wrong, Is there a better way? Commented May 18, 2016 at 7:34

0

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.