0

I have a list in javascript and I need save them to MSSQL DB. Please suggest any solution? I want to take them to asp.net cs file with the session.

function myFunction() {
            var chks = document.querySelectorAll("#KitapTurs input[type='checkbox']");
            var sayi = chks.length;
            var userchoise = new Array();//this is my array
            for (var i = 0; i < sayi; i++) {
                if (chks[i].checked) {
                    userchoise.push(chks[i].id);
                }
            }
            for (var i = 0; i < userchoise.length; i++) {
                document.write(userchoise[i] + " / ");
            }
//this is javascript session
            sessionStorage.setItem("userchosed", userchoise);
        };

I want to save my array to DB in asp.net

2
  • You can use fetch to send data from a web browser back to a asp.net page on the server. That page can store the data in the database. developer.mozilla.org/en-US/docs/Web/API/Fetch_API Commented Dec 8, 2020 at 9:24
  • thanks for your comment @Kokodoko. i am looking for more easy solution. Commented Dec 8, 2020 at 9:30

2 Answers 2

1

You can create a hidden field and set its value that you want to get at server side

<input type="hidden" value="" id="DataHolder" name="DataHolder" />

Then you can append values to a variable with any delimiter (here it is comma)

var userChoiseData = "";
for (var i = 0; i < sayi; i++) {
    if (chks[i].checked) {
        //userchoise.push(chks[i].id);
        //Append values to userChoiseData variable
        userChoiseData = userChoiseData + chks[i].id + ",";
    }
}

After loop iteration you will have comma delimited value in the variable userChoiseData

Set this as hidden field's value

document.getElementById("DataHolder").value = "userChoiseData;

Post back the page and get the value at server side (.cs) with:

Request.Form["DataHolder"]
Sign up to request clarification or add additional context in comments.

Comments

0

Hum, I think if you want to send that array up to the server side and process it?

It not 100% clear the data type. But I would consider just adding a web method to the page in question.

So, you code say is this:

var userchoise = new Array();//this is my array

 for (var i = 0; i < sayi; i++) {
    if (chks[i].checked) {
       userchoise.push(chks[i].id);
    }
 }

    $.ajax({
        type: "POST",
        url: 'MyWebpageTest.aspx/PassArray',
        data: JSON.stringify({ "MyIdList" : userchoise }),
        contentType: "application/json; charset=utf-8",
        datatype: "json",
        success: function () {
            alert('ok');
        }
    });

And the server side Sub in the web page?

This looks ok:

<WebMethod()>
Public Shared Sub Pass1(MyIdList As Integer())

    For Each v As Integer In MyIdList
        Debug.Print(v)
    Next
    ' code here to write array of id's to database.

End Sub

The above assumes jQuery for the ajax post. Do keep in mind that you not have to set a page post back. Nor bother with a page post back. The effort to get a page post back (with hidden controls) is a bit of setup. And this means you don't need nor have a page post back anyway.

And in above, I put the sub in the same as the web page - but that web page class will NOT have a instance created - so I used "shared" (which is static in c#). So above would become this:

[WebMethod()]
public static void Pass1(int[] MyIdList)
{
foreach (int v in MyIdList)
    Debug.Print(v);
}

So I think just passing the array up without a bunch of hidden fields, and even having to setup a post back? You can just send the array. A small Sub (void function) that just sends the array to the server means at that point you can do what you please/want with the array. You don't mention if the array is just a list of "id" or has some type of additional structure. I think a hidden field/text box is possible - but you have to execute a post back, and either way its more steps - the ajax calls are really simple, and the types are not only strong, but the json string parameters match what you declare server side - so I think you write the least amount of js code with this approach, and no additonal client side controls are required - except for jQuery.

Comments

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.